Computing an entire line in a pascal triangle?

I am trying to calculate the total of the 1500th row in a pascal triangle in C ++.

I tried over 6 different pieces of code from around the web.

Some of them crashed to the 10th line, some went crazy, etc.

How can I calculate the total number of numbers in the 1500th row in a Pascal triangle.

I think that there should be a formula for finding a line without iterating over each line, because iterations cause the program to crash.

+4
source share
3 answers

The sum of the numbers on any line is 2 ^ n , where n is the line (starting at 0). Therefore, in your case it will be just 1 << 1499 .

enter image description here

Your answer is as follows:

17537331055217019373813793980140428996762007940165414412037899012395481925281661101828540443292484630826575203397718758699647274470734979877085519459002350423944978242664548632243401355791731473268341092170069314725677729132473171262691809694657480322332526275875721167754624586680565177898054854942790337156977105108828923716313380366502376637658596066837351781686391648520996613526331666834254976000087526677764529440217091269193357761841856604274688

+4
source

Search google from wiki:

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

Therefore, you need to calculate 2 ^ 1500 instead of trying to iterate over all the rows.

+3
source

The sum of the rows of the pascal triangle is 1<<n , where n is the row number (based on zero). The final value is 1<<1499 .

Thus, your program disables the display of a 1500-bit integer, which should be the main problem. to create a binary output use

 printf("1"); for (int i=1;i<1500;i++) printf("0"); printf("\n"); 
+1
source

Source: https://habr.com/ru/post/1436952/


All Articles