Question

I've been trying to calculate total of 1500th row in pascal triangle in c++.

I tried more than 6 different code snippets from all over the web.

Some of them crashed before 10th row, some gone crazy etc.

How can one achieve calculating total of numbers in 1500th row in pascal triangle.

I think there must be a formula to find a row without iterating over each row, because iterations causing program to crash.

Was it helpful?

Solution

The sum of the numbers in any row is equal to 2 ^ n where n is the row (starting at 0). So in your case, it would just be 1 << 1499.

enter image description here

Your answer is the following:

17537331055217019373813793980140428996762007940165414412037899012395481925281661101828540443292484630826575203397718758699647274470734979877085519459002350423944978242664548632243401355791731473268341092170069314725677729132473171262691809694657480322332526275875721167754624586680565177898054854942790337156977105108828923716313380366502376637658596066837351781686391648520996613526331666834254976000087526677764529440217091269193357761841856604274688

OTHER TIPS

A google search reveals from wiki :

The sum of the entries in the nth row of Pascal's triangle is the nth power of 2.

Hence you have to calculate 2^1500 instead of trying to iterate over all rows.

The row-sum of the pascal triangle is 1<<n with n being the row number (zero based). Your final value is 1<<1499.

So your program neads to display a 1500 bit integer, which should be the main problem. to produce a binary output, use

printf("1"); 
for (int i=1;i<1500;i++) printf("0");
printf("\n");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top