Magic triangles

We define a magic triangle of size n as a triangle with n rows, the
first one with n elements, the second one with n − 1 elements, …, and
the last one with one element, such that:

- All numbers between 1 and n(n + 1)/2 appear once.

- Each number that is not in the first row is equal to the absolute
  value of the difference of the two values immediately above it.

Write a program that, given an n, prints all the magic triangles of size
n, or indicates that there is none.

Input

Input consists of a natural number n > 0.

Output

Print all the magic triangles of size n. Note that the numbers are
always written with two digits (if needed, adding a zero to the left),
and that they are separated with two spaces. Print an empty line after
each triangle.

Ignore the triangles that are symmetrical of any other lexicographically
smaller. If there is no possible triangle for the given n, indicate it
following the format of the example.

Observation

It can be proved that there is no solution for n ≥ 6, but do not use
this fact in your program.

Information about the checker

You can print the solutions to this exercise in any order.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T10:14:30.902Z

© Jutge.org, 2006–2026.
https://jutge.org
