Ulam numbers: Difference between revisions
Added really fast XPL0 example.
(→Version 2: Added a note about 100,000th Ulam number.) |
(Added really fast XPL0 example.) |
||
Line 497:
=={{header|XPL0}}==
Seeing "set" in the Go version and "sieve" in Phix, etc. lit a little
light bulb. This version exploits those ideas and finds the 100,000th
<lang XPL0>func Ulam(N); \Return Nth Ulam number▼
Ulam number in 24.7 seconds on a Pi4.
▲<lang XPL0>func Ulam(N); \Return Nth Ulam number
int N;
def Max = 1_352_000; \enough for 100_000th Ulam number
int List, Size, L, H, Query, Sum, Cnt;▼
[if N < 3 then return N;▼
char Sums(Max*2); \array: 0, 1, or more ways to sum Ulams
for I:= 0 to Max*2 do Sums(I):= 0;
List(0):= 1; List(1):= 2;
repeat Query:= List(Size-1)+1; \possible next Ulam no.
loop [Cnt:= 0;▼
loop [if Sums(Query) = 1
[for
[Sum:=
[Cnt:= Cnt+1;▼
▲ ];
▲ ];
];▼
];
];
Query:= Query+1; \possible next Ulam no.
until Size >= N;
return Query;
Line 536 ⟶ 535:
IntOut(0, Ulam(N)); CrLf(0);
N:= N*10;
until N >
]</lang>
Line 545 ⟶ 544:
The 1000th Ulam number is 12294
The 10000th Ulam number is 132788
The 100000th Ulam number is 1351223
</pre>
|