Klarner-Rado sequence: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: (zero - indexed -> one - indexed for Kth 10Kth))
(lang -> syntaxhighlight)
Line 33: Line 33:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Generates the sequence in order. Note that to run this with ALGOL 68G under Windows (and probably Linux), a large heap size must be specified on the command line, e.g.: <code>-heap 1024M</code>.
Generates the sequence in order. Note that to run this with ALGOL 68G under Windows (and probably Linux), a large heap size must be specified on the command line, e.g.: <code>-heap 1024M</code>.
<lang algol68>BEGIN # find elements of the Klarner-Rado sequence #
<syntaxhighlight lang=algol68>BEGIN # find elements of the Klarner-Rado sequence #
# - if n is an element, so are 2n + 1 and 3n + 1 #
# - if n is an element, so are 2n + 1 and 3n + 1 #
INT max element = 100 000 000;
INT max element = 100 000 000;
Line 75: Line 75:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 92: Line 92:
One way to test numbers for membership of the sequence is to feed them to a recursive handler which determines whether or not there's a Klarner-Rado route from them down to 0. It makes finding the elements in order simple, but takes about five and a half minutes to get to the millionth one.
One way to test numbers for membership of the sequence is to feed them to a recursive handler which determines whether or not there's a Klarner-Rado route from them down to 0. It makes finding the elements in order simple, but takes about five and a half minutes to get to the millionth one.


<lang applescript>-- Is n in the Klarner-Rado sequence?
<syntaxhighlight lang=applescript>-- Is n in the Klarner-Rado sequence?
-- Fully recursive:
-- Fully recursive:
(* on isKlarnerRado(n)
(* on isKlarnerRado(n)
Line 150: Line 150:
end task
end task


task()</lang>
task()</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>"First 100 elements:
<syntaxhighlight lang=applescript>"First 100 elements:
1 3 4 7 9 10 13 15 19 21 22 27 28 31 39 40 43 45 46 55
1 3 4 7 9 10 13 15 19 21 22 27 28 31 39 40 43 45 46 55
57 58 63 64 67 79 81 82 85 87 91 93 94 111 115 117 118 121 127 129
57 58 63 64 67 79 81 82 85 87 91 93 94 111 115 117 118 121 127 129
Line 162: Line 162:
10,000th element: 157653
10,000th element: 157653
100,000th element: 2911581
100,000th element: 2911581
1,000,000th element: 54381285"</lang>
1,000,000th element: 54381285"</syntaxhighlight>


Another way is to produce a list with Klarner-Rado elements in the slots indexed by those numbers and 'missing values' in the other slots. If a number being tested is exactly divisible by 2 or by 3, and the slot whose index is the result of the division contains a number instead of 'missing value', the tested number plus 1 is a Klarner-Rado element and should go into the slot it indexes. The list will contain vastly more 'missing values' than Klarner-Rado elements and it — or portions of it — ideally need to exist before the sequence elements are inserted. So while an overabundance and sorting of sequence elements isn't needed, an overabundance of 'missing values' is! The script below carries out the task in about 75 seconds on my current machine and produces the same output as above.
Another way is to produce a list with Klarner-Rado elements in the slots indexed by those numbers and 'missing values' in the other slots. If a number being tested is exactly divisible by 2 or by 3, and the slot whose index is the result of the division contains a number instead of 'missing value', the tested number plus 1 is a Klarner-Rado element and should go into the slot it indexes. The list will contain vastly more 'missing values' than Klarner-Rado elements and it — or portions of it — ideally need to exist before the sequence elements are inserted. So while an overabundance and sorting of sequence elements isn't needed, an overabundance of 'missing values' is! The script below carries out the task in about 75 seconds on my current machine and produces the same output as above.


<lang applescript>on KlarnerRadoSequence(target)
<syntaxhighlight lang=applescript>on KlarnerRadoSequence(target)
-- To find a million KR numbers with this method nominally needs a list with at least 54,381,285
-- To find a million KR numbers with this method nominally needs a list with at least 54,381,285
-- slots. But the number isn't known in advance, "growing" a list to the required length would
-- slots. But the number isn't known in advance, "growing" a list to the required length would
Line 294: Line 294:
end task
end task


task()</lang>
task()</syntaxhighlight>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang=fsharp>
// Klarner-Rado sequence. Nigel Galloway: August 19th., 20
// Klarner-Rado sequence. Nigel Galloway: August 19th., 20
let kr()=Seq.unfold(fun(n,g)->Some(g|>Seq.filter((>)n)|>Seq.sort,(n*2L+1L,seq[for n in g do yield n; yield n*2L+1L; yield n*3L+1L]|>Seq.filter((<=)n)|>Seq.distinct)))(3L,seq[1L])|>Seq.concat
let kr()=Seq.unfold(fun(n,g)->Some(g|>Seq.filter((>)n)|>Seq.sort,(n*2L+1L,seq[for n in g do yield n; yield n*2L+1L; yield n*3L+1L]|>Seq.filter((<=)n)|>Seq.distinct)))(3L,seq[1L])|>Seq.concat
let n=kr()|>Seq.take 1000000|>Array.ofSeq in n|>Array.take 100|>Array.iter(printf "%d "); printfn "\nkr[999] is %d\nkr[9999] is %d\nkr[99999] is %d\nkr[999999] is %d" n.[999] n.[9999] n.[99999] n.[999999]
let n=kr()|>Seq.take 1000000|>Array.ofSeq in n|>Array.take 100|>Array.iter(printf "%d "); printfn "\nkr[999] is %d\nkr[9999] is %d\nkr[99999] is %d\nkr[999999] is %d" n.[999] n.[9999] n.[99999] n.[999999]
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 313: Line 313:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Phix}}
{{trans|Phix}}
<lang freebasic>#include "string.bi"
<syntaxhighlight lang=freebasic>#include "string.bi"


Dim As Integer limit = 1e8
Dim As Integer limit = 1e8
Line 342: Line 342:
End If
End If
Next i
Next i
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as Phix entry.</pre>
<pre>Same as Phix entry.</pre>
Line 401: Line 401:


=={{header|J}}==
=={{header|J}}==
Implementation:<lang J>krsieve=: {{
Implementation:<syntaxhighlight lang=J>krsieve=: {{
for_i. i.<.-:#b=. (1+y){.0 1 do.
for_i. i.<.-:#b=. (1+y){.0 1 do.
if. i{b do. b=. 1 ((#~ y&>)1+2 3*i)} b end.
if. i{b do. b=. 1 ((#~ y&>)1+2 3*i)} b end.
end.
end.
I.b
I.b
}}</lang>
}}</syntaxhighlight>


Task examples (including stretch):<lang J> #kr7e7=: krsieve 7e7
Task examples (including stretch):<syntaxhighlight lang=J> #kr7e7=: krsieve 7e7
1215307
1215307
100{.kr7e7
100{.kr7e7
Line 419: Line 419:
2911581
2911581
(1e6-1){kr7e7
(1e6-1){kr7e7
54381285</lang>
54381285</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Formatting
<syntaxhighlight lang=julia>using Formatting


function KlarnerRado(N)
function KlarnerRado(N)
Line 451: Line 451:
foreach(n -> println("The ", format(n, commas=true), "th Klarner-Rado number is ",
foreach(n -> println("The ", format(n, commas=true), "th Klarner-Rado number is ",
format(kr1m[n], commas=true)), [1000, 10000, 100000, 1000000])
format(kr1m[n], commas=true)), [1000, 10000, 100000, 1000000])
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
First 100 Klarner-Rado numbers:
First 100 Klarner-Rado numbers:
Line 466: Line 466:
=== Faster version ===
=== Faster version ===
Probably does get an overabundance, but no sorting. `falses()` is implemented as a bit vector, so huge arrays are not needed. From ALGOL.
Probably does get an overabundance, but no sorting. `falses()` is implemented as a bit vector, so huge arrays are not needed. From ALGOL.
<lang ruby>using Formatting
<syntaxhighlight lang=ruby>using Formatting


function KlamerRado(N)
function KlamerRado(N)
Line 486: Line 486:
foreach(n -> println("The ", format(n, commas=true), "th Klarner-Rado number is ",
foreach(n -> println("The ", format(n, commas=true), "th Klarner-Rado number is ",
format(kr1m[n], commas=true)), [1000, 10000, 100000, 1000000])
format(kr1m[n], commas=true)), [1000, 10000, 100000, 1000000])
</lang>{{out}} same as above version.
</syntaxhighlight>{{out}} same as above version.


=={{header|Phix}}==
=={{header|Phix}}==
{{trans|ALGOL_68}}
{{trans|ALGOL_68}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">10_000_000</span><span style="color: #0000FF;">:</span><span style="color: #000000;">100_000_000</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #000000;">10_000_000</span><span style="color: #0000FF;">:</span><span style="color: #000000;">100_000_000</span><span style="color: #0000FF;">)</span>
Line 520: Line 520:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 539: Line 539:
<br>
<br>
As PL/M only handles unsigned 8 and 16 bit integers, this only finds the first 1000 elements. This is based on the Algol 68 sample, but as with the VB.NET sample, uses a "bit vector" (here an array of bytes) - as suggested by the Julia sample.
As PL/M only handles unsigned 8 and 16 bit integers, this only finds the first 1000 elements. This is based on the Algol 68 sample, but as with the VB.NET sample, uses a "bit vector" (here an array of bytes) - as suggested by the Julia sample.
<lang pli>100H: /* EIND ELEMENTS OF THE KLARNER-RADO SEQUENCE */
<syntaxhighlight lang=pli>100H: /* EIND ELEMENTS OF THE KLARNER-RADO SEQUENCE */
/* - IF N IS AN ELEMENT, SO ARE 2N+1 AND 3N+!, 1 IS AN ELEMENT */
/* - IF N IS AN ELEMENT, SO ARE 2N+1 AND 3N+!, 1 IS AN ELEMENT */


Line 607: Line 607:
END;
END;


EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 619: Line 619:


=={{header|Python}}==
=={{header|Python}}==
<lang python>def KlarnerRado(N):
<syntaxhighlight lang=python>def KlarnerRado(N):
K = [1]
K = [1]
for i in range(N):
for i in range(N):
Line 648: Line 648:
for n in [1000, 10_000, 100_000]:
for n in [1000, 10_000, 100_000]:
print(f'The {n :,}th Klarner-Rado number is {kr1m[n-1] :,}')
print(f'The {n :,}th Klarner-Rado number is {kr1m[n-1] :,}')
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
First 100 Klarner-Rado sequence numbers:
First 100 Klarner-Rado sequence numbers:
Line 662: Line 662:


=== faster version ===
=== faster version ===
<lang python>from numpy import ndarray
<syntaxhighlight lang=python>from numpy import ndarray


def KlarnerRado(N):
def KlarnerRado(N):
Line 681: Line 681:
for n in [1000, 10_000, 100_000, 1_000_000]:
for n in [1000, 10_000, 100_000, 1_000_000]:
print(f'The {n :,}th Klarner-Rado number is {kr1m[n-1] :,}')
print(f'The {n :,}th Klarner-Rado number is {kr1m[n-1] :,}')
</lang>{{output}} Same as previous version.
</syntaxhighlight>{{output}} Same as previous version.


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>sub Klarner-Rado ($n) {
<syntaxhighlight lang=perl6>sub Klarner-Rado ($n) {
my @klarner-rado = 1;
my @klarner-rado = 1;
my @next = x2, x3;
my @next = x2, x3;
Line 707: Line 707:
put '';
put '';
put (($_».Int».&ordinal».tc »~» ' element: ').List Z~ |(List.new: (Klarner-Rado ($_ »-» 1))».&comma)
put (($_».Int».&ordinal».tc »~» ' element: ').List Z~ |(List.new: (Klarner-Rado ($_ »-» 1))».&comma)
given $(1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6)).join: "\n";</lang>
given $(1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6)).join: "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>First 100 elements of Klarner-Rado sequence:
<pre>First 100 elements of Klarner-Rado sequence:
Line 731: Line 731:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
Based on the ALGOL 68 sample, but using a "bit vector" (array of integers), as suggested by the Julia sample. Simplifies printing "the powers of ten" elements, as in the Phix sample.
Based on the ALGOL 68 sample, but using a "bit vector" (array of integers), as suggested by the Julia sample. Simplifies printing "the powers of ten" elements, as in the Phix sample.
<lang vbnet>Option Strict On
<syntaxhighlight lang=vbnet>Option Strict On
Option Explicit On
Option Explicit On


Line 830: Line 830:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 851: Line 851:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
There's no actual sorting here. The Find class (and its binary search methods) just happen to be in Wren-sort.
There's no actual sorting here. The Find class (and its binary search methods) just happen to be in Wren-sort.
<lang ecmascript>import "./sort" for Find
<syntaxhighlight lang=ecmascript>import "./sort" for Find
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 877: Line 877:
for (limit in limits) {
for (limit in limits) {
Fmt.print("The $,r element: $,d", limit, kr[limit-1])
Fmt.print("The $,r element: $,d", limit, kr[limit-1])
} </lang>
} </syntaxhighlight>


{{out}}
{{out}}
Line 909: Line 909:


Although not shown here, if the size of the BitArray is increased to 1.1 billion and 'max' to 1e7, then the 10 millionth element (1,031,926,801) will eventually be found but takes 4 minutes 50 seconds to do so.
Although not shown here, if the size of the BitArray is increased to 1.1 billion and 'max' to 1e7, then the 10 millionth element (1,031,926,801) will eventually be found but takes 4 minutes 50 seconds to do so.
<lang ecmascript>import "./array" for BitArray
<syntaxhighlight lang=ecmascript>import "./array" for BitArray
import "./fmt" for Fmt
import "./fmt" for Fmt


Line 954: Line 954:
Fmt.print("The $,r element: $,d", limit, pow10[limit])
Fmt.print("The $,r element: $,d", limit, pow10[limit])
limit = 10 * limit
limit = 10 * limit
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}