Como calcular grande nPr em C?
Eu escrevi uma função para calcular o nPr de dois números em C, você pode por favor me ajudar a adaptá-lo para lidar com números grandes?
Eu preciso ser capaz de calcular um valor de até 1x10 ^ 12 - eu tentei muitos tipos de dados diferentes e estou muito preso!
#include<stdio.h>
#include<math.h>
int main()
{
long int n=49,k=6;
printf("%li nPr %li = %li\n\n",n,k,nPr(n,k));
return 0;
}
long nPr(long int n, long int k);
long nPr(long int n, long int k){
if (n < 0 ){
printf("\nERROR - n is less than 0\n\n");
return -1;
}
if (k > n ){
printf("\nERROR - k is greater than n\n\n");
return -1;
}
else {
long int i,result = 1,c=n+1-k;
for(i=c; i<=n; i++)
{
result = result * i;
}
return result;
}
}
obrigado
J
ATUALIZAR: Estas são permutações sem repique,
também tentei
long long nPr(long long int n, long long int k);
long long nPr(long long int n, long long int k){
if (n < 0 ){
printf("\nERROR - n is less than 0\n\n");
return -1;
}
if (k > n ){
printf("\nERROR - k is greater than n\n\n");
return -1;
}
else {
long long int i,result = 1,c=n+1-k;
for(i=c; i<=n; i++)
{
result = result * i;
}
return result;
}
}
no entanto, não parece fazer qualquer diferença