Program to print prime number pyramid



I think you've already figured it out by looking at the above pattern that the numbers are all prime numbers. It takes number of lines \((n)\) as input and prints exactly \(n(n+1)/2\) prime numbers. For example, if \(n=4\) then we have \(4(4+1)/2 = 10\) prime numbers in the pattern. Each line in the pattern has prime numbers exactly equal to the line number. For example, line 4 has 4 prime numbers, similarly, line 7 has 7 prime numbers.
\[\therefore 1 + 2 + 3 + 4 +...+(n-1)+ n\\
= n(n+1)/2\\
or, \sum_{i=1}^{n} i = n(n+1)/2\]
This follows the triangular number sequence.

My solution:

References:




Comments