Ulam numbers: Difference between revisions

Added Sidef
(Added Sidef)
Line 1,138:
Ulam(100000) = 1351223
Elapsed time: 10.68s
</pre>
 
=={{header|Sidef}}==
{{trans|Perl}}
<lang ruby>func ulam(n) {
 
static u = Set(1,2)
static ulams = [0, 1, 2]
 
return ulams[n] if (ulams.end >= n)
 
++n
 
for(var i = 3; true; ++i) {
var count = 0
 
ulams.each {|v|
if (u.has(i - v) && (v != i-v)) {
break if (count++ > 2)
}
}
 
if (count == 2) {
ulams << i
u << i
break if (ulams.len == n)
}
}
 
ulams.tail
}
 
for k in (1..3) {
say "The 10^#{k}-th Ulam number is: #{ulam(10**k)}"
}</lang>
{{out}}
<pre>
The 10^1-th Ulam number is: 18
The 10^2-th Ulam number is: 690
The 10^3-th Ulam number is: 12294
</pre>
 
2,747

edits