このディレクトリの索引
http://pc12.2ch.net/test/read.cgi/tech/1279286575/56
#  n 個のものの中から r 個のものを取り出すときの組合せの数 nCr を求める関数 CCO(int n, int r) を再帰型で作成しなさい.  
#  ただし, x, y の入力と結果の出力は main関数で行わせること.  
#   
#  #include <stdio.h>  
#   
#  int CCO(int x,int y) 
#  { 
#   if((x < 0) || (x < y)){        
#    return -1;     
#   }else if(x <= 1){          
#    return 1;     
#   }else{        
#    (return x * CCO(x-1))/(return y * CCO(y-1));   
#   }  
#  }  
#  int main(void) 
#  { 
#   int a,b; 
#   
#   printf("値を入力してください: "); 
#   scanf("%d", &a); 
#   
#   printf("値を入力してください: "); 
#   scanf("%d", &b); 
#   
#   printf("%dC%d = %d\n", input1,input2, CCO(a,b)); 
#   return 0; 
#  } 
# 
# 

nCr :-
      催促付き整数入力('n : ',N),
      催促付き整数入力('r : ',R),
      nCr(N,R,X),
      write_formatted('nCr = %tC%t = %t\n',[N,R,X]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%
nCr(N,R,X) :-
      U is N - R + 1,
      階乗(U,N,K1),
      階乗(R,K2),
      X is K1 // K2 .

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
階乗(M,N,1) :- M > N,!.
階乗(M,N,X) :-
      M2 is M + 1,
      階乗(M2,N,Y),
      X is M * Y.

階乗(0,1) :- !.
階乗(1,1) :- !.
階乗(N,X) :-
      N2 is N - 1,
      階乗(N2,Y),
      X is N * Y,
      asserta((階乗(N,X) :- (!))).