4

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.

Max Abrahamsson
  • 1,460
  • 3
  • 17
  • 35
  • What is the basic approach of what you have tried ? – Rndm Sep 29 '12 at 13:30
  • all of the codes i tried was based on iterating over rows one by one till Nth row – Max Abrahamsson Sep 29 '12 at 13:31
  • one approach was this : http://jithusunnyk.blogspot.com/2011/09/pascals-triangle-c.html – Max Abrahamsson Sep 29 '12 at 13:32
  • Are you trying to calculate sum of numbers in a row ? – Rndm Sep 29 '12 at 13:35
  • When attacking this kind of problems, do not throw yourself into implementation right out. Instead, "reduce" the size of the problem: what are the sums of the 1st row, the 2nd, the 3rd ? 1st -> 1, 2nd -> 2, 3st -> 4, 4th -> 8, 5th -> 16. Do you notice a pattern ? Often times these problems either have an arithmetic solution or a dynamic programming solution. It all begins by noticing the pattern. – Matthieu M. Sep 29 '12 at 18:32

3 Answers3

4

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

arshajii
  • 127,459
  • 24
  • 238
  • 287
3

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.

Rndm
  • 6,710
  • 7
  • 39
  • 58
1

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");
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92