The Ulam Sequence

HardAcc. 73.1%
+45 XP 25

Unique Additions

The Ulam Sequence ($U_n$) starts with $U_1=1, U_2=2$. Every subsequent number is the smallest integer that can be represented as the sum of exactly two distinct earlier terms in exactly one way. 1, 2, 3 (1+2), 4 (1+3), 6 (2+4)... (5 is skipped because it's 1+4 AND 2+3).

The Assignment

Your function receives a parameter count.

  1. Maintain a list (array) of sequence members.
  2. Search for the next smallest number that fits the "single way sum" rule using nested loops over the current members.
  3. Print exactly count numbers.

01EXAMPLE 1

Inputcount=5
Output1 2 3 4 6

Explanation: 5 is excluded (two ways to sum).

Constraints

  • Use nested loops for the sum check.
  • Print each result on a new line.
MathLoops
JavaScript
Loading...
2 Hidden

Input Arguments

count5

Expected Output

1
2
3
4
6

Click RUN to test your solution