DEV Community

Иван
Иван

Posted on

Triangle of even numbers

I invite you to consider the issue.
Consider a number series consisting of even numbers.
Let's decompose sequentially even numbers in a triangle, starting from 0 at the top and on each new line we will add one element.

  1. 0
  2. 2 4
  3. 6 8 10
  4. 12 14 16 18
  5. 20 22 24 26 28 n. ... n+1. ...... And so on ad infinitum

I think the layout is clear.
The question is as follows.
It is required to find the sum of the specified string and all elements of .

For instance:
Given: the number X is the row number
Find:
1) all elements from this row
2) the sum of these elements

Bonus:
Given: the number A is a random number from 0 to infinity
Find:
1) check if the number is even
2) if the given number is even then find
on which line this number is located

Here is such a problem for warming up the brain.

I would be glad to hear your opinion, and it is interesting to see interesting solutions.

Sincerely yours @vova2kms

Top comments (0)