Ulam numbers: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 20:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F ulam(n)
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)))</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight Actionlang="action!">PROC Main()
DEFINE MAX="1000"
INT ARRAY ulam(MAX)
Line 85:
n==+1
OD
RETURN</langsyntaxhighlight>
{{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">
<lang AWK>
# syntax: GAWK -f ULAM_NUMBERS.AWK
BEGIN {
Line 116:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 128:
=={{header|C}}==
{{trans|Phix}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 198:
printf("Elapsed time: %.3f seconds\n", (finish - start + 0.0)/CLOCKS_PER_SEC);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 213:
=={{header|C++}}==
{{trans|Phix}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <chrono>
#include <iostream>
Line 241:
std::chrono::duration<double> duration(end - start);
std::cout << "Elapsed time: " << duration.count() << " seconds\n";
}</langsyntaxhighlight>
 
{{out}}
Line 255:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">redim as uinteger ulam(1 to 2)
ulam(1) = 1 : ulam(2) = 2
 
Line 289:
print 10^i, get_ulam(10^i, ulam())
next i
</syntaxhighlight>
</lang>
 
{{out}}
Line 300:
===Version 1===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 335:
fmt.Println("The", n, "\bth Ulam number is", ulam(n))
}
}</langsyntaxhighlight>
 
{{out}}
Line 350:
 
Although not shown here, the 100,000th Ulam number (1,351,223) is computed in about 13.5 seconds.
<langsyntaxhighlight lang="go">package main
 
import (
Line 409:
}
fmt.Println("\nTook", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 427:
 
As mentioned in the Wren version 3 example, you need to know how much memory to allocate in advance.
<langsyntaxhighlight lang="go">package main
 
import (
Line 490:
}
fmt.Println("\nTook", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 506:
 
===Lazy List===
<langsyntaxhighlight lang="haskell">
import Data.List
 
Line 535:
isSingleton as
| length as == 1 = True
| otherwise = False</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Phix}}
<langsyntaxhighlight lang="java">public class UlamNumbers {
public static void main(String[] args) {
long start = System.currentTimeMillis();
Line 582:
return newArray;
}
}</langsyntaxhighlight>
 
{{out}}
Line 597:
=={{header|jq}}==
'''Adaptation of [[#awk|awk]] solution'''
<langsyntaxhighlight lang="jq"># Input: the target number of Ulam numbers to generate
# Output: an array of Ulam numbers
def ulams:
Line 617:
select(.nulams >= $target) | .ulam, break $done);
 
def nth_ulam: ulams[.-1];</langsyntaxhighlight>
 
'''Illustration'''
<langsyntaxhighlight lang="jq">(5 | nth_ulam) | "5 => \(.)",
"",
([5, 10, 100] as $in
Line 626:
| $in[]
| "\(.) => \($u[. - 1])" )
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 638:
=={{header|Julia}}==
{{trans|Wren}}
<langsyntaxhighlight lang="julia">function nthUlam(n)
ulams = [1, 2]
memoized = Set([1, 2])
Line 664:
@time println("The ", n, "th Ulam number is: ", nthUlam(n))
end
</langsyntaxhighlight>{{out}}
<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.
<langsyntaxhighlight lang="lua">function ulam(n)
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</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Nimlang="nim">import strformat, times
 
func ulam(n: Positive): int =
Line 737:
echo &"The {n}{suffix} Ulam number is {ulam(n)}."
n *= 10
echo &"\nTook {cpuTime() - t0:.3f} s."</langsyntaxhighlight>
 
{{out}}
Line 755:
It has been compiled with the same option as the other version.
 
<langsyntaxhighlight Nimlang="nim"> import strformat, times
 
func ulam(n: Positive): int =
Line 786:
echo &"The {n}th Ulam number is {ulam(n)}."
n *= 10
echo &"\nTook {cpuTime() - t0:.3f} s."</langsyntaxhighlight>
 
{{out}}
Line 801:
=={{header|Pascal}}==
{{works with|Free Pascal}} like GO,PHIX who was first
<langsyntaxhighlight lang="pascal">program UlamNumbers;
{$IFDEF FPC}
{$MODE DELPHI}
Line 897:
setlength(Ulams,0);
end.
</langsyntaxhighlight>{{out}}
<pre> Ulam(1) 1
Ulam(10) 18
Line 918:
=={{header|Perl}}==
{{trans|Julia}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature <say state>;
Line 952:
}
 
printf "The %dth Ulam number is: %d\n", 10**$_, ulam(10**$_) for 1..4;</langsyntaxhighlight>
{{out}}
<pre>The 10th Ulam number is: 18
Line 961:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,012:
=={{header|Python}}==
{{trans|XPL0}}
<langsyntaxhighlight lang="python">import time
 
def ulam(n):
Line 1,042:
 
print("\nElapsed time:", time.time() - t0)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,063:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>my @ulams = 1, 2, &next-ulam … *;
 
sub next-ulam {
Line 1,076:
for 1 .. 4 {
say "The {10**$_}th Ulam number is: ", @ulams[10**$_ - 1]
}</langsyntaxhighlight>
{{out}}
<pre>The 10th Ulam number is: 18
Line 1,087:
 
This REXX version has several speed improvements.
<langsyntaxhighlight lang="rexx">/*REXX program finds & displays the Nth Ulam number (or any number of specified values).*/
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 @.#</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 10 &nbsp; 100 &nbsp; 1000 &nbsp; 10000 </tt>}}
<pre>
Line 1,131:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,168:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,179:
=={{header|Rust}}==
{{trans|Phix}}
<langsyntaxhighlight lang="rust">fn ulam(n: usize) -> usize {
let mut ulams = vec![1, 2];
let mut sieve = vec![1, 1];
Line 1,208:
}
println!("Elapsed time: {:.2?}", start.elapsed());
}</langsyntaxhighlight>
 
{{out}}
Line 1,223:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func ulam(n) {
 
static u = Set(1,2)
Line 1,253:
for k in (1..3) {
say "The 10^#{k}-th Ulam number is: #{ulam(10**k)}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,264:
===Version 1===
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import time
fn ulam(n int) int {
mut ulams := [1, 2]
Line 1,298:
}
println("\nTook ${time.since(start)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,313:
{{trans|Go}}
The following version, which builds up a sieve as it goes along, is (astonishingly) about 40 times faster!
<langsyntaxhighlight lang="go">import time
fn ulam(n int) int {
mut ulams := [1, 2]
Line 1,366:
}
println("\nTook ${time.since(start)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,384:
 
As mentioned in the Wren version 3 example, you need to know how much memory to allocate in advance.
<langsyntaxhighlight lang="vlang">import time
fn ulam(n int) int {
if n <= 2 {
Line 1,445:
}
println("\nTook ${time.since(start)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,462:
===Version 1===
{{libheader|Wren-set}}
<langsyntaxhighlight lang="ecmascript">import "/set" for Set
 
var ulam = Fn.new() { |n|
Line 1,491:
System.print("The %(n)th Ulam number is %(ulam.call(n))")
if (n == 10000) break
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="ecmascript">import "/seq" for Lst
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.")</langsyntaxhighlight>
 
{{out}}
Line 1,549:
 
The only downside with this version is that you need to know how much memory to allocate in advance.
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var ulam = Fn.new { |n|
Line 1,588:
if (n > 100000) break
}
System.print("\nTook %(System.clock - start) seconds.")</langsyntaxhighlight>
 
{{out}}
Line 1,606:
Ulam number in 24.7 seconds on a Pi4.
 
<langsyntaxhighlight XPL0lang="xpl0">func Ulam(N); \Return Nth Ulam number
int N;
def Max = 1_352_000; \enough for 100_000th Ulam number
Line 1,641:
N:= N*10;
until N > 100_000;
]</langsyntaxhighlight>
 
{{out}}
10,333

edits