Hofstadter Figure-Figure sequences: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Kotlin}}: Tweak irregular header markup) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 32: | Line 32: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V cR = [1] |
||
V cS = [2] |
V cS = [2] |
||
Line 64: | Line 64: | ||
print(‘All Integers 1..1000 found OK’) |
print(‘All Integers 1..1000 found OK’) |
||
E |
E |
||
print(‘All Integers 1..1000 NOT found only once: ERROR’)</ |
print(‘All Integers 1..1000 NOT found only once: ERROR’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 74: | Line 74: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Specifying a package providing the functions FFR and FFS: |
Specifying a package providing the functions FFR and FFS: |
||
< |
<syntaxhighlight lang="ada">package Hofstadter_Figure_Figure is |
||
function FFR(P: Positive) return Positive; |
function FFR(P: Positive) return Positive; |
||
Line 80: | Line 80: | ||
function FFS(P: Positive) return Positive; |
function FFS(P: Positive) return Positive; |
||
end Hofstadter_Figure_Figure;</ |
end Hofstadter_Figure_Figure;</syntaxhighlight> |
||
The implementation of the package internally uses functions which generate an array of Figures or Spaces: |
The implementation of the package internally uses functions which generate an array of Figures or Spaces: |
||
< |
<syntaxhighlight lang="ada">package body Hofstadter_Figure_Figure is |
||
type Positive_Array is array (Positive range <>) of Positive; |
type Positive_Array is array (Positive range <>) of Positive; |
||
Line 133: | Line 133: | ||
end FFS; |
end FFS; |
||
end Hofstadter_Figure_Figure;</ |
end Hofstadter_Figure_Figure;</syntaxhighlight> |
||
Finally, a test program for the package, solving the task at hand: |
Finally, a test program for the package, solving the task at hand: |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO, Hofstadter_Figure_Figure; |
||
procedure Test_HSS is |
procedure Test_HSS is |
||
Line 175: | Line 175: | ||
exception |
exception |
||
when Program_Error => Ada.Text_IO.Put_Line("Test Failed"); raise; |
when Program_Error => Ada.Text_IO.Put_Line("Test Failed"); raise; |
||
end Test_HSS;</ |
end Test_HSS;</syntaxhighlight> |
||
The output of the test program: |
The output of the test program: |
||
<lang> 1 3 7 12 18 26 35 45 56 69 |
<syntaxhighlight lang="text"> 1 3 7 12 18 26 35 45 56 69 |
||
Test Passed: No overlap between FFR(I) and FFS(J)</ |
Test Passed: No overlap between FFR(I) and FFS(J)</syntaxhighlight> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">:Class HFF |
||
:Field Private Shared RBuf←,1 |
:Field Private Shared RBuf←,1 |
||
∇r←ffr n |
∇r←ffr n |
||
Line 207: | Line 207: | ||
:EndIf |
:EndIf |
||
∇ |
∇ |
||
:EndClass</ |
:EndClass</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 216: | Line 216: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang="autohotkey">R(n){ |
|||
<lang AutoHotkey>R(n){ |
|||
if n=1 |
if n=1 |
||
return 1 |
return 1 |
||
Line 234: | Line 234: | ||
if !(ObjR[A_Index]||ObjS[A_Index]) |
if !(ObjR[A_Index]||ObjS[A_Index]) |
||
return A_index |
return A_index |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">Loop |
||
MsgBox, 262144, , % "R(" A_Index ") = " R(A_Index) "`nS(" A_Index ") = " S(A_Index)</ |
MsgBox, 262144, , % "R(" A_Index ") = " R(A_Index) "`nS(" A_Index ") = " S(A_Index)</syntaxhighlight> |
||
Outputs:<pre>R(1) = 1, 3, 7, 12, 18, 26, 35,... |
Outputs:<pre>R(1) = 1, 3, 7, 12, 18, 26, 35,... |
||
S(1) = 2, 4, 5, 6, 8, 9, 10,...</pre> |
S(1) = 2, 4, 5, 6, 8, 9, 10,...</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk"># Hofstadter Figure-Figure sequences |
||
# |
# |
||
# R(1) = 1; S(1) = 2; |
# R(1) = 1; S(1) = 2; |
||
Line 329: | Line 329: | ||
} |
} |
||
return S[ n ]; |
return S[ n ]; |
||
} # ffs</ |
} # ffs</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 338: | Line 338: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> PRINT "First 10 values of R:" |
||
FOR i% = 1 TO 10 : PRINT ;FNffr(i%) " "; : NEXT : PRINT |
FOR i% = 1 TO 10 : PRINT ;FNffr(i%) " "; : NEXT : PRINT |
||
PRINT "First 10 values of S:" |
PRINT "First 10 values of S:" |
||
Line 390: | Line 390: | ||
IF R% <= 2*N% V%?R% = 1 |
IF R% <= 2*N% V%?R% = 1 |
||
NEXT I% |
NEXT I% |
||
= S%</ |
= S%</syntaxhighlight> |
||
<pre> |
<pre> |
||
First 10 values of R: |
First 10 values of R: |
||
Line 401: | Line 401: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 484: | Line 484: | ||
puts("\nfirst 1000 ok"); |
puts("\nfirst 1000 ok"); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Creates an IEnumerable for R and S and uses those to complete the task |
Creates an IEnumerable for R and S and uses those to complete the task |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 572: | Line 572: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre>1 |
<pre>1 |
||
Line 589: | Line 589: | ||
{{works with|gcc}} |
{{works with|gcc}} |
||
{{works with|C++|11, 14, 17}} |
{{works with|C++|11, 14, 17}} |
||
< |
<syntaxhighlight lang="cpp">#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
#include <set> |
#include <set> |
||
Line 654: | Line 654: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">% ./hofstadter 40 100 2> /dev/null |
||
R(40): |
R(40): |
||
1 3 7 12 18 26 35 45 56 69 83 98 114 131 150 170 191 213 236 260 |
1 3 7 12 18 26 35 45 56 69 83 98 114 131 150 170 191 213 236 260 |
||
Line 666: | Line 666: | ||
49 50 51 52 53 54 55 57 58 59 60 61 62 63 64 65 66 67 68 70 |
49 50 51 52 53 54 55 57 58 59 60 61 62 63 64 65 66 67 68 70 |
||
71 72 73 74 75 76 77 78 79 80 81 82 84 85 86 87 88 89 90 91 |
71 72 73 74 75 76 77 78 79 80 81 82 84 85 86 87 88 89 90 91 |
||
92 93 94 95 96 97 99 100 101 102 103 104 105 106 107 108 109 110 111 112</ |
92 93 94 95 96 97 99 100 101 102 103 104 105 106 107 108 109 110 111 112</syntaxhighlight> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">figfig = cluster is ffr, ffs |
||
rep = null |
rep = null |
||
ai = array[int] |
ai = array[int] |
||
Line 731: | Line 731: | ||
int$unparse(i) || " occurs " || int$unparse(occur[i]) || " times.") |
int$unparse(i) || " occurs " || int$unparse(occur[i]) || " times.") |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>R[1..10] = 1 3 7 12 18 26 35 45 56 69 |
<pre>R[1..10] = 1 3 7 12 18 26 35 45 56 69 |
||
Line 738: | Line 738: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="coffeescript">R = [ null, 1 ] |
||
S = [ null, 2 ] |
S = [ null, 2 ] |
||
Line 763: | Line 763: | ||
if int_array[i - 1] != i |
if int_array[i - 1] != i |
||
throw 'Something\'s wrong!' |
throw 'Something\'s wrong!' |
||
console.log '1000 integer check ok.'</ |
console.log '1000 integer check ok.'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
As JavaScript. |
As JavaScript. |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">;;; equally doable with a list |
||
(flet ((seq (i) (make-array 1 :element-type 'integer |
(flet ((seq (i) (make-array 1 :element-type 'integer |
||
:initial-element i |
:initial-element i |
||
Line 801: | Line 801: | ||
(take #'seq-s 960)) |
(take #'seq-s 960)) |
||
#'<)) |
#'<)) |
||
(princ "Ok")</ |
(princ "Ok")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>First of R: (1 3 7 12 18 26 35 45 56 69) |
<pre>First of R: (1 3 7 12 18 26 35 45 56 69) |
||
Line 807: | Line 807: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
include "strings.coh"; |
include "strings.coh"; |
||
include "malloc.coh"; |
include "malloc.coh"; |
||
Line 949: | Line 949: | ||
if ok != 0 then |
if ok != 0 then |
||
print("All numbers 1 .. 1000 found!\n"); |
print("All numbers 1 .. 1000 found!\n"); |
||
end if;</ |
end if;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 959: | Line 959: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="d">int delegate(in int) nothrow ffr, ffs; |
||
nothrow static this() { |
nothrow static this() { |
||
Line 989: | Line 989: | ||
auto t = iota(1, 41).map!ffr.chain(iota(1, 961).map!ffs); |
auto t = iota(1, 41).map!ffr.chain(iota(1, 961).map!ffs); |
||
t.array.sort().equal(iota(1, 1001)).writeln; |
t.array.sort().equal(iota(1, 1001)).writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 3, 7, 12, 18, 26, 35, 45, 56, 69] |
<pre>[1, 3, 7, 12, 18, 26, 35, 45, 56, 69] |
||
Line 996: | Line 996: | ||
{{trans|Python}} |
{{trans|Python}} |
||
(Same output) |
(Same output) |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.array, std.range, std.algorithm; |
||
struct ffr { |
struct ffr { |
||
Line 1,046: | Line 1,046: | ||
auto t = iota(1, 41).map!ffr.chain(iota(1, 961).map!ffs); |
auto t = iota(1, 41).map!ffr.chain(iota(1, 961).map!ffs); |
||
t.array.sort().equal(iota(1, 1001)).writeln; |
t.array.sort().equal(iota(1, 1001)).writeln; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme">(define (FFR n) |
||
(+ (FFR (1- n)) (FFS (1- n)))) |
(+ (FFR (1- n)) (FFS (1- n)))) |
||
Line 1,060: | Line 1,060: | ||
(remember 'FFR #(0 1)) ;; init cache |
(remember 'FFR #(0 1)) ;; init cache |
||
(remember 'FFS #(0 2)) |
(remember 'FFS #(0 2)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define-macro m-range [a .. b] (range a (1+ b))) |
(define-macro m-range [a .. b] (range a (1+ b))) |
||
Line 1,070: | Line 1,070: | ||
;; checking |
;; checking |
||
(equal? [1 .. 1000] (list-sort < (append (map FFR [1 .. 40]) (map FFS [1 .. 960])))) |
(equal? [1 .. 1000] (list-sort < (append (map FFR [1 .. 40]) (map FFS [1 .. 960])))) |
||
→ #t</ |
→ #t</syntaxhighlight> |
||
=={{header|Euler Math Toolbox}}== |
=={{header|Euler Math Toolbox}}== |
||
<syntaxhighlight lang="euler math toolbox"> |
|||
<lang Euler Math Toolbox> |
|||
>function RSstep (r,s) ... |
>function RSstep (r,s) ... |
||
$ n=cols(r); |
$ n=cols(r); |
||
Line 1,094: | Line 1,094: | ||
>all(sort(r[1:40]|s[1:960])==(1:1000)) |
>all(sort(r[1:40]|s[1:960])==(1:1000)) |
||
1 |
1 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
===The function=== |
===The function=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Populate R and S with values of Hofstadter Figure Figure sequence. Nigel Galloway: August 28th., 2020 |
// Populate R and S with values of Hofstadter Figure Figure sequence. Nigel Galloway: August 28th., 2020 |
||
let fF q=let R,S=Array.zeroCreate<int>q,Array.zeroCreate<int>q |
let fF q=let R,S=Array.zeroCreate<int>q,Array.zeroCreate<int>q |
||
Line 1,107: | Line 1,107: | ||
|i->S.[n]<-i+1; fN (n+1) (g+1) |
|i->S.[n]<-i+1; fN (n+1) (g+1) |
||
fN 1 1 |
fN 1 1 |
||
</syntaxhighlight> |
|||
</lang> |
|||
===The Tasks=== |
===The Tasks=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let ffr,ffs=fF 960 |
let ffr,ffs=fF 960 |
||
ffr|>Seq.take 10|>Seq.iter(printf "%d "); printfn "" |
ffr|>Seq.take 10|>Seq.iter(printf "%d "); printfn "" |
||
let N=Array.concat [|ffs;(Array.take 40 ffr)|] in printfn "Unique values=%d Minimum value=%d Maximum Value=%d" ((Array.distinct N).Length)(Array.min N)(Array.max N) |
let N=Array.concat [|ffs;(Array.take 40 ffr)|] in printfn "Unique values=%d Minimum value=%d Maximum Value=%d" ((Array.distinct N).Length)(Array.min N)(Array.max N) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,122: | Line 1,122: | ||
===Unbounded n?=== |
===Unbounded n?=== |
||
n is bounded in this implementation because it is an signed 32 integer. Within such limit the 10 millionth value will have to be sufficiently unbounded. It can be found in 43 thousandths of sec. |
n is bounded in this implementation because it is an signed 32 integer. Within such limit the 10 millionth value will have to be sufficiently unbounded. It can be found in 43 thousandths of sec. |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let ffr,ffs=fF 10000000 |
let ffr,ffs=fF 10000000 |
||
printfn "%d\n%d (Array.last ffr) (Array.last ffs) |
printfn "%d\n%d (Array.last ffr) (Array.last ffs) |
||
</syntaxhighlight> |
|||
</lang> |
|||
1584961838 |
1584961838 |
||
10004416 |
10004416 |
||
Line 1,131: | Line 1,131: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
We keep lists S and R, and increment them when necessary. |
We keep lists S and R, and increment them when necessary. |
||
< |
<syntaxhighlight lang="factor">SYMBOL: S V{ 2 } S set |
||
SYMBOL: R V{ 1 } R set |
SYMBOL: R V{ 1 } R set |
||
Line 1,151: | Line 1,151: | ||
: ffr ( n -- R(n) ) |
: ffr ( n -- R(n) ) |
||
dup R get length - inc-SR |
dup R get length - inc-SR |
||
1 - R get nth ;</ |
1 - R get nth ;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="factor">( scratchpad ) 10 iota [ 1 + ffr ] map . |
||
{ 1 3 7 12 18 26 35 45 56 69 } |
{ 1 3 7 12 18 26 35 45 56 69 } |
||
( scratchpad ) 40 iota [ 1 + ffr ] map 960 iota [ 1 + ffs ] map append 1000 iota 1 v+n set= . |
( scratchpad ) 40 iota [ 1 + ffr ] map 960 iota [ 1 + ffs ] map append 1000 iota 1 v+n set= . |
||
t</ |
t</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
< |
<syntaxhighlight lang="freebasic">function ffr( n as integer ) as integer |
||
if n = 1 then return 1 |
if n = 1 then return 1 |
||
dim as integer i, j, r=1, s=1, v(1 to 2*n+1) |
dim as integer i, j, r=1, s=1, v(1 to 2*n+1) |
||
Line 1,209: | Line 1,209: | ||
next i |
next i |
||
if failed then print "Oh no!" else print "All integers from 1 to 1000 accounted for"</ |
if failed then print "Oh no!" else print "All integers from 1 to 1000 accounted for"</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
R S |
R S |
||
Line 1,227: | Line 1,227: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,282: | Line 1,282: | ||
} |
} |
||
fmt.Println("task 4: PASS") |
fmt.Println("task 4: PASS") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,298: | Line 1,298: | ||
The following defines two mutually recursive generators without caching results. Each generator will end up dragging a tree of closures behind it, but due to the odd nature of the two series' growth pattern, it's still a heck of a lot faster than the above method when producing either series in sequence. |
The following defines two mutually recursive generators without caching results. Each generator will end up dragging a tree of closures behind it, but due to the odd nature of the two series' growth pattern, it's still a heck of a lot faster than the above method when producing either series in sequence. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,346: | Line 1,346: | ||
} |
} |
||
fmt.Println(sum) |
fmt.Println(sum) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (delete, sort) |
||
-- Functions by Reinhard Zumkeller |
-- Functions by Reinhard Zumkeller |
||
Line 1,372: | Line 1,372: | ||
print $ ffr <$> [1 .. 10] |
print $ ffr <$> [1 .. 10] |
||
let i1000 = sort (fmap ffr [1 .. 40] ++ fmap ffs [1 .. 960]) |
let i1000 = sort (fmap ffr [1 .. 40] ++ fmap ffs [1 .. 960]) |
||
print (i1000 == [1 .. 1000])</ |
print (i1000 == [1 .. 1000])</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>[1,3,7,12,18,26,35,45,56,69] |
<pre>[1,3,7,12,18,26,35,45,56,69] |
||
Line 1,378: | Line 1,378: | ||
Defining R and S literally: |
Defining R and S literally: |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sort) |
||
r :: [Int] |
r :: [Int] |
||
Line 1,396: | Line 1,396: | ||
print (take 10 s) |
print (take 10 s) |
||
putStr "test 1000: " |
putStr "test 1000: " |
||
print $ [1 .. 1000] == sort (take 40 r ++ take 960 s)</ |
print $ [1 .. 1000] == sort (take 40 r ++ take 960 s)</syntaxhighlight> |
||
output: |
output: |
||
<pre> |
<pre> |
||
Line 1,405: | Line 1,405: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">link printf,ximage |
||
procedure main() |
procedure main() |
||
Line 1,463: | Line 1,463: | ||
} |
} |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 1,484: | Line 1,484: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">R=: 1 1 3 |
||
S=: 0 2 4 |
S=: 0 2 4 |
||
FF=: 3 :0 |
FF=: 3 :0 |
||
Line 1,494: | Line 1,494: | ||
) |
) |
||
ffr=: { 0 {:: FF@(>./@,) |
ffr=: { 0 {:: FF@(>./@,) |
||
ffs=: { 1 {:: FF@(0,>./@,)</ |
ffs=: { 1 {:: FF@(0,>./@,)</syntaxhighlight> |
||
Required examples: |
Required examples: |
||
< |
<syntaxhighlight lang="j"> ffr 1+i.10 |
||
1 3 7 12 18 26 35 45 56 69 |
1 3 7 12 18 26 35 45 56 69 |
||
(1+i.1000) -: /:~ (ffr 1+i.40), ffs 1+i.960 |
(1+i.1000) -: /:~ (ffr 1+i.40), ffs 1+i.960 |
||
1</ |
1</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 1,507: | Line 1,507: | ||
'''Code:''' |
'''Code:''' |
||
< |
<syntaxhighlight lang="java">import java.util.*; |
||
class Hofstadter |
class Hofstadter |
||
Line 1,562: | Line 1,562: | ||
System.out.println("Done"); |
System.out.println("Done"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
Line 1,571: | Line 1,571: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="javascript">var R = [null, 1]; |
||
var S = [null, 2]; |
var S = [null, 2]; |
||
Line 1,617: | Line 1,617: | ||
throw "Something's wrong!" |
throw "Something's wrong!" |
||
} else { console.log("1000 integer check ok."); } |
} else { console.log("1000 integer check ok."); } |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>R(1) = 1 |
<pre>R(1) = 1 |
||
Line 1,641: | Line 1,641: | ||
to accomplish the specific tasks. |
to accomplish the specific tasks. |
||
< |
<syntaxhighlight lang="jq">def init: {r: [0, 1], s: [0, 2] }; |
||
# input: {r,s} |
# input: {r,s} |
||
Line 1,671: | Line 1,671: | ||
| (.r[1:41] + .s[1:961] | sort) == [range(1;1001)] ) ; |
| (.r[1:41] + .s[1:961] | sort) == [range(1;1001)] ) ; |
||
task1(10), task2</ |
task1(10), task2</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,685: | Line 1,685: | ||
'''Functions''' |
'''Functions''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
type FigureFigure{T<:Integer} |
type FigureFigure{T<:Integer} |
||
r::Array{T,1} |
r::Array{T,1} |
||
Line 1,747: | Line 1,747: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Main''' |
'''Main''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
NR = 40 |
NR = 40 |
||
NS = 960 |
NS = 960 |
||
Line 1,791: | Line 1,791: | ||
end |
end |
||
println("cover the entire interval.") |
println("cover the entire interval.") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,804: | Line 1,804: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Translated from Java. |
Translated from Java. |
||
< |
<syntaxhighlight lang="scala">fun ffr(n: Int) = get(n, 0)[n - 1] |
||
fun ffs(n: Int) = get(0, n)[n - 1] |
fun ffs(n: Int) = get(0, n)[n - 1] |
||
Line 1,838: | Line 1,838: | ||
indices.forEach { println("Integer $it either in both or neither set") } |
indices.forEach { println("Integer $it either in both or neither set") } |
||
println("Done") |
println("Done") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Line 1,849: | Line 1,849: | ||
2. No maximum value for n should be assumed. |
2. No maximum value for n should be assumed. |
||
<syntaxhighlight lang="mathematica"> |
|||
<lang Mathematica> |
|||
ffr[j_] := Module[{R = {1}, S = 2, k = 1}, |
ffr[j_] := Module[{R = {1}, S = 2, k = 1}, |
||
Do[While[Position[R, S] != {}, S++]; k = k + S; S++; |
Do[While[Position[R, S] != {}, S++]; k = k + S; S++; |
||
Line 1,855: | Line 1,855: | ||
ffs[j_] := Differences[ffr[j + 1]] |
ffs[j_] := Differences[ffr[j + 1]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
3. Calculate and show that the first ten values of R are: 1, 3, 7, 12, 18, 26, 35, 45, 56, and 69 |
3. Calculate and show that the first ten values of R are: 1, 3, 7, 12, 18, 26, 35, 45, 56, and 69 |
||
<syntaxhighlight lang="mathematica"> |
|||
<lang Mathematica> |
|||
ffr[10] |
ffr[10] |
||
(* out *) |
(* out *) |
||
{1, 3, 7, 12, 18, 26, 35, 45, 56, 69} |
{1, 3, 7, 12, 18, 26, 35, 45, 56, 69} |
||
</syntaxhighlight> |
|||
</lang> |
|||
4. Calculate and show that the first 40 values of ffr plus the first 960 values of ffs include all the integers from 1 to 1000 exactly once. |
4. Calculate and show that the first 40 values of ffr plus the first 960 values of ffs include all the integers from 1 to 1000 exactly once. |
||
<syntaxhighlight lang="mathematica"> |
|||
<lang Mathematica> |
|||
t = Sort[Join[ffr[40], ffs[960]]]; |
t = Sort[Join[ffr[40], ffs[960]]]; |
||
Line 1,875: | Line 1,875: | ||
(* out *) |
(* out *) |
||
True |
True |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
Line 1,882: | Line 1,882: | ||
2. No maximum value for n should be assumed. |
2. No maximum value for n should be assumed. |
||
< |
<syntaxhighlight lang="matlab"> function [R,S] = ffr_ffs(N) |
||
t = [1,0]; |
t = [1,0]; |
||
T = 1; |
T = 1; |
||
Line 1,905: | Line 1,905: | ||
printf('Sequence S:\n'); disp(S); |
printf('Sequence S:\n'); disp(S); |
||
end; |
end; |
||
end; </ |
end; </syntaxhighlight> |
||
3. Calculate and show that the first ten values of R are: 1, 3, 7, 12, 18, 26, 35, 45, 56, and 69 |
3. Calculate and show that the first ten values of R are: 1, 3, 7, 12, 18, 26, 35, 45, 56, and 69 |
||
Line 1,926: | Line 1,926: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">var cr = @[1] |
||
var cs = @[2] |
var cs = @[2] |
||
Line 1,958: | Line 1,958: | ||
if all: echo "All Integers 1..1000 found OK" |
if all: echo "All Integers 1..1000 found OK" |
||
else: echo "All Integers 1..1000 NOT found only once: ERROR"</ |
else: echo "All Integers 1..1000 NOT found only once: ERROR"</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>/home/deen/git/nim-unsorted/hofstadter |
<pre>/home/deen/git/nim-unsorted/hofstadter |
||
Line 1,965: | Line 1,965: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">tvar: R |
||
ListBuffer new 1 over add R put |
ListBuffer new 1 over add R put |
||
Line 1,985: | Line 1,985: | ||
: ffs(n) |
: ffs(n) |
||
while ( S at size n < ) [ buildnext ] |
while ( S at size n < ) [ buildnext ] |
||
n S at at ;</ |
n S at at ;</syntaxhighlight> |
||
Output : |
Output : |
||
Line 2,002: | Line 2,002: | ||
Then we go through the first 1000 outputs, mark those which are seen, then check if all values in the range one through one thousand were seen. |
Then we go through the first 1000 outputs, mark those which are seen, then check if all values in the range one through one thousand were seen. |
||
< |
<syntaxhighlight lang="perl">#!perl |
||
use strict; |
use strict; |
||
use warnings; |
use warnings; |
||
Line 2,037: | Line 2,037: | ||
print "These were duplicated: @dupped\n"; |
print "These were duplicated: @dupped\n"; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Initialising such that length(S)>length(F) simplified things significantly. |
Initialising such that length(S)>length(F) simplified things significantly. |
||
<!--< |
<!--<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: #004080;">sequence</span> <span style="color: #000000;">F</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">F</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span> |
||
Line 2,078: | Line 2,078: | ||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"some error!\n"</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"some error!\n"</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,086: | Line 2,086: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(setq *RNext 2) |
||
(de ffr (N) |
(de ffr (N) |
||
Line 2,102: | Line 2,102: | ||
(inc 'S) |
(inc 'S) |
||
(inc '*RNext) ) |
(inc '*RNext) ) |
||
S ) ) ) )</ |
S ) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp">: (mapcar ffr (range 1 10)) |
||
-> (1 3 7 12 18 26 35 45 56 69) |
-> (1 3 7 12 18 26 35 45 56 69) |
||
Line 2,110: | Line 2,110: | ||
(range 1 1000) |
(range 1 1000) |
||
(sort (conc (mapcar ffr (range 1 40)) (mapcar ffs (range 1 960)))) ) |
(sort (conc (mapcar ffr (range 1 40)) (mapcar ffs (range 1 960)))) ) |
||
-> T</ |
-> T</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli">ffr: procedure (n) returns (fixed binary(31)); |
||
declare n fixed binary (31); |
declare n fixed binary (31); |
||
declare v(2*n+1) bit(1); |
declare v(2*n+1) bit(1); |
||
Line 2,135: | Line 2,135: | ||
end; |
end; |
||
return (r); |
return (r); |
||
end ffr;</ |
end ffr;</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,143: | Line 2,143: | ||
602 640 679 719 760 802 845 889 935 982 |
602 640 679 719 760 802 845 889 935 982 |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="pli">ffs: procedure (n) returns (fixed binary (31)); |
||
declare n fixed binary (31); |
declare n fixed binary (31); |
||
declare v(2*n+1) bit(1); |
declare v(2*n+1) bit(1); |
||
Line 2,165: | Line 2,165: | ||
end; |
end; |
||
return (s); |
return (s); |
||
end ffs;</ |
end ffs;</syntaxhighlight> |
||
Output of first 960 values: |
Output of first 960 values: |
||
<pre> |
<pre> |
||
Line 2,175: | Line 2,175: | ||
</pre> |
</pre> |
||
Verification using the above procedures: |
Verification using the above procedures: |
||
< |
<syntaxhighlight lang="pli"> |
||
Dcl t(1000) Bit(1) Init((1000)(1)'0'b); |
Dcl t(1000) Bit(1) Init((1000)(1)'0'b); |
||
put skip list ('Verification that the first 40 FFR numbers and the first'); |
put skip list ('Verification that the first 40 FFR numbers and the first'); |
||
Line 2,190: | Line 2,190: | ||
end; |
end; |
||
if all(t = '1'b) then put skip list ('passed test'); |
if all(t = '1'b) then put skip list ('passed test'); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,202: | Line 2,202: | ||
CHR is a programming language created by '''Professor Thom Frühwirth'''.<br> |
CHR is a programming language created by '''Professor Thom Frühwirth'''.<br> |
||
Works with SWI-Prolog and module '''chr''' written by '''Tom Schrijvers''' and '''Jan Wielemaker''' |
Works with SWI-Prolog and module '''chr''' written by '''Tom Schrijvers''' and '''Jan Wielemaker''' |
||
< |
<syntaxhighlight lang="prolog">:- use_module(library(chr)). |
||
:- chr_constraint ffr/2, ffs/2, hofstadter/1,hofstadter/2. |
:- chr_constraint ffr/2, ffs/2, hofstadter/1,hofstadter/2. |
||
Line 2,228: | Line 2,228: | ||
hofstadter(N), ffr(N1, _R), ffs(N1, _S) ==> N1 < N, N2 is N1 +1 | ffr(N2,_), ffs(N2,_). |
hofstadter(N), ffr(N1, _R), ffs(N1, _S) ==> N1 < N, N2 is N1 +1 | ffr(N2,_), ffs(N2,_). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output for first task : |
Output for first task : |
||
<pre> ?- hofstadter(10), bagof(ffr(X,Y), find_chr_constraint(ffr(X,Y)), L). |
<pre> ?- hofstadter(10), bagof(ffr(X,Y), find_chr_constraint(ffr(X,Y)), L). |
||
Line 2,256: | Line 2,256: | ||
Code for the second task |
Code for the second task |
||
< |
<syntaxhighlight lang="prolog">hofstadter :- |
||
hofstadter(960), |
hofstadter(960), |
||
% fetch the values of ffr |
% fetch the values of ffr |
||
Line 2,270: | Line 2,270: | ||
% to remove all pending constraints |
% to remove all pending constraints |
||
fail. |
fail. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output for second task |
Output for second task |
||
<pre> ?- hofstadter. |
<pre> ?- hofstadter. |
||
Line 2,278: | Line 2,278: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">def ffr(n): |
||
if n < 1 or type(n) != int: raise ValueError("n must be an int >= 1") |
if n < 1 or type(n) != int: raise ValueError("n must be an int >= 1") |
||
try: |
try: |
||
Line 2,323: | Line 2,323: | ||
print("All Integers 1..1000 found OK") |
print("All Integers 1..1000 found OK") |
||
else: |
else: |
||
print("All Integers 1..1000 NOT found only once: ERROR")</ |
print("All Integers 1..1000 NOT found only once: ERROR")</syntaxhighlight> |
||
;Output: |
;Output: |
||
Line 2,330: | Line 2,330: | ||
===Alternative=== |
===Alternative=== |
||
< |
<syntaxhighlight lang="python">cR = [1] |
||
cS = [2] |
cS = [2] |
||
Line 2,358: | Line 2,358: | ||
# the fact that we got here without a key error |
# the fact that we got here without a key error |
||
print("Ok")</ |
print("Ok")</syntaxhighlight>output<syntaxhighlight lang="text">[1, 3, 7, 12, 18, 26, 35, 45, 56, 69] |
||
Ok</ |
Ok</syntaxhighlight> |
||
===Using cyclic iterators=== |
===Using cyclic iterators=== |
||
{{trans|Haskell}} |
{{trans|Haskell}} |
||
Defining R and S as mutually recursive generators. Follows directly from the definition of the R and S sequences. |
Defining R and S as mutually recursive generators. Follows directly from the definition of the R and S sequences. |
||
< |
<syntaxhighlight lang="python">from itertools import islice |
||
def R(): |
def R(): |
||
Line 2,389: | Line 2,389: | ||
# perf test case |
# perf test case |
||
# print sum(lst(R, 10000000))</ |
# print sum(lst(R, 10000000))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>R: [1, 3, 7, 12, 18, 26, 35, 45, 56, 69] |
<pre>R: [1, 3, 7, 12, 18, 26, 35, 45, 56, 69] |
||
Line 2,398: | Line 2,398: | ||
=={{header|R}}== |
=={{header|R}}== |
||
Global variables aren't idiomatic R, but this is otherwise an ideal task for the language. Comments aside, this is easily one of the shortest solutions on this page. This is mostly due to how R treats most things as a vector. For example, rValues starts out as the number 1, but repeatedly has new values appended to it without much ceremony. |
Global variables aren't idiomatic R, but this is otherwise an ideal task for the language. Comments aside, this is easily one of the shortest solutions on this page. This is mostly due to how R treats most things as a vector. For example, rValues starts out as the number 1, but repeatedly has new values appended to it without much ceremony. |
||
< |
<syntaxhighlight lang="rsplus">rValues <- 1 |
||
sValues <- 2 |
sValues <- 2 |
||
ffr <- function(n) |
ffr <- function(n) |
||
Line 2,427: | Line 2,427: | ||
This gives an output of length 960, which clearly cannot contain 1000 different values. |
This gives an output of length 960, which clearly cannot contain 1000 different values. |
||
#Presumably, the task wants us to append rValues[1:40] and sValues[1:960]. |
#Presumably, the task wants us to append rValues[1:40] and sValues[1:960]. |
||
print(table(c(rValues[1:40], sValues[1:960])))</ |
print(table(c(rValues[1:40], sValues[1:960])))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>> print(rValues) |
<pre>> print(rValues) |
||
Line 2,545: | Line 2,545: | ||
We store the values of r and s in hash-tables. The first values are added by hand. The procedure extend-r-s! adds more values. |
We store the values of r and s in hash-tables. The first values are added by hand. The procedure extend-r-s! adds more values. |
||
< |
<syntaxhighlight lang="racket">#lang racket/base |
||
(define r-cache (make-hash '((1 . 1) (2 . 3) (3 . 7)))) |
(define r-cache (make-hash '((1 . 1) (2 . 3) (3 . 7)))) |
||
Line 2,558: | Line 2,558: | ||
(define offset (- s-count last-r)) |
(define offset (- s-count last-r)) |
||
(for ([val (in-range (add1 last-r) new-r)]) |
(for ([val (in-range (add1 last-r) new-r)]) |
||
(hash-set! s-cache (+ val offset) val)))</ |
(hash-set! s-cache (+ val offset) val)))</syntaxhighlight> |
||
The functions ffr and ffs simply retrieve the value from the hash table if it exist, or call extend-r-s until they are long enought. |
The functions ffr and ffs simply retrieve the value from the hash table if it exist, or call extend-r-s until they are long enought. |
||
< |
<syntaxhighlight lang="racket">(define (ffr n) |
||
(hash-ref r-cache n (lambda () (extend-r-s!) (ffr n)))) |
(hash-ref r-cache n (lambda () (extend-r-s!) (ffr n)))) |
||
(define (ffs n) |
(define (ffs n) |
||
(hash-ref s-cache n (lambda () (extend-r-s!) (ffs n))))</ |
(hash-ref s-cache n (lambda () (extend-r-s!) (ffs n))))</syntaxhighlight> |
||
Tests: |
Tests: |
||
< |
<syntaxhighlight lang="racket">(displayln (map ffr (list 1 2 3 4 5 6 7 8 9 10))) |
||
(displayln (map ffs (list 1 2 3 4 5 6 7 8 9 10))) |
(displayln (map ffs (list 1 2 3 4 5 6 7 8 9 10))) |
||
Line 2,581: | Line 2,581: | ||
i)) |
i)) |
||
"Test passed" |
"Test passed" |
||
"Test failed"))</ |
"Test failed"))</syntaxhighlight> |
||
'''Sample Output:''' |
'''Sample Output:''' |
||
Line 2,591: | Line 2,591: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|2018.03}} |
{{works with|Rakudo|2018.03}} |
||
<lang |
<syntaxhighlight lang="raku" line>my %r = 1 => 1; |
||
my %s = 1 => 2; |
my %s = 1 => 2; |
||
Line 2,601: | Line 2,601: | ||
say @ffr[^10]; |
say @ffr[^10]; |
||
say "Rawks!" if 1...1000 eqv sort |@ffr[^40], |@ffs[^960];</ |
say "Rawks!" if 1...1000 eqv sort |@ffr[^40], |@ffs[^960];</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,615: | Line 2,615: | ||
This REXX version is over '''15,000%''' faster than REXX version 2. |
This REXX version is over '''15,000%''' faster than REXX version 2. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program calculates and verifies the Hofstadter Figure─Figure sequences. */ |
||
parse arg x top bot . /*obtain optional arguments from the CL*/ |
parse arg x top bot . /*obtain optional arguments from the CL*/ |
||
if x=='' | x=="," then x= 10 /*Not specified? Then use the default.*/ |
if x=='' | x=="," then x= 10 /*Not specified? Then use the default.*/ |
||
Line 2,657: | Line 2,657: | ||
return s.n /*return S.n value to the invoker. */ |
return s.n /*return S.n value to the invoker. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
ser: errs=errs+1; say '***error***' arg(1); return</ |
ser: errs=errs+1; say '***error***' arg(1); return</syntaxhighlight> |
||
To see the talk section about this REXX program's timings, click here: [http://rosettacode.org/wiki/Talk:Hofstadter_Figure-Figure_sequences#timings_for_the_REXX_solutions timings for the REXX solutions]. <br> |
To see the talk section about this REXX program's timings, click here: [http://rosettacode.org/wiki/Talk:Hofstadter_Figure-Figure_sequences#timings_for_the_REXX_solutions timings for the REXX solutions]. <br> |
||
Line 2,677: | Line 2,677: | ||
===Version 2 from PL/I === |
===Version 2 from PL/I === |
||
< |
<syntaxhighlight lang="rexx">/* REXX ************************************************************** |
||
* 21.11.2012 Walter Pachl transcribed from PL/I |
* 21.11.2012 Walter Pachl transcribed from PL/I |
||
**********************************************************************/ |
**********************************************************************/ |
||
Line 2,748: | Line 2,748: | ||
if r <= 2*n then v.r = 1 |
if r <= 2*n then v.r = 1 |
||
end |
end |
||
return s</ |
return s</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Verification that the first 40 FFR numbers and the first |
<pre>Verification that the first 40 FFR numbers and the first |
||
Line 2,763: | Line 2,763: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Hofstadter Figure-Figure sequences |
# Project : Hofstadter Figure-Figure sequences |
||
Line 2,794: | Line 2,794: | ||
svect = left(svect, len(svect) - 1) |
svect = left(svect, len(svect) - 1) |
||
see svect + nl |
see svect + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,805: | Line 2,805: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
< |
<syntaxhighlight lang="ruby">$r = [nil, 1] |
||
$s = [nil, 2] |
$s = [nil, 2] |
||
Line 2,846: | Line 2,846: | ||
assert_equal(rs_values, Set.new( 1..1000 )) |
assert_equal(rs_values, Set.new( 1..1000 )) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
outputs |
outputs |
||
Line 2,857: | Line 2,857: | ||
===Using cyclic iterators=== |
===Using cyclic iterators=== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ruby">R = Enumerator.new do |y| |
||
y << n = 1 |
y << n = 1 |
||
S.each{|s_val| y << n += s_val} |
S.each{|s_val| y << n += s_val} |
||
Line 2,876: | Line 2,876: | ||
p S.take(10) |
p S.take(10) |
||
p (R.take(40)+ S.take(960)).sort == (1..1000).to_a |
p (R.take(40)+ S.take(960)).sort == (1..1000).to_a |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,885: | Line 2,885: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
use std::collections::HashMap; |
use std::collections::HashMap; |
||
Line 2,961: | Line 2,961: | ||
assert_eq!(f1000, s960, "Does NOT match"); |
assert_eq!(f1000, s960, "Does NOT match"); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,978: | Line 2,978: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">object HofstadterFigFigSeq extends App { |
||
import scala.collection.mutable.ListBuffer |
import scala.collection.mutable.ListBuffer |
||
Line 3,004: | Line 3,004: | ||
(1 to 10).map(i=>(i,ffr(i))).foreach(t=>println("r("+t._1+"): "+t._2)) |
(1 to 10).map(i=>(i,ffr(i))).foreach(t=>println("r("+t._1+"): "+t._2)) |
||
println((1 to 1000).toList.filterNot(((1 to 40).map(ffr(_))++(1 to 960).map(ffs(_))).contains)==List()) |
println((1 to 1000).toList.filterNot(((1 to 40).map(ffr(_))++(1 to 960).map(ffs(_))).contains)==List()) |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>r(1): 1 |
<pre>r(1): 1 |
||
Line 3,020: | Line 3,020: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">var r = [nil, 1] |
||
var s = [nil, 2] |
var s = [nil, 2] |
||
Line 3,054: | Line 3,054: | ||
say "These were missed: #{missed}" |
say "These were missed: #{missed}" |
||
say "These were duplicated: #{dupped}" |
say "These were duplicated: #{dupped}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,076: | Line 3,076: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{tcllib|struct::set}} |
{{tcllib|struct::set}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
package require struct::set |
package require struct::set |
||
Line 3,118: | Line 3,118: | ||
} |
} |
||
puts "set sizes: [struct::set size $numsInSeq] vs [struct::set size $numsRS]" |
puts "set sizes: [struct::set size $numsInSeq] vs [struct::set size $numsRS]" |
||
puts "set equality: [expr {[struct::set equal $numsInSeq $numsRS]?{yes}:{no}}]"</ |
puts "set equality: [expr {[struct::set equal $numsInSeq $numsRS]?{yes}:{no}}]"</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,138: | Line 3,138: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
Note that uBasic/4tH has ''no'' dynamic memory facilities and only ''one single array'' of 256 elements. So the only way to cram over a 1000 values there is to use a bitmap. This bitmap consists of an '''R''' range and an '''S''' range. In each range, a bit represents a positional value (bit 0 = "1", bit 1 = "2", etc.). The '''R'''(x) and '''S'''(x) functions simply count the number of bits set they encountered. To determine whether all integers between 1 and 1000 are complementary, both ranges are ''XOR''ed, which would result in a value other than 2<sup>31</sup>-1 if there were any discrepancies present. An extra check determines if there are exactly 40 '''R''' values. |
Note that uBasic/4tH has ''no'' dynamic memory facilities and only ''one single array'' of 256 elements. So the only way to cram over a 1000 values there is to use a bitmap. This bitmap consists of an '''R''' range and an '''S''' range. In each range, a bit represents a positional value (bit 0 = "1", bit 1 = "2", etc.). The '''R'''(x) and '''S'''(x) functions simply count the number of bits set they encountered. To determine whether all integers between 1 and 1000 are complementary, both ranges are ''XOR''ed, which would result in a value other than 2<sup>31</sup>-1 if there were any discrepancies present. An extra check determines if there are exactly 40 '''R''' values. |
||
<lang>Proc _SetBitR(1) ' Set the first R value |
<syntaxhighlight lang="text">Proc _SetBitR(1) ' Set the first R value |
||
Proc _SetBitS(2) ' Set the first S value |
Proc _SetBitS(2) ' Set the first S value |
||
Line 3,238: | Line 3,238: | ||
_GetBitS Param(1) ' Return bit n-1 in S-bitmap |
_GetBitS Param(1) ' Return bit n-1 in S-bitmap |
||
a@ = a@ - 1 |
a@ = a@ - 1 |
||
Return (AND(@(64+a@/31), SHL(1,a@%31))#0)</ |
Return (AND(@(64+a@/31), SHL(1,a@%31))#0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Creating bitmap, wait.. |
<pre>Creating bitmap, wait.. |
||
Line 3,282: | Line 3,282: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Private Function ffr(n As Long) As Long |
||
Dim R As New Collection |
Dim R As New Collection |
||
Dim S As New Collection |
Dim S As New Collection |
||
Line 3,340: | Line 3,340: | ||
Debug.Print "The first 40 values of ffr plus the first 960 values of ffs " |
Debug.Print "The first 40 values of ffr plus the first 960 values of ffs " |
||
Debug.Print "include all the integers from 1 to 1000 exactly once is "; Format(x.Count = 0) |
Debug.Print "include all the integers from 1 to 1000 exactly once is "; Format(x.Count = 0) |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>The first ten values of R are: |
<pre>The first ten values of R are: |
||
1 3 7 12 18 26 35 45 56 69 |
1 3 7 12 18 26 35 45 56 69 |
||
Line 3,347: | Line 3,347: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
'Initialize the r and the s arrays. |
'Initialize the r and the s arrays. |
||
Set r = CreateObject("System.Collections.ArrayList") |
Set r = CreateObject("System.Collections.ArrayList") |
||
Line 3,418: | Line 3,418: | ||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
End If |
End If |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 3,430: | Line 3,430: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ecmascript">var r = [0, 1] |
||
var s = [0, 2] |
var s = [0, 2] |
||
Line 3,459: | Line 3,459: | ||
var allPresent = present.skip(1).all { |i| i == true } |
var allPresent = present.skip(1).all { |i| i == true } |
||
System.print("\nThe first 40 values of ffr plus the first 960 values of ffs") |
System.print("\nThe first 40 values of ffr plus the first 960 values of ffs") |
||
System.print("includes all integers from 1 to 1000 exactly once is %(allPresent).")</ |
System.print("includes all integers from 1 to 1000 exactly once is %(allPresent).")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,471: | Line 3,471: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn genRS(reset=False){ //-->(n,R,S) |
||
var n=0, Rs=L(0,1), S=2; |
var n=0, Rs=L(0,1), S=2; |
||
if(True==reset){ n=0; Rs=L(0,1); S=2; return(); } |
if(True==reset){ n=0; Rs=L(0,1); S=2; return(); } |
||
Line 3,482: | Line 3,482: | ||
return(n+=1,R,S); |
return(n+=1,R,S); |
||
} |
} |
||
fcn ffrs(n) { genRS(True); do(n){ n=genRS() } n[1,2] } //-->( R(n),S(n) )</ |
fcn ffrs(n) { genRS(True); do(n){ n=genRS() } n[1,2] } //-->( R(n),S(n) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,488: | Line 3,488: | ||
L(1,3,7,12,18,26,35,45,56,69) |
L(1,3,7,12,18,26,35,45,56,69) |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="zkl">genRS(True); // reset |
||
sink:=(0).pump(40,List, 'wrap(ns){ T(Void.Write,Void.Write,genRS()[1,*]) }); |
sink:=(0).pump(40,List, 'wrap(ns){ T(Void.Write,Void.Write,genRS()[1,*]) }); |
||
sink= (0).pump(960-40,sink,'wrap(ns){ T(Void.Write,genRS()[2]) }); |
sink= (0).pump(960-40,sink,'wrap(ns){ T(Void.Write,genRS()[2]) }); |
||
(sink.sort()==[1..1000].pump(List)) // [1..n].pump(List)-->(1,2,3...) |
(sink.sort()==[1..1000].pump(List)) // [1..n].pump(List)-->(1,2,3...) |
||
.println("<-- should be True");</ |
.println("<-- should be True");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |