Number pattern - 3

preview_player
Показать описание
Program:
num = int(input())
k = num * (num + 1) // 2
for i in range(num):
for j in range(i+1):
print(k, end = ' ')
k -= 1
print()

Assume num = 5

Iteration 1 (i = 0):
Inner loop (j): range(1) (0 to i+1)
Prints: 15
k = 14

Iteration 2 (i = 1):
Inner loop (j): range(2) (0 to i+1)
Prints: 14 13
k = 12

Iteration 3 (i = 2):
Inner loop (j): range(3) (0 to i+1)
Prints: 12 11 10
k = 9

Iteration 4 (i = 3):
Inner loop (j): range(4) (0 to i+1)
Prints: 9 8 7 6
k = 5

Iteration 5 (i = 4):
Inner loop (j): range(5) (0 to i+1)
Prints: 5 4 3 2 1
k = 0

Output:
15
14 13
12 11 10
9 8 7 6
5 4 3 2 1
Рекомендации по теме