Ulam numbers: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 20:
{{trans|Python}}
<
I n <= 2
R n
Line 45:
L(p) 5
V n = 10 ^ p
print(‘The #.#. Ulam number is #.’.format(n, I n != 1 {‘th’} E ‘st’, ulam(n)))</
{{out}}
Line 58:
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<
DEFINE MAX="1000"
INT ARRAY ulam(MAX)
Line 85:
n==+1
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Ulam_numbers.png Screenshot from Atari 8-bit computer]
Line 93:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f ULAM_NUMBERS.AWK
BEGIN {
Line 116:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 128:
=={{header|C}}==
{{trans|Phix}}
<
#include <stdlib.h>
#include <string.h>
Line 198:
printf("Elapsed time: %.3f seconds\n", (finish - start + 0.0)/CLOCKS_PER_SEC);
return 0;
}</
{{out}}
Line 213:
=={{header|C++}}==
{{trans|Phix}}
<
#include <chrono>
#include <iostream>
Line 241:
std::chrono::duration<double> duration(end - start);
std::cout << "Elapsed time: " << duration.count() << " seconds\n";
}</
{{out}}
Line 255:
=={{header|FreeBASIC}}==
<
ulam(1) = 1 : ulam(2) = 2
Line 289:
print 10^i, get_ulam(10^i, ulam())
next i
</syntaxhighlight>
{{out}}
Line 300:
===Version 1===
{{trans|Wren}}
<
import "fmt"
Line 335:
fmt.Println("The", n, "\bth Ulam number is", ulam(n))
}
}</
{{out}}
Line 350:
Although not shown here, the 100,000th Ulam number (1,351,223) is computed in about 13.5 seconds.
<
import (
Line 409:
}
fmt.Println("\nTook", time.Since(start))
}</
{{out}}
Line 427:
As mentioned in the Wren version 3 example, you need to know how much memory to allocate in advance.
<
import (
Line 490:
}
fmt.Println("\nTook", time.Since(start))
}</
{{out}}
Line 506:
===Lazy List===
<
import Data.List
Line 535:
isSingleton as
| length as == 1 = True
| otherwise = False</
=={{header|Java}}==
{{trans|Phix}}
<
public static void main(String[] args) {
long start = System.currentTimeMillis();
Line 582:
return newArray;
}
}</
{{out}}
Line 597:
=={{header|jq}}==
'''Adaptation of [[#awk|awk]] solution'''
<
# Output: an array of Ulam numbers
def ulams:
Line 617:
select(.nulams >= $target) | .ulam, break $done);
def nth_ulam: ulams[.-1];</
'''Illustration'''
<
"",
([5, 10, 100] as $in
Line 626:
| $in[]
| "\(.) => \($u[. - 1])" )
</syntaxhighlight>
{{out}}
<pre>
Line 638:
=={{header|Julia}}==
{{trans|Wren}}
<
ulams = [1, 2]
memoized = Set([1, 2])
Line 664:
@time println("The ", n, "th Ulam number is: ", nthUlam(n))
end
</
<pre>
The 10th Ulam number is: 18
Line 678:
=={{header|Lua}}==
Implemented from scratch, but algorithmically equivalent to other solutions where a running count of number-of-ways-to-reach-sum is maintained in order to sieve candidate values.
<
local ulams, nways, i = { 1,2 }, { 0,0,1 }, 3
repeat
Line 696:
local s, u, e = os.clock(), ulam(n), os.clock()
print(string.format("%dth is %d (%f seconds elapsed)", n, u, e-s))
end</
{{out}}
Times are Lua 5.4 on i7-2600@3.4GHz
Line 711:
It has been compiled with option <code>-d:release</code> which means that all runtime checks are done but debugging data is limited.
<
func ulam(n: Positive): int =
Line 737:
echo &"The {n}{suffix} Ulam number is {ulam(n)}."
n *= 10
echo &"\nTook {cpuTime() - t0:.3f} s."</
{{out}}
Line 755:
It has been compiled with the same option as the other version.
<
func ulam(n: Positive): int =
Line 786:
echo &"The {n}th Ulam number is {ulam(n)}."
n *= 10
echo &"\nTook {cpuTime() - t0:.3f} s."</
{{out}}
Line 801:
=={{header|Pascal}}==
{{works with|Free Pascal}} like GO,PHIX who was first
<
{$IFDEF FPC}
{$MODE DELPHI}
Line 897:
setlength(Ulams,0);
end.
</
<pre> Ulam(1) 1
Ulam(10) 18
Line 918:
=={{header|Perl}}==
{{trans|Julia}}
<
use warnings;
use feature <say state>;
Line 952:
}
printf "The %dth Ulam number is: %d\n", 10**$_, ulam(10**$_) for 1..4;</
{{out}}
<pre>The 10th Ulam number is: 18
Line 961:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ulam</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 989:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 1,012:
=={{header|Python}}==
{{trans|XPL0}}
<
def ulam(n):
Line 1,042:
print("\nElapsed time:", time.time() - t0)
</syntaxhighlight>
{{out}}
Line 1,063:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
sub next-ulam {
Line 1,076:
for 1 .. 4 {
say "The {10**$_}th Ulam number is: ", @ulams[10**$_ - 1]
}</
{{out}}
<pre>The 10th Ulam number is: 18
Line 1,087:
This REXX version has several speed improvements.
<
parse arg $ /*obtain optional argument from the CL.*/
if $='' | $="," then $= 10 100 1000 10000 /*Not specified? Then use the defaults.*/
Line 1,117:
z= z + 1 /*bump next possible term. */
end /*until*/
return @.#</
{{out|output|text= when using the default input of: <tt> 10 100 1000 10000 </tt>}}
<pre>
Line 1,131:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,168:
ok
next
</syntaxhighlight>
Output:
<pre>
Line 1,179:
=={{header|Rust}}==
{{trans|Phix}}
<
let mut ulams = vec![1, 2];
let mut sieve = vec![1, 1];
Line 1,208:
}
println!("Elapsed time: {:.2?}", start.elapsed());
}</
{{out}}
Line 1,223:
=={{header|Sidef}}==
{{trans|Perl}}
<
static u = Set(1,2)
Line 1,253:
for k in (1..3) {
say "The 10^#{k}-th Ulam number is: #{ulam(10**k)}"
}</
{{out}}
<pre>
Line 1,264:
===Version 1===
{{trans|Go}}
<
fn ulam(n int) int {
mut ulams := [1, 2]
Line 1,298:
}
println("\nTook ${time.since(start)}")
}</
{{out}}
Line 1,313:
{{trans|Go}}
The following version, which builds up a sieve as it goes along, is (astonishingly) about 40 times faster!
<
fn ulam(n int) int {
mut ulams := [1, 2]
Line 1,366:
}
println("\nTook ${time.since(start)}")
}</
{{out}}
Line 1,384:
As mentioned in the Wren version 3 example, you need to know how much memory to allocate in advance.
<
fn ulam(n int) int {
if n <= 2 {
Line 1,445:
}
println("\nTook ${time.since(start)}")
}</
{{out}}
Line 1,462:
===Version 1===
{{libheader|Wren-set}}
<
var ulam = Fn.new() { |n|
Line 1,491:
System.print("The %(n)th Ulam number is %(ulam.call(n))")
if (n == 10000) break
}</
{{out}}
Line 1,506:
{{libheader|Wren-fmt}}
The above version is reasonably efficient and runs in about 21.6 seconds on my machine (Intel Core i7-8565U). The following version, which builds up a sieve as it goes along, is more than 3 times faster.
<
import "/fmt" for Fmt
Line 1,531:
Fmt.print("The $,r Ulam number is $,d", n, ulam.call(n))
}
System.print("\nTook %(System.clock - start) seconds.")</
{{out}}
Line 1,549:
The only downside with this version is that you need to know how much memory to allocate in advance.
<
var ulam = Fn.new { |n|
Line 1,588:
if (n > 100000) break
}
System.print("\nTook %(System.clock - start) seconds.")</
{{out}}
Line 1,606:
Ulam number in 24.7 seconds on a Pi4.
<
int N;
def Max = 1_352_000; \enough for 100_000th Ulam number
Line 1,641:
N:= N*10;
until N > 100_000;
]</
{{out}}
|