A
Aaron
Hi,
I have written a pascals triangle program. However, my program gets a
floating point exception somewhere between 60 and 70 and any subsequent
larger value. This is no doubt due to dividing two large numbers in
the nCr function. By using uint64_t instead of int helped get it this
far. I would like to see if it is possible to use it for larger
values. Unlike with most programs, execution time is of absolutely no
concern. Does anyone have any ideas? Here is my code:
#include <math.h>
#include <stdint.h>
#include <stdio.h>
uint64_t factorial(int n)
{
int i;
uint64_t result=1;
for (i=1; i<=n; i++)
{
result = result * i;
}
return(result);
}
uint64_t partial_factorial(int n, int r)
{
int i;
uint64_t result=1;
for (i=r+1; i<=n; i++)
{
result = result * i;
}
return(result);
}
uint64_t nCr(int n, int r)
{
return(partial_factorial(n,r)/factorial(n-r));
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("usage: pascal <integer>\n");
exit(-1);
}
int size = atoi(argv[1]);
if (size <= 0)
{
printf("Please enter integers greater than 0\n");
exit(-1);
}
uint64_t bc;
int i,j;
for (i=0; i<size; i++)
{
printf("\n");
for (j=0; j<=i; j++)
{
bc = nCr(i, j);
printf("%lld ", bc);
}
}
printf("\n");
}
I have written a pascals triangle program. However, my program gets a
floating point exception somewhere between 60 and 70 and any subsequent
larger value. This is no doubt due to dividing two large numbers in
the nCr function. By using uint64_t instead of int helped get it this
far. I would like to see if it is possible to use it for larger
values. Unlike with most programs, execution time is of absolutely no
concern. Does anyone have any ideas? Here is my code:
#include <math.h>
#include <stdint.h>
#include <stdio.h>
uint64_t factorial(int n)
{
int i;
uint64_t result=1;
for (i=1; i<=n; i++)
{
result = result * i;
}
return(result);
}
uint64_t partial_factorial(int n, int r)
{
int i;
uint64_t result=1;
for (i=r+1; i<=n; i++)
{
result = result * i;
}
return(result);
}
uint64_t nCr(int n, int r)
{
return(partial_factorial(n,r)/factorial(n-r));
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("usage: pascal <integer>\n");
exit(-1);
}
int size = atoi(argv[1]);
if (size <= 0)
{
printf("Please enter integers greater than 0\n");
exit(-1);
}
uint64_t bc;
int i,j;
for (i=0; i<size; i++)
{
printf("\n");
for (j=0; j<=i; j++)
{
bc = nCr(i, j);
printf("%lld ", bc);
}
}
printf("\n");
}