473,503 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pascal's Triangle

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");
}

Dec 15 '06 #1
4 6655
Aaron wrote:
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.
You are evaluating n!/(r! * (n-r)!).

The partial factorial is a step in the right direction. You can start
dividing before you finish the partial factorial. Perform the
operations in the following order:

n / 1 * (n-1) / 2 * (n-3) / 3 ... (n-r+1)/r

The largest intermediate value is n!/((r-1)!*(n-r)!).

--
Thad
Dec 15 '06 #2
/ n \ / n \
| | == | |
\ r / \n-r/
r= min(r, n-r);
or beter
if (2*r n) r= n - r;

Dec 15 '06 #3
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:
<snip>
uint64_t nCr(int n, int r)
{
return(partial_factorial(n,r)/factorial(n-r));
}
I would suggest you use the identities nCr = (n-1)C(r-1) + (n-1)Cr,
plus xC0 = xCx = 1 (either iterative or recursive implementation).
That way you're adding reasonable sized numbers, rather than
calculating two huge numbers and dividing them by each other.

Michael

Dec 15 '06 #4
On Thu, 14 Dec 2006 18:42:25 -0800, Aaron wrote:
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:
<snip>
If you want to fill the triangle, Michael's method is the way to go;
64 bits will get you up to 67Cr, 128 bits up to 131Cr.

If you want to compute a few (approximate) values of nCr you could use the
lgamma function from the math library:
static double ncr( int n, int r)
{ return exp( lgamma(n+1)-lgamma(r+1)-lgamma(n-r+1));
}
Note though you'll need to use something bigger than a double for n>1029;
1029C514 is 1.4298207e+308, while the above routine returns inf for
1030C515.
Duncan
Dec 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1404
by: Statsstudent2006 | last post by:
Hello! I am a newbie, and have received my first VBA assignment. I have to write a subroutine that generates the Fibonacci sequence of numbers (the first n numbers, where n is an integer number...
25
5691
by: GUPTAJI | last post by:
hi all, can u give me the code to create a Pascal's Triangle........... and yes, it should work thankx
5
6588
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
0
2036
by: compiler | last post by:
hai, iam new to c,i need c-code for pascal triangle,could you please provide me the code. thanks and regards.
0
1827
geo039
by: geo039 | last post by:
I have a program that takes user input from a textbox. Based on those 3 numbers it will tell them whether it is a right triangle, equilateral triangle or not a triangle. I've written 3 constructors...
19
8243
by: lost1 | last post by:
Can someone point me in the right direction on how to get the triangle type to display. Below is a triangle class that is tested by another completely separate class. The main method of the test...
6
13336
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
3
2336
by: abraxas91 | last post by:
Hi, my name is Stephan. I've been working on this program off and on over the past few days. I have yet to perfect it. I would really appreciate any help! For some reason the output begins to...
2
1985
by: J Ivan P Silvestre | last post by:
c #: Implementing the method Int TrianglePascal (int n) which returns the triangle to the level of Pascal N. public static int TrianglePascal(int n) { int arr = new int; for(int...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.