Hofstadter-Conway $10,000 sequence: Difference between revisions

Added Easylang
(Added Easylang)
 
(36 intermediate revisions by 20 users not shown)
Line 30:
It was later found that [[wp:Douglas Hofstadter|Hofstadter]] had also done prior work on the sequence.
 
The 'prize' was won quite quickly by [httphttps://wwwen.researchwikipedia.avayalabs.comorg/gcmwiki/usa/en-us/people/all/mallows.htmColin_Lingwood_Mallows Dr. Colin L. Mallows] who proved the properties of the sequence and allowed him to find the value of   n   (which is much smaller than the 3,173,375,556 quoted in the NYT article).
 
 
Line 44:
*   [http://mathworld.wolfram.com/Hofstadter-Conway10000-DollarSequence.html Mathworld Article].
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V last = 1 << 20
V a_list = [0] * (last+1)
a_list[0] = -50'000
a_list[1] = 1
a_list[2] = 1
 
V v = a_list[2]
V k1 = 2
V lg2 = 1
V amax = 0.0
 
L(n) 3..last
v = a_list[v] + a_list[n - v]
a_list[n] = v
amax = max(amax, Float(v) / n)
I (k1 [&] n) == 0
print(‘Maximum between 2^#. and 2^#. was #.6’.format(lg2, lg2 + 1, amax))
amax = 0
lg2++
k1 = n</syntaxhighlight>
 
{{out}}
<pre>
Maximum between 2^1 and 2^2 was 0.666667
Maximum between 2^2 and 2^3 was 0.666667
Maximum between 2^3 and 2^4 was 0.636364
Maximum between 2^4 and 2^5 was 0.608696
...
Maximum between 2^17 and 2^18 was 0.536020
Maximum between 2^18 and 2^19 was 0.534645
Maximum between 2^19 and 2^20 was 0.533779
</pre>
 
=={{header|360 Assembly}}==
Line 51 ⟶ 87:
<br>The program addresses the problem for l=2**12 (4K). For l=2**20 (1M) you must
allocate dynamic storage instead using static storage.
<langsyntaxhighlight lang="360asm">* Hofstadter-Conway $10,000 sequence 07/05/2016
HOFSTADT START
B 72(R15) skip savearea
Line 135 ⟶ 171:
A DS 4096F array a(uprdim)
REGEQU
END HOFSTADT</langsyntaxhighlight>
{{out}}
<pre>
Line 153 ⟶ 189:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">-- Ada95 version
-- Allocation of arrays on the heap
 
Line 226 ⟶ 262:
end Conway;
 
</syntaxhighlight>
</lang>
Sample output:
<pre>
Line 257 ⟶ 293:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.3.6/algol68g-2.3.6.tar.gz/download algol68g-2.3.6]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - ELLA Algol68 has no printf}}
<langsyntaxhighlight lang="algol68">PROC do sqnc = (INT max)INT:
BEGIN
[max]INT a list;
Line 292 ⟶ 328:
of the same name inside PROC do sqnc - they are in different scopes#
 
printf(($"You too might have won $1000 with an answer of n = "g(0)$, mallows number))</langsyntaxhighlight>
Output:
<pre>
Line 316 ⟶ 352:
You too might have won $1000 with an answer of n = 1489
</pre>
 
=={{header|AppleScript}}==
Like the other solutions here, this takes the linguistically confusing expression ''"the first position, &nbsp; p &nbsp; in the sequence where │a(n)/n│ < 0.55 for all n > p"'' to mean the last position where the result's ''not'' < 0.55.
 
<syntaxhighlight lang="applescript">on HC10000(ceiling)
script o
property lst : {1, 1}
property maxima : {}
end script
set p to missing value
set max to 0
set maxPos to 0
set power to 0
set x to end of o's lst
repeat with n from 3 to ceiling
set x to (item x of o's lst) + (item -x of o's lst)
set end of o's lst to x
set ann to x / n
if (ann is not less than 0.55) then set p to n
if (ann > max) then
set max to ann
set maxPos to n
end if
if (ann is 0.5) then
set power to power + 1
set end of o's maxima to {|<-powers->|:{power, power + 1}, n:maxPos, max:max}
set max to 0
end if
end repeat
return {p:p, maxima:o's maxima}
end HC10000
 
HC10000(2 ^ 20)</syntaxhighlight>
 
{{output}}
 
<syntaxhighlight lang="applescript">{p:1489, maxima:{{|<-powers->|:{1, 2}, n:3, max:0.666666666667}, {|<-powers->|:{2, 3}, n:6, max:0.666666666667}, {|<-powers->|:{3, 4}, n:11, max:0.636363636364}, {|<-powers->|:{4, 5}, n:23, max:0.608695652174}, {|<-powers->|:{5, 6}, n:44, max:0.590909090909}, {|<-powers->|:{6, 7}, n:92, max:0.576086956522}, {|<-powers->|:{7, 8}, n:178, max:0.567415730337}, {|<-powers->|:{8, 9}, n:370, max:0.559459459459}, {|<-powers->|:{9, 10}, n:719, max:0.554937413074}, {|<-powers->|:{10, 11}, n:1487, max:0.550100874243}, {|<-powers->|:{11, 12}, n:2897, max:0.547462892648}, {|<-powers->|:{12, 13}, n:5969, max:0.544144747864}, {|<-powers->|:{13, 14}, n:11651, max:0.54244270878}, {|<-powers->|:{14, 15}, n:22223, max:0.540071097512}, {|<-powers->|:{15, 16}, n:45083, max:0.538784020584}, {|<-powers->|:{16, 17}, n:89516, max:0.537043657}, {|<-powers->|:{17, 18}, n:181385, max:0.536020067812}, {|<-powers->|:{18, 19}, n:353683, max:0.534645431078}, {|<-powers->|:{19, 20}, n:722589, max:0.533779229963}}}</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">Progress, b2 w150 zh0 fs9, CreateLists ...
CreateLists(2 ** (Max:=20))
 
Line 364 ⟶ 439:
n_%A_Index% := a_%A_Index% / A_Index
}
}</langsyntaxhighlight>
Message box shows:
<pre>Maximum between 2^1 and 2^2 is 0.666667 for n = 3
Line 390 ⟶ 465:
 
Iterative approach:
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
BEGIN {
NN = 20;
Line 411 ⟶ 486:
Q[n] = Q[Q[n-1]]+Q[n-Q[n-1]];
}
} </langsyntaxhighlight>
 
Recursive variant:
 
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
BEGIN {
Q[1] = 1;
Line 454 ⟶ 529:
S[n] = 1;
return (Q[n]);
} </langsyntaxhighlight>
 
Output:
Line 478 ⟶ 553:
</pre>
 
=={{header|BBC BASIC}}==
==={{header|BASIC256}}===
<lang bbcbasic>HIMEM=LOMEM+1E7 : REM Reserve enough memory for a 4 MB array, plus other code
<syntaxhighlight lang="basic256">arraybase 1
pow2 = 2
p2 = 2 ^ pow2
peak = .5
dim a(2 ^ 20)
a[1] = 1
a[2] = 1
 
for n = 3 to 2 ^ 20
a[n] = a[a[n-1]] + a[n-a[n-1]]
r = a[n] / n
if r >= .55 then mallows = n
if r > peak then peak = r : peakpos = n
if n = p2 then
print "Maximum between 2 ^ "; rjust((pow2 - 1),2); " and 2 ^ "; rjust(pow2,2); " is "; ljust(peak,13,"0"); " at n = "; peakpos
pow2 += 1
p2 = 2 ^ pow2
peak = .5
end if
next n
 
print
print "Mallows number is "; mallows</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic">HIMEM=LOMEM+1E7 : REM Reserve enough memory for a 4 MB array, plus other code
DIM a%(2^20)
a%(1)=1
Line 499 ⟶ 602:
ENDIF
NEXT n%
PRINT "Mallows number is ";Mallows%</langsyntaxhighlight>
 
Results
Line 523 ⟶ 626:
Maximum between 2^19 and 2^20 is 0.53377923 at n=722589
Mallows number is 1489</pre>
 
==={{header|FreeBASIC}}===
{{trans|BBC BASIC}}
<syntaxhighlight lang="freebasic">' version 13-07-2018
' compile with: fbc -s console
 
Dim As UInteger a(), pow2 = 2, p2 = 2 ^ pow2, peakpos, n, mallows
Dim As Double peak = 0.5, r
ReDim a(2 ^ 20)
a(1) = 1
a(2) = 1
 
For n = 3 To 2 ^ 20
a(n) = a(a(n -1)) + a(n - a(n -1))
r = a(n) / n
If r >= 0.55 Then mallows = n
If r > peak Then peak = r : peakpos = n
If n = p2 Then
Print Using "Maximum between 2 ^ ## and 2 ^ ## is"; pow2 -1; pow2;
Print Using " #.##### at n = "; peak;
Print peakpos
pow2 += 1
p2 = 2 ^ pow2
peak = 0.5
End If
Next
 
Print
Print "Mallows number is "; mallows
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>Maximum between 2 ^ 1 and 2 ^ 2 is 0.66667 at n = 3
Maximum between 2 ^ 2 and 2 ^ 3 is 0.66667 at n = 6
Maximum between 2 ^ 3 and 2 ^ 4 is 0.63636 at n = 11
Maximum between 2 ^ 4 and 2 ^ 5 is 0.60870 at n = 23
Maximum between 2 ^ 5 and 2 ^ 6 is 0.59091 at n = 44
Maximum between 2 ^ 6 and 2 ^ 7 is 0.57609 at n = 92
Maximum between 2 ^ 7 and 2 ^ 8 is 0.56742 at n = 178
Maximum between 2 ^ 8 and 2 ^ 9 is 0.55946 at n = 370
Maximum between 2 ^ 9 and 2 ^ 10 is 0.55494 at n = 719
Maximum between 2 ^ 10 and 2 ^ 11 is 0.55010 at n = 1487
Maximum between 2 ^ 11 and 2 ^ 12 is 0.54746 at n = 2897
Maximum between 2 ^ 12 and 2 ^ 13 is 0.54414 at n = 5969
Maximum between 2 ^ 13 and 2 ^ 14 is 0.54244 at n = 11651
Maximum between 2 ^ 14 and 2 ^ 15 is 0.54007 at n = 22223
Maximum between 2 ^ 15 and 2 ^ 16 is 0.53878 at n = 45083
Maximum between 2 ^ 16 and 2 ^ 17 is 0.53704 at n = 89516
Maximum between 2 ^ 17 and 2 ^ 18 is 0.53602 at n = 181385
Maximum between 2 ^ 18 and 2 ^ 19 is 0.53465 at n = 353683
Maximum between 2 ^ 19 and 2 ^ 20 is 0.53378 at n = 722589
 
Mallows number is 1489</pre>
 
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">If OpenConsole()
Define.i upperlim, i=1, k1=2, n=3, v=1
Define.d Maximum
Print("Enter limit (ENTER gives 2^20=1048576): "): upperlim=Val(Input())
If upperlim<=0: upperlim=1048576: EndIf
Dim tal(upperlim)
If ArraySize(tal())=-1
PrintN("Could not allocate needed memory!"): Input(): End
EndIf
tal(1)=1: tal(2)=1
While n<=upperlim
v=tal(v)+tal(n-v)
tal(n)=v
If Maximum<(v/n): Maximum=v/n: EndIf
If Not n&k1
PrintN("Maximum between 2^"+Str(i)+" and 2^"+Str(i+1)+" was "+StrD(Maximum,6))
Maximum=0.0
i+1
EndIf
k1=n
n+1
Wend
Print(#CRLF$+"Press ENTER to exit."): Input()
CloseConsole()
EndIf</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">input "Enter upper limit between 1 and 20 (ENTER 20 gives 2^20):"); uprLim
if uprLim < 1 or uprLim > 20 then uprLim = 20
dim a(2^uprLim)
a(1) = 1
a(2) = 1
pow2 = 2
p2 = 2^pow2
p = 0.5
pPos = 0
for n = 3 TO 2^uprLim
a(n) = a(a(n-1)) + a(n-a(n-1))
r = a(n)/n
if r >= 0.55 THEN Mallows = n
if r > p THEN
p = r
pPos = n
end if
if n = p2 THEN
print "Maximum between";chr$(9);" 2^";pow2-1;" and 2^";pow2;chr$(9);" is ";p;chr$(9);" at n = ";pPos
pow2 = pow2 + 1
p2 = 2^pow2
p = 0.5
end IF
next n
print "Mallows number is ";Mallows</syntaxhighlight>
<pre>Enter upper limit between 1 and 20 (ENTER 20 gives 2^20): ?20
Maximum between 2^1 and 2^2 is 0.666666698 at n = 3
Maximum between 2^2 and 2^3 is 0.666666698 at n = 6
Maximum between 2^3 and 2^4 is 0.636363601 at n = 11
Maximum between 2^4 and 2^5 is 0.608695602 at n = 23
Maximum between 2^5 and 2^6 is 0.590909051 at n = 44
Maximum between 2^6 and 2^7 is 0.57608695 at n = 92
Maximum between 2^7 and 2^8 is 0.567415714 at n = 178
Maximum between 2^8 and 2^9 is 0.559459447 at n = 370
Maximum between 2^9 and 2^10 is 0.55493741 at n = 719
Maximum between 2^10 and 2^11 is 0.550100851 at n = 1487
Maximum between 2^11 and 2^12 is 0.547462892 at n = 2897
Maximum between 2^12 and 2^13 is 0.544144725 at n = 5969
Maximum between 2^13 and 2^14 is 0.542442655 at n = 11651
Maximum between 2^14 and 2^15 is 0.540071058 at n = 22223
Maximum between 2^15 and 2^16 is 0.538784027 at n = 45083
Maximum between 2^16 and 2^17 is 0.537043619 at n = 89516
Maximum between 2^17 and 2^18 is 0.53602004 at n = 181385
Maximum between 2^18 and 2^19 is 0.534645414 at n = 353683
Maximum between 2^19 and 2^20 is 0.533779191 at n = 722589
Mallows number is 1489</pre>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">LET pow2 = 2
LET p2 = 2^pow2
LET peak = 0.5
DIM a(0)
MAT REDIM a(2^20)
LET a(1) = 1
LET a(2) = 1
FOR n = 3 TO 2^20
LET a(n) = a(a(n-1))+a(n-a(n-1))
LET r = a(n)/n
IF r >= 0.55 THEN LET mallows = n
IF r > peak THEN
LET peak = r
LET peakpos = n
END IF
IF n = p2 THEN
PRINT USING "Maximum between 2 ^ ## and 2 ^ ## is": pow2-1, pow2;
PRINT USING " #.##### at n = ": peak;
PRINT peakpos
LET pow2 = pow2+1
LET p2 = 2^pow2
LET peak = 0.5
END IF
NEXT n
PRINT
PRINT "Mallows number is "; mallows
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="freebasic">pow2 = 2
p2 = 2 ^ pow2
peak = .5
dim a(2 ^ 20)
a(1) = 1
a(2) = 1
 
for n = 3 to 2 ^ 20
a(n) = a(a(n - 1)) + a(n - a(n - 1))
r = a(n) / n
if r >= .55 mallows = n
if r > peak then peak = r : peakpos = n : fi
if n = p2 then
print "Maximum between 2 ^ ", pow2 - 1 using("##"), " and 2 ^ ", pow2 using("##"), " is ", peak using("#.#####"), " at n = ", peakpos
pow2 = pow2 + 1
p2 = 2 ^ pow2
peak = .5
end if
next n
 
print "\nMallows number is ", mallows</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|ZX Spectrum Basic}}===
{{trans|BBC_BASIC}}
Nine first results.
<syntaxhighlight lang="zxbasic">10 DIM a(2000)
20 LET a(1)=1: LET a(2)=1
30 LET pow2=2: LET p2=2^pow2
40 LET peak=0.5: LET peakpos=0
50 FOR n=3 TO 2000
60 LET a(n)=a(a(n-1))+a(n-a(n-1))
70 LET r=a(n)/n
80 IF r>0.55 THEN LET Mallows=n
90 IF r>peak THEN LET peak=r: LET peakpos=n
100 IF n=p2 THEN PRINT "Maximum (2^";pow2-1;", 2^";pow2;") is ";peak;" at n=";peakpos: LET pow2=pow2+1: LET p2=2^pow2: LET peak=0.5
110 NEXT n
120 PRINT "Mallows number is ";Mallows</syntaxhighlight>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( a
=
. !arg:(1|2)&1
Line 580 ⟶ 890:
)
)
)</langsyntaxhighlight>
Output:
<pre>Between 2^0 and 2^1 the maximum value of a(n)/n is reached for n = 1 with the value 1
Line 605 ⟶ 915:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 633 ⟶ 943:
}
return 1;
}</langsyntaxhighlight>
Results
<pre>Maximum between 2^1 and 2^2 was 0.666667
Line 643 ⟶ 953:
Maximum between 2^19 and 2^20 was 0.533779</pre>
 
=={{header|C++}}==
<lang cpp>
#include <deque>
#include <iostream>
 
int hcseq(int n)
{
static std::deque<int> seq(2, 1);
while (seq.size() < n)
{
int x = seq.back();
seq.push_back(seq[x-1] + seq[seq.size()-x]);
}
return seq[n-1];
}
 
int main()
{
int pow2 = 1;
for (int i = 0; i < 20; ++i)
{
int pow2next = 2*pow2;
double max = 0;
for (int n = pow2; n < pow2next; ++n)
{
double anon = hcseq(n)/double(n);
if (anon > max)
max = anon;
}
std::cout << "maximum of a(n)/n between 2^" << i
<< " (" << pow2 << ") and 2^" << i+1
<< " (" << pow2next << ") is " << max << "\n";
pow2 = pow2next;
}
}
</lang>
Output:
<pre>
maximum of a(n)/n between 2^0 (1) and 2^1 (2) is 1
maximum of a(n)/n between 2^1 (2) and 2^2 (4) is 0.666667
maximum of a(n)/n between 2^2 (4) and 2^3 (8) is 0.666667
maximum of a(n)/n between 2^3 (8) and 2^4 (16) is 0.636364
maximum of a(n)/n between 2^4 (16) and 2^5 (32) is 0.608696
maximum of a(n)/n between 2^5 (32) and 2^6 (64) is 0.590909
maximum of a(n)/n between 2^6 (64) and 2^7 (128) is 0.576087
maximum of a(n)/n between 2^7 (128) and 2^8 (256) is 0.567416
maximum of a(n)/n between 2^8 (256) and 2^9 (512) is 0.559459
maximum of a(n)/n between 2^9 (512) and 2^10 (1024) is 0.554937
maximum of a(n)/n between 2^10 (1024) and 2^11 (2048) is 0.550101
maximum of a(n)/n between 2^11 (2048) and 2^12 (4096) is 0.547463
maximum of a(n)/n between 2^12 (4096) and 2^13 (8192) is 0.544145
maximum of a(n)/n between 2^13 (8192) and 2^14 (16384) is 0.542443
maximum of a(n)/n between 2^14 (16384) and 2^15 (32768) is 0.540071
maximum of a(n)/n between 2^15 (32768) and 2^16 (65536) is 0.538784
maximum of a(n)/n between 2^16 (65536) and 2^17 (131072) is 0.537044
maximum of a(n)/n between 2^17 (131072) and 2^18 (262144) is 0.53602
maximum of a(n)/n between 2^18 (262144) and 2^19 (524288) is 0.534645
maximum of a(n)/n between 2^19 (524288) and 2^20 (1048576) is 0.533779
</pre>
=={{header|C sharp|C#}}==
{{works with|C#|3.0}}
 
<langsyntaxhighlight lang="csharp">
using System;
using System.Linq;
Line 749 ⟶ 1,000:
}
}
</syntaxhighlight>
</lang>
Output:-
<pre>Maximum from 2^1 to 2^2 is 0.666666666666667 at 3
Line 772 ⟶ 1,023:
The winning number is 1489.
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
#include <deque>
#include <iostream>
 
int hcseq(int n)
{
static std::deque<int> seq(2, 1);
while (seq.size() < n)
{
int x = seq.back();
seq.push_back(seq[x-1] + seq[seq.size()-x]);
}
return seq[n-1];
}
 
int main()
{
int pow2 = 1;
for (int i = 0; i < 20; ++i)
{
int pow2next = 2*pow2;
double max = 0;
for (int n = pow2; n < pow2next; ++n)
{
double anon = hcseq(n)/double(n);
if (anon > max)
max = anon;
}
std::cout << "maximum of a(n)/n between 2^" << i
<< " (" << pow2 << ") and 2^" << i+1
<< " (" << pow2next << ") is " << max << "\n";
pow2 = pow2next;
}
}
</syntaxhighlight>
Output:
<pre>
maximum of a(n)/n between 2^0 (1) and 2^1 (2) is 1
maximum of a(n)/n between 2^1 (2) and 2^2 (4) is 0.666667
maximum of a(n)/n between 2^2 (4) and 2^3 (8) is 0.666667
maximum of a(n)/n between 2^3 (8) and 2^4 (16) is 0.636364
maximum of a(n)/n between 2^4 (16) and 2^5 (32) is 0.608696
maximum of a(n)/n between 2^5 (32) and 2^6 (64) is 0.590909
maximum of a(n)/n between 2^6 (64) and 2^7 (128) is 0.576087
maximum of a(n)/n between 2^7 (128) and 2^8 (256) is 0.567416
maximum of a(n)/n between 2^8 (256) and 2^9 (512) is 0.559459
maximum of a(n)/n between 2^9 (512) and 2^10 (1024) is 0.554937
maximum of a(n)/n between 2^10 (1024) and 2^11 (2048) is 0.550101
maximum of a(n)/n between 2^11 (2048) and 2^12 (4096) is 0.547463
maximum of a(n)/n between 2^12 (4096) and 2^13 (8192) is 0.544145
maximum of a(n)/n between 2^13 (8192) and 2^14 (16384) is 0.542443
maximum of a(n)/n between 2^14 (16384) and 2^15 (32768) is 0.540071
maximum of a(n)/n between 2^15 (32768) and 2^16 (65536) is 0.538784
maximum of a(n)/n between 2^16 (65536) and 2^17 (131072) is 0.537044
maximum of a(n)/n between 2^17 (131072) and 2^18 (262144) is 0.53602
maximum of a(n)/n between 2^18 (262144) and 2^19 (524288) is 0.534645
maximum of a(n)/n between 2^19 (524288) and 2^20 (1048576) is 0.533779
</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(ns rosettacode.hofstader-conway
(:use [clojure.math.numeric-tower :only [expt]]))
 
Line 796 ⟶ 1,108:
(take 4 maxima) "\n"
(apply >= m20) "\n"
(map double m20))) ; output the decimal forms</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defparameter *hof-con*
(make-array '(2) :initial-contents '(1 1) :adjustable t
:element-type 'integer :fill-pointer 2))
Line 840 ⟶ 1,152:
(hof-con n)
(do ((i (1- n) (1- i)))
((> (aref *hof-con-ratios* i) 0.55) (+ i 1)))))</langsyntaxhighlight>
Sample session:
<pre>ROSETTA> (maxima 20)
Line 867 ⟶ 1,179:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
void hofstadterConwaySequence(in int m) {
Line 890 ⟶ 1,202:
void main() {
hofstadterConwaySequence(2 ^^ 20);
}</langsyntaxhighlight>
 
Output:
Line 912 ⟶ 1,224:
Max in [2^18, 2^19]: 0.534645
Max in [2^19, 2^20]: 0.533779</pre>
 
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight>
numfmt 4 0
a[] = [ 1 1 ]
x = 1
n = 2
mallow = 0
for p = 1 to 19
max = 0
nextPot = n * 2
while n < nextPot
n = len a[] + 1
x = a[x] + a[n - x]
a[] &= x
f = x / n
max = higher max f
if f >= 0.55
mallow = n
.
.
print "max between 2^" & p & " and 2^" & p + 1 & " was " & max
.
print "winning number " & mallow
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(decimals 4)
(cache-size 2000000)
Line 943 ⟶ 1,281:
#:break (> (// (a n) n) 0.55) => n )
)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 969 ⟶ 1,307:
→ 1489 ;; Mallows number
</pre>
 
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,033 ⟶ 1,370:
 
end
</syntaxhighlight>
</lang>
{{out}}
As the run time is quite slow, the test output is shown only up to 2^15.
Line 1,054 ⟶ 1,391:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( hofstadter_conway ).
 
Line 1,090 ⟶ 1,427:
At_end = dict:fetch( Key - Last_number, Dict ),
dict:store( Key, At_begining + At_end, Dict ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,117 ⟶ 1,454:
=={{header|Euler Math Toolbox}}==
 
<syntaxhighlight lang="euler math toolbox">
<lang Euler Math Toolbox>
>function hofstadter (n) ...
$v=ones(1,n);
Line 1,145 ⟶ 1,482:
>max(nonzeros(v1>0.55))
1489
</syntaxhighlight>
</lang>
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let a = ResizeArray[0; 1; 1]
while a.Count <= (1 <<< 20) do
a.[a.[a.Count - 1]] + a.[a.Count - a.[a.Count - 1]] |> a.Add
Line 1,158 ⟶ 1,496:
|> List.rev
|> List.find (fun (i, n) -> float(n) / float(i) > 0.55)
printfn "Mallows number is %d" mallows</langsyntaxhighlight>
Outputs:
<pre>Maximum in 2.. 4 is 0.666667
Line 1,180 ⟶ 1,518:
Maximum in 524288..1048576 is 0.533779
Mallows number is 1489</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: combinators formatting io kernel locals math math.ranges
prettyprint sequences splitting ;
 
MEMO:: a ( n -- a(n) ) ! memoize the recurrence relation
n {
{ 1 [ 1 ] }
{ 2 [ 1 ] }
[ 1 - a a n n 1 - a - a + ]
} case ;
 
20 2^ [1,b] [ [ a ] [ 1 + / ] bi* ] map-index
[
{ 1/2 } split harvest rest-slice
[ supremum ] map 1 19 [a,b]
[ dup 1 + [ 2^ ] bi@ "%f max in (%d, %d)\n" printf ]
2each
]
[ "Mallow's number: " write [ 0.55 >= ] find-last drop 1 + . ]
bi</syntaxhighlight>
{{out}}
<pre>
0.666667 max in (2, 4)
0.666667 max in (4, 8)
0.636364 max in (8, 16)
0.608696 max in (16, 32)
0.590909 max in (32, 64)
0.576087 max in (64, 128)
0.567416 max in (128, 256)
0.559459 max in (256, 512)
0.554937 max in (512, 1024)
0.550101 max in (1024, 2048)
0.547463 max in (2048, 4096)
0.544145 max in (4096, 8192)
0.542443 max in (8192, 16384)
0.540071 max in (16384, 32768)
0.538784 max in (32768, 65536)
0.537044 max in (65536, 131072)
0.536020 max in (131072, 262144)
0.534645 max in (262144, 524288)
0.533779 max in (524288, 1048576)
Mallow's number: 1489
</pre>
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
<lang Fortran>
program conway
implicit none
Line 1,227 ⟶ 1,610:
 
end program conway
</syntaxhighlight>
</lang>
 
Output:
Line 1,252 ⟶ 1,635:
Mallows number = 1489
</pre>
=={{header|FreeBASIC}}==
{{trans|BBC BASIC}}
<lang freebasic>' version 13-07-2018
' compile with: fbc -s console
 
=={{header|Fōrmulæ}}==
Dim As UInteger a(), pow2 = 2, p2 = 2 ^ pow2, peakpos, n, mallows
Dim As Double peak = 0.5, r
ReDim a(2 ^ 20)
a(1) = 1
a(2) = 1
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Hofstadter-Conway_%2410%2C000_sequence}}
For n = 3 To 2 ^ 20
a(n) = a(a(n -1)) + a(n - a(n -1))
r = a(n) / n
If r >= 0.55 Then mallows = n
If r > peak Then peak = r : peakpos = n
If n = p2 Then
Print Using "Maximum between 2 ^ ## and 2 ^ ## is"; pow2 -1; pow2;
Print Using " #.##### at n = "; peak;
Print peakpos
pow2 += 1
p2 = 2 ^ pow2
peak = 0.5
End If
Next
 
'''Solution'''
Print
Print "Mallows number is "; mallows
 
This is the function according to the definition. It is very inefficient:
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>Maximum between 2 ^ 1 and 2 ^ 2 is 0.66667 at n = 3
Maximum between 2 ^ 2 and 2 ^ 3 is 0.66667 at n = 6
Maximum between 2 ^ 3 and 2 ^ 4 is 0.63636 at n = 11
Maximum between 2 ^ 4 and 2 ^ 5 is 0.60870 at n = 23
Maximum between 2 ^ 5 and 2 ^ 6 is 0.59091 at n = 44
Maximum between 2 ^ 6 and 2 ^ 7 is 0.57609 at n = 92
Maximum between 2 ^ 7 and 2 ^ 8 is 0.56742 at n = 178
Maximum between 2 ^ 8 and 2 ^ 9 is 0.55946 at n = 370
Maximum between 2 ^ 9 and 2 ^ 10 is 0.55494 at n = 719
Maximum between 2 ^ 10 and 2 ^ 11 is 0.55010 at n = 1487
Maximum between 2 ^ 11 and 2 ^ 12 is 0.54746 at n = 2897
Maximum between 2 ^ 12 and 2 ^ 13 is 0.54414 at n = 5969
Maximum between 2 ^ 13 and 2 ^ 14 is 0.54244 at n = 11651
Maximum between 2 ^ 14 and 2 ^ 15 is 0.54007 at n = 22223
Maximum between 2 ^ 15 and 2 ^ 16 is 0.53878 at n = 45083
Maximum between 2 ^ 16 and 2 ^ 17 is 0.53704 at n = 89516
Maximum between 2 ^ 17 and 2 ^ 18 is 0.53602 at n = 181385
Maximum between 2 ^ 18 and 2 ^ 19 is 0.53465 at n = 353683
Maximum between 2 ^ 19 and 2 ^ 20 is 0.53378 at n = 722589
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 01.png]]
Mallows number is 1489</pre>
 
If a sequence is desired, it is much better to store the already calculated terms:
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 02.png]]
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 03.png]]
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 04.png]]
 
'''Test case 1.''' Show the maxima of <math>\frac{A(n)}{n}</math> between successive powers of two up to 2<sup>20</sup>
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 05.png]]
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 06.png]]
 
'''Test case 2.''' compute the value of n that would have won the prize and confirm it is true for n up to 2<sup>20</sup>
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 07.png]]
 
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 08.png]]
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">window 1
include "ConsoleWindow"
 
// Set width of tab
Line 1,357 ⟶ 1,713:
print
print "Dr. Mallow's winning number is:"; Mallows
 
</lang>
HandleEvents</syntaxhighlight>
 
Output:
Line 1,387 ⟶ 1,744:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,415 ⟶ 1,772:
}
fmt.Println("winning number", mallow)
}</langsyntaxhighlight>
Output:
<pre>
Line 1,442 ⟶ 1,799:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List
import Data.Ord
import Data.Array
Line 1,466 ⟶ 1,823:
max seq = maximumBy (comparing snd) $ zip seq (map hc' seq)
powers = map (\n -> [2^n .. 2^(n + 1) - 1]) [0 .. 19]
mallows = last.takeWhile ((< 0.55) . hc') $ [2^20, 2^20 - 1 .. 1]</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="icon">procedure main(args)
m := integer(!args) | 20
nextNum := create put(A := [], 1 | 1 | |A[A[*A]]+A[-A[*A]])[*A]
Line 1,484 ⟶ 1,841:
}
write("Mallows's number is ",\mallows | "NOT found!")
end</langsyntaxhighlight>
 
Output:
Line 1,512 ⟶ 1,869:
 
=={{header|J}}==
'''Solution''' (tacit): <langsyntaxhighlight lang="j"> hc10k =: , ] +/@:{~ (,&<: -.)@{: NB. Actual sequence a(n)
AnN =: % 1+i.@:# NB. a(n)/n
MxAnN =: >./;.1~ 2 (=<.)@:^. 1+i.@# NB. Maxima of a(n)/n between successive powers of 2</langsyntaxhighlight>
'''Alternative solution''' (exponential growth):
<br>The first, naive, formulation of <code>hc10k</code> grows by a single term every iteration; in this one, the series grows exponentially in the iterations.
<langsyntaxhighlight lang="j"> hc10kE =: 1 1 , expand@tail
expand =: 2+I.@;
tail =: copies&.>^:(<@>:`(<@,@2:))
copies =: >: |.@(#!.1 |.)~ 1 j. #;.1 #^:_1 ::1:~ ]~:{.</langsyntaxhighlight>
 
'''Example''': <langsyntaxhighlight lang="j"> ] A=:1 1 hc10k @]^:[~ 2^20x
1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 ...
AnN A
Line 1,529 ⟶ 1,886:
1 0.666667 0.666667 0.636364 ...
MxAnN@AnN@hc10kE 20
1 0.666667 0.666667 0.636364 ...</langsyntaxhighlight>
 
=={{header|Java}}==
 
{{trans|C}} with corrections to 0 indexing.
Remove translation and provide native Java implementation.
<lang java>public class HofCon
 
{
<syntaxhighlight lang="java">
public static void main(final String[] args)
// Title: Hofstadter-Conway $10,000 sequence
{
 
doSqnc(1<<20);
public class HofstadterConwaySequence {
}
 
public static void doSqnc(int m)
private static int MAX = (int) Math.pow(2, 20) + 1;
{
private static int[] a_listHCS = new int[m + 1MAX];
int max_df =static 0;{
int p2_max HCS[1] = 21;
int k1 = HCS[2;] = 1;
for ( int lg2n = 13 ; n < MAX ; n++ ) {
int nm1 = HCS[n - 1];
double amax = 0;
a_list HCS[0n] = a_listHCS[1nm1] =+ 1HCS[n - nm1];
int v = a_list[2]; }
}
for (int n = 2; n <= m; n++)
{
public static void main(String[] args) {
v = a_list[n] = a_list[v] + a_list[n - v];
if (amax < v * 1.0int /mNum n)= 0;
amax = v * for ( int m = 1.0 /; m < 20 n; m++ ) {
if (0 = int min = (k1int) &Math.pow(2, n)m);
int max = min * 2;
{
double maxRatio = 0.0;
System.out.printf("Maximum between 2^%d and 2^%d was %f\n", lg2, lg2 + 1, amax);
amax int nVal = 0;
for ( int n = min ; n <= max ; n ++ ) {
lg2++;
double ratio = (double) HCS[n] / n;
}
if ( ratio > maxRatio ) {
k1 = n;
maxRatio = Math.max(ratio, maxRatio);
}
nVal = n;
}
}
}</lang>
if ( ratio >= 0.55 ) {
Output:
mNum = n;
<pre>Maximum between 2^1 and 2^2 was 0.666667
}
Maximum between 2^2 and 2^3 was 0.666667
}
Maximum between 2^3 and 2^4 was 0.636364
System.out.printf("Max ratio between 2^%d and 2^%d is %f at n = %,d%n", m, m+1, maxRatio, nVal);
Maximum between 2^4 and 2^5 was 0.608696
}
....
System.out.printf("Mallow's number is %d.%n", mNum);
Maximum between 2^18 and 2^19 was 0.534645
}
Maximum between 2^19 and 2^20 was 0.533779</pre>
 
}
</syntaxhighlight>
{{out}}
<pre>
Max ratio between 2^1 and 2^2 is 0.666667 at n = 3
Max ratio between 2^2 and 2^3 is 0.666667 at n = 6
Max ratio between 2^3 and 2^4 is 0.636364 at n = 11
Max ratio between 2^4 and 2^5 is 0.608696 at n = 23
Max ratio between 2^5 and 2^6 is 0.590909 at n = 44
Max ratio between 2^6 and 2^7 is 0.576087 at n = 92
Max ratio between 2^7 and 2^8 is 0.567416 at n = 178
Max ratio between 2^8 and 2^9 is 0.559459 at n = 370
Max ratio between 2^9 and 2^10 is 0.554937 at n = 719
Max ratio between 2^10 and 2^11 is 0.550101 at n = 1,487
Max ratio between 2^11 and 2^12 is 0.547463 at n = 2,897
Max ratio between 2^12 and 2^13 is 0.544145 at n = 5,969
Max ratio between 2^13 and 2^14 is 0.542443 at n = 11,651
Max ratio between 2^14 and 2^15 is 0.540071 at n = 22,223
Max ratio between 2^15 and 2^16 is 0.538784 at n = 45,083
Max ratio between 2^16 and 2^17 is 0.537044 at n = 89,516
Max ratio between 2^17 and 2^18 is 0.536020 at n = 181,385
Max ratio between 2^18 and 2^19 is 0.534645 at n = 353,683
Max ratio between 2^19 and 2^20 is 0.533779 at n = 722,589
Mallow's number is 1489.
</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight JavaScriptlang="javascript">var hofst_10k = function(n) {
var memo = [1, 1];
 
Line 1,596 ⟶ 1,981:
for(var i = 1; i <= 20; i += 1) {
console.log("Maxima between 2^"+i+"-2^"+(i+1)+" is: "+maxima_between_twos(i)+"\n");
}</langsyntaxhighlight>
Output:
<pre>Maxima between 2^1-2^2 is: 0.6666666666666666
Line 1,607 ⟶ 1,992:
Maxima between 2^19-2^20 is: 0.5337792299633678
Maxima between 2^20-2^21 is: 0.5326770563524978
</pre>
 
=={{header|jq}}==
{{works with|jq}}
<syntaxhighlight lang="jq">def hcSequence($limit):
reduce range(3; $limit) as $n ([0,1,1];
.[$n-1] as $p
| .[$n] = .[$p] + .[$n-$p]);
 
def task($limit):
hcSequence($limit) as $a
| " Range Maximum",
"---------------- --------",
(foreach range(2; $limit) as $n ( {max: $a[1], pow2: 1, p:1 };
($a[$n] / $n) as $r
| .emit = null
| if $r > .max then .max=$r else . end
| if ($n == .pow2 * 2)
then .emit = "2 ^ \(.p-1) to 2 ^ \(.p) \(.max)"
| .pow2 *= 2
| .p += 1
| .max = $r
else . end;
select(.emit).emit)),
 
( (first( range( $limit-1; 0; -1) as $n
| if ($a[$n]/$n >= 0.55) then $n else empty end) ) // 0
| "\nMallows' number = \(.)") ;
 
task( pow(2;20) + 1 )</syntaxhighlight>
{{out}}
<pre>
Range Maximum
---------------- --------
2 ^ 0 to 2 ^ 1 1
2 ^ 1 to 2 ^ 2 0.6666666666666666
2 ^ 2 to 2 ^ 3 0.6666666666666666
2 ^ 3 to 2 ^ 4 0.6363636363636364
2 ^ 4 to 2 ^ 5 0.6086956521739131
2 ^ 5 to 2 ^ 6 0.5909090909090909
2 ^ 6 to 2 ^ 7 0.5760869565217391
2 ^ 7 to 2 ^ 8 0.5674157303370787
2 ^ 8 to 2 ^ 9 0.5594594594594594
2 ^ 9 to 2 ^ 10 0.5549374130737135
2 ^ 10 to 2 ^ 11 0.5501008742434432
2 ^ 11 to 2 ^ 12 0.5474628926475664
2 ^ 12 to 2 ^ 13 0.5441447478639638
2 ^ 13 to 2 ^ 14 0.5424427087803622
2 ^ 14 to 2 ^ 15 0.5400710975115871
2 ^ 15 to 2 ^ 16 0.5387840205842557
2 ^ 16 to 2 ^ 17 0.5370436569998659
2 ^ 17 to 2 ^ 18 0.5360200678115611
2 ^ 18 to 2 ^ 19 0.5346454310781124
2 ^ 19 to 2 ^ 20 0.5337792299633678
 
Mallows' number = 1489
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6
 
# Task 1
Line 1,647 ⟶ 2,088:
end
 
println("You too might have won \$1000 with the mallows number of ", lastindex(0.55))</langsyntaxhighlight>
 
{{out}}
Line 1,673 ⟶ 2,114:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun main(args: Array<String>) {
Line 1,709 ⟶ 2,150:
}
println("\nMallows' number = $prize")
}</langsyntaxhighlight>
 
{{out}}
Line 1,737 ⟶ 2,178:
 
Mallows' number = 1489
</pre>
 
=={{header|Lua}}==
I solved it, using a coroutine to generate the Hofstadter numbers, because it's fun to do so. This can be done differently, but not with so much fun, I guess. It's counting from the first number 1, the second 1, the third 2 and so on. This doesn't change anything about the outcome, but I guess it's better like this and consistent with such things like fibonacci numbers.
<syntaxhighlight lang="lua">
local fmt, write=string.format,io.write
local hof=coroutine.wrap(function()
local yield=coroutine.yield
local a={1,1}
yield(a[1], 1)
yield(a[2], 2)
local n=a[#a]
repeat
n=a[n]+a[1+#a-n]
a[#a+1]=n
yield(n, #a)
until false
end)
 
local mallows, mdiv=0,0
for p=1,20 do
local max, div, num, last, fdiv=0,0,0,0,0
for i=2^(p-1),2^p-1 do
h,n=hof()
div=h/n
if div>max then
max=div
num=n
end
if div>0.55 then
last=n
fdiv=div
end
end
write(fmt("From 2^%-2d to 2^%-2d the max is %.4f the %6dth Hofstadter number.\n",
p-1, p, max, num))
if max>.55 and p>4 then
mallows, mdiv=last, fdiv
end
end
write("So Mallows number is ", mallows, " with ", fmt("%.4f",mdiv), ", yay, just wire me my $10000 now!\n")
</syntaxhighlight>
{{out}}
<pre>
From 2^0 to 2^1 the max is 1.0000 the 1th Hofstadter number.
From 2^1 to 2^2 the max is 0.6667 the 3th Hofstadter number.
From 2^2 to 2^3 the max is 0.6667 the 6th Hofstadter number.
From 2^3 to 2^4 the max is 0.6364 the 11th Hofstadter number.
From 2^4 to 2^5 the max is 0.6087 the 23th Hofstadter number.
From 2^5 to 2^6 the max is 0.5909 the 44th Hofstadter number.
From 2^6 to 2^7 the max is 0.5761 the 92th Hofstadter number.
From 2^7 to 2^8 the max is 0.5674 the 178th Hofstadter number.
From 2^8 to 2^9 the max is 0.5595 the 370th Hofstadter number.
From 2^9 to 2^10 the max is 0.5549 the 719th Hofstadter number.
From 2^10 to 2^11 the max is 0.5501 the 1487th Hofstadter number.
From 2^11 to 2^12 the max is 0.5475 the 2897th Hofstadter number.
From 2^12 to 2^13 the max is 0.5441 the 5969th Hofstadter number.
From 2^13 to 2^14 the max is 0.5424 the 11651th Hofstadter number.
From 2^14 to 2^15 the max is 0.5401 the 22223th Hofstadter number.
From 2^15 to 2^16 the max is 0.5388 the 45083th Hofstadter number.
From 2^16 to 2^17 the max is 0.5370 the 89516th Hofstadter number.
From 2^17 to 2^18 the max is 0.5360 the 181385th Hofstadter number.
From 2^18 to 2^19 the max is 0.5346 the 353683th Hofstadter number.
From 2^19 to 2^20 the max is 0.5338 the 722589th Hofstadter number.
So Mallows number is 1489 with 0.5500, yay, just wire me my $10000 now!
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">a[1] := 1; a[2] := 1;
a[n_] := a[n] = a[a[n-1]] + a[n-a[n-1]]
 
Map[Print["Max value: ",Max[Table[a[n]/n//N,{n,2^#,2^(#+1)}]]," for n between 2^",#," and 2^",(#+1)]& , Range[19]]
n=2^20; While[(a[n]/n//N)<0.55,n--]; Print["Mallows number: ",n]</langsyntaxhighlight>
 
Outputs:
Line 1,770 ⟶ 2,276:
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight lang="matlab"> function Q = HCsequence(N)
Q = zeros(1,N);
Q(1:2) = 1;
Line 1,776 ⟶ 2,282:
Q(n) = Q(Q(n-1))+Q(n-Q(n-1));
end;
end;</langsyntaxhighlight>
 
The function can be tested in this way:
<langsyntaxhighlight lang="matlab">NN = 20;
Q = HCsequence(2^NN+1);
V = Q./(1:2^NN);
Line 1,786 ⟶ 2,292:
i = i + 2^k - 1;
printf('Maximum between 2^%i and 2^%i is %f at n=%i\n',k,k+1,m,i);
end; </langsyntaxhighlight>
 
Output:
Line 1,811 ⟶ 2,317:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">import strutils
 
const last = 1 shl 20
Line 1,831 ⟶ 2,337:
aMax = 0
inc lg2
k1 = n</langsyntaxhighlight>
Output:
<pre>Maximum between 2^1 and 2^2 was 0.6666666666666666
Line 1,842 ⟶ 2,348:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class HofCon {
function : Main(args : String[]) ~ Nil {
Line 1,881 ⟶ 2,387:
}
}
</syntaxhighlight>
</lang>
 
<pre>
Line 1,904 ⟶ 2,410:
Maximum between 2^19 and 2^20 was 0.53377923
</pre>
 
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: hofstadter(n)
| l i |
ListBuffer newSize(n) dup add(1) dup add(1) ->l
Line 1,925 ⟶ 2,430:
"Mallows number ==>" . h reverse detect(#[ first 0.55 >= ], true) println
; </langsyntaxhighlight>
 
{{out}}
Line 1,953 ⟶ 2,458:
=={{header|Oz}}==
A direct implementation of the recursive definition with explicit memoization using a mutable map (dictionary):
<langsyntaxhighlight lang="oz">declare
local
Cache = {Dictionary.new}
Line 1,979 ⟶ 2,484:
in
{System.showInfo "Max. between 2^"#I#" and 2^"#I+1#": "#Maximum}
end</langsyntaxhighlight>
 
Output:
Line 2,006 ⟶ 2,511:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">HC(n)=my(a=vectorsmall(n));a[1]=a[2]=1;for(i=3,n,a[i]=a[a[i-1]]+a[i-a[i-1]]);a;
maxima(n)=my(a=HC(1<<n),m);vector(n-1,k,m=0;for(i=1<<k+1,1<<(k+1)-1,m=max(m,a[i]/i));m);
forstep(i=#a,1,-1,if(a[i]/i>=.55,return(i)))</langsyntaxhighlight>
Output:
<pre>%1 = [2/3, 2/3, 7/11, 14/23, 13/22, 53/92, 101/178, 207/370, 399/719, 818/1487, 1586/2897, 3248/5969, 6320/11651, 12002/22223, 24290/45083, 24037/44758, 97226/181385, 189095/353683, 385703/722589]
%2 = 1489</pre>
 
=={{header|Pascal}}==
tested with freepascal 3.1.1 64 Bit.
<langsyntaxhighlight lang="pascal">
program HofStadterConway;
const
Line 2,134 ⟶ 2,640:
writeln('Mallows number with limit ',l:10:8,' at ',SearchLastPos(a,l));
freemem(p);
end.</langsyntaxhighlight>
;output:
<pre>
Line 2,153 ⟶ 2,659:
 
=={{header|Perl}}==
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
use warnings ;
use strict ;
Line 2,178 ⟶ 2,684:
}
print "The prize would have been won at $mallows !\n"
</syntaxhighlight>
</lang>
Output:
<pre>Between 2 ^ 1 and 2 ^ 2 the maximum value is 0.666666666666667 at 3 !
Line 2,202 ⟶ 2,708:
</pre>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{works with|Rakudo|2015-09-14}}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Here is a list-oriented version. Note that <tt>@a</tt> is a lazy array, and the Z variants are "zipwith" operators.
<span style="color: #004080;">sequence</span> <span style="color: #000000;">a</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;">1</span><span style="color: #0000FF;">}</span>
<lang perl6>my $n = 3;
my @a = (0,1,1, -> $p { @a[$p] + @a[$n++ - $p] } ... *);
<span style="color: #008080;">function</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
my $last55;
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]]+</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]]</span>
for 1..19 -> $power {
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
my @range := 2**$power .. 2**($power+1)-1;
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span>
my @ratios = (@a[@range] Z/ @range) Z=> @range;
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
my $max = @ratios.max;
($last55 = .value if .key >= .55 for @ratios) if $max.key >= .55;
<span style="color: #004080;">integer</span> <span style="color: #000000;">mallows</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;">max_n</span>
say $power.fmt('%2d'), @range.min.fmt("%10d"), '..', @range.max.fmt("%-10d"),
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">19</span> <span style="color: #008080;">do</span>
$max.key, ' at ', $max.value;
<span style="color: #004080;">atom</span> <span style="color: #000000;">max_an</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.5</span>
}
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span>
say "Mallows' number would appear to be ", $last55;</lang>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span> <span style="color: #008080;">to</span> <span style="color: #000000;">h</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">an</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">n</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">an</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">max_an</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">max_an</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">an</span>
<span style="color: #000000;">max_n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">an</span><span style="color: #0000FF;">></span><span style="color: #000000;">0.55</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">mallows</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</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: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Maximum in range %6d to %-7d occurs at %6d: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">max_n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">max_an</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Mallows number is %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mallows</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
In this particular case the for loop of q() only ever iterates 0 or 1 times.
{{out}}
<pre>
<lang> 1 2..3 0.666666666666667 at 3
2Maximum in range 1 4..7to 2 0.666666666666667occurs at 6 1: 1.000000
3Maximum in range 2 8..15to 4 0.636363636363636occurs at 11 3: 0.666667
4Maximum in range 16..314 to 8 0.608695652173913 occurs at 23 6: 0.666667
5Maximum in range 32..638 to 16 0.590909090909091occurs at 44 11: 0.636364
6Maximum in range 16 64..127to 32 0.576086956521739occurs at 92 23: 0.608696
7Maximum in range 128..25532 to 64 0.567415730337079 occurs at 178 44: 0.590909
8Maximum in range 256..51164 to 128 0.559459459459459occurs at 370 92: 0.576087
9Maximum in range 128 512..1023to 256 0.554937413073713occurs at 719 178: 0.567416
10Maximum in range 1024..2047256 to 512 0.550100874243443 occurs at 1487 370: 0.559459
11Maximum in range 2048..4095512 to 1024 0.547462892647566occurs at 2897 719: 0.554937
Maximum in range 1024 to 2048 occurs at 1487: 0.550101
12 4096..8191 0.544144747863964 at 5969
Maximum in range 2048 to 4096 occurs at 2897: 0.547463
13 8192..16383 0.542442708780362 at 11651
Maximum in range 4096 to 8192 occurs at 5969: 0.544145
14 16384..32767 0.540071097511587 at 22223
Maximum in range 8192 to 16384 occurs at 11651: 0.542443
15 32768..65535 0.538784020584256 at 45083
Maximum in range 16384 to 32768 occurs at 22223: 0.540071
16 65536..131071 0.537043656999866 at 89516
Maximum in range 32768 to 65536 occurs at 45083: 0.538784
17 131072..262143 0.536020067811561 at 181385
Maximum in range 65536 to 131072 occurs at 89516: 0.537044
18 262144..524287 0.534645431078112 at 353683
Maximum in range 131072 to 262144 occurs at 181385: 0.536020
19 524288..1048575 0.533779229963368 at 722589
Maximum in range 262144 to 524288 occurs at 353683: 0.534645
Mallows' number would appear to be 1489</lang>
Maximum in range 524288 to 1048576 occurs at 722589: 0.533779
Mallows number is 1489
</pre>
 
=={{header|Picat}}==
The lists are convenient, but here is a version written in relatively low-level primitives for performance:
<syntaxhighlight lang="picat">go =>
<lang perl6>my int $POW = 20;
foreach(N in 0..19)
my int $top = 2**$POW;
[Val,Ix] = argmax({a(I)/I : I in 2**N..2**(N+1)}),
printf("Max from 2^%2d..2^%-2d is %0.8f at %d\n",N,N+1,Val,Ix+2**N-1)
my int @a = (0,1,1);
end,
@a[$top] = 0; # pre-extend array
println(mallows_number=mallows_number()),
my int $n = 3;nl.
 
my int $p = 1;
% The sequence definition
table
loop ($n = 3; $n <= $top; ++$n) {
a(1) = 1.
@a[$n] = $p = @a[$p] + @a[$n - $p];
a(2) = 1.
}
a(N) = a(a(N-1))+a(N-a(N-1)).
 
my int $last55;
% argmax: find the (first) index for the max value(s) of L.
for 1 ..^ $POW -> int $power {
argmax(L) = [Max,MaxIxFirst] =>
Max = max(L),
my int $beg = 2 ** $power;
MaxIxFirst = {I : I in 1..L.length, L[I] == Max}.first.
my int $end = $beg * 2 - 1;
 
my $max;
% Calculate the Mallows number separately.
my $ratio;
mallows_number() = Mallow =>
loop (my $nMallow = $beg; $n <=_, $end; ++$n) {
foreach(M in 1..19)
my $ratio = @a[$n] / $n;
$last55Min = $n if $ratio 2* 100 >= 55;*M,
$max max= $ratioMax => $n;Min*2,
} MaxRatio = 0,
NVal = 0,
foreach(N in Min..Max)
say $power.fmt('%2d'), $beg.fmt("%10d"), '..', $end.fmt("%-10d"), $max.key, " at ", $max.value;
Ratio = a(N)/N,
}
if Ratio > MaxRatio then
say "Mallows' number would appear to be ", $last55;</lang>
MaxRatio := Ratio,
NVal := N
end,
if Ratio > 0.55 then
Mallow := N
end
end
end.</syntaxhighlight>
 
{{out}}
<pre>Max from 2^ 0..2^1 is 1.00000000 at 1
Max from 2^ 1..2^2 is 0.66666667 at 3
Max from 2^ 2..2^3 is 0.66666667 at 6
Max from 2^ 3..2^4 is 0.63636364 at 11
Max from 2^ 4..2^5 is 0.60869565 at 23
Max from 2^ 5..2^6 is 0.59090909 at 44
Max from 2^ 6..2^7 is 0.57608696 at 92
Max from 2^ 7..2^8 is 0.56741573 at 178
Max from 2^ 8..2^9 is 0.55945946 at 370
Max from 2^ 9..2^10 is 0.55493741 at 719
Max from 2^10..2^11 is 0.55010087 at 1487
Max from 2^11..2^12 is 0.54746289 at 2897
Max from 2^12..2^13 is 0.54414475 at 5969
Max from 2^13..2^14 is 0.54244271 at 11651
Max from 2^14..2^15 is 0.54007110 at 22223
Max from 2^15..2^16 is 0.53878402 at 45083
Max from 2^16..2^17 is 0.53704366 at 89516
Max from 2^17..2^18 is 0.53602007 at 181385
Max from 2^18..2^19 is 0.53464543 at 353683
Max from 2^19..2^20 is 0.53377923 at 722589
mallows_number = 1489</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de hofcon (N)
(cache '(NIL) N
(if (>= 2 N)
Line 2,301 ⟶ 2,856:
" (the task requests 'n > p' now)" ) ) )
 
(sequence (** 2 20))</langsyntaxhighlight>
Output:
<pre>Maximum between 2 and 4 was 0.66666666666666666667
Line 2,325 ⟶ 2,880:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* First part: */
 
Line 2,334 ⟶ 2,889:
L(i) = L(i-k) + L(1+k);
end;
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>If OpenConsole()
Define.i upperlim, i=1, k1=2, n=3, v=1
Define.d Maximum
Print("Enter limit (ENTER gives 2^20=1048576): "): upperlim=Val(Input())
If upperlim<=0: upperlim=1048576: EndIf
Dim tal(upperlim)
If ArraySize(tal())=-1
PrintN("Could not allocate needed memory!"): Input(): End
EndIf
tal(1)=1: tal(2)=1
While n<=upperlim
v=tal(v)+tal(n-v)
tal(n)=v
If Maximum<(v/n): Maximum=v/n: EndIf
If Not n&k1
PrintN("Maximum between 2^"+Str(i)+" and 2^"+Str(i+1)+" was "+StrD(Maximum,6))
Maximum=0.0
i+1
EndIf
k1=n
n+1
Wend
Print(#CRLF$+"Press ENTER to exit."): Input()
CloseConsole()
EndIf</lang>
 
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">from __future__ import division
 
def maxandmallows(nmaxpower2):
Line 2,397 ⟶ 2,924:
if mallows:
print("\nYou too might have won $1000 with the mallows number of %i" % mallows)
</syntaxhighlight>
</lang>
 
''Sample output''
Line 2,423 ⟶ 2,950:
You too might have won $1000 with the mallows number of 1489</pre>
If you don't create enough terms in the sequence, no mallows number is returned.
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ ' [ 1 1 ]
swap 2 - times
[ dup -1 peek 2dup 1 - peek
dip [ dip dup negate peek ]
+ join ] ] is hof-con ( n --> [ )
 
[ behead do rot
witheach
[ do 2over 2over v- v0<
if 2swap
2drop ] ] is maximum ( [ --> n n )
20 dup bit 1 - hof-con
[] swap witheach
[ i^ 1+ join nested join ]
swap 1 - bit times
[ i^ 1+ dup 2dup
say "Maximum in range " echo
say " to " 1 << 1 - echo
step split swap maximum
say " at n = "
dup echo
say " is " 10 point$ echo$ cr ]
drop</syntaxhighlight>
 
{{out}}
 
<pre>Maximum in range 1 to 1 at n = 1 is 1
Maximum in range 2 to 3 at n = 3 is 0.6666666667
Maximum in range 4 to 7 at n = 6 is 0.6666666667
Maximum in range 8 to 15 at n = 11 is 0.6363636364
Maximum in range 16 to 31 at n = 23 is 0.6086956522
Maximum in range 32 to 64 at n = 44 is 0.5909090909
Maximum in range 64 to 127 at n = 92 is 0.5760869565
Maximum in range 128 to 255 at n = 178 is 0.5674157303
Maximum in range 256 to 511 at n = 370 is 0.5594594595
Maximum in range 512 to 1023 at n = 719 is 0.5549374131
Maximum in range 1024 to 2047 at n = 1487 is 0.5501008742
Maximum in range 2048 to 4095 at n = 2897 is 0.5474628926
Maximum in range 4096 to 8191 at n = 5969 is 0.5441447479
Maximum in range 8192 to 16383 at n = 11651 is 0.5424427088
Maximum in range 16384 to 32767 at n = 22223 is 0.5400710975
Maximum in range 32768 to 65535 at n = 45083 is 0.5387840206
Maximum in range 65536 to 131071 at n = 89516 is 0.537043657
Maximum in range 131072 to 262143 at n = 181385 is 0.5360200678
Maximum in range 262144 to 524287 at n = 353683 is 0.5346454311
Maximum in range 524288 to 1048575 at n = 722589 is 0.53377923
</pre>
 
=={{header|R}}==
A memoizing function to compute individual elements of the sequence could be written like this:
 
<langsyntaxhighlight lang="r">f = local(
{a = c(1, 1)
function(n)
{if (is.na(a[n]))
a[n] <<- f(f(n - 1)) + f(n - f(n - 1))
a[n]}})</langsyntaxhighlight>
 
But a more straightforward way to get the local maxima and the Mallows point is to begin by generating as much of the sequence as we need.
 
<langsyntaxhighlight lang="r">hofcon = c(1, 1, rep(NA, 2^20 - 2))
for (n in 3 : (2^20))
{hofcon[n] =
hofcon[hofcon[n - 1]] +
hofcon[n - hofcon[n - 1]]}</langsyntaxhighlight>
 
We can now quickly finish the task with vectorized operations.
 
<langsyntaxhighlight lang="r">ratios = hofcon / seq_along(hofcon)
 
message("Maxima:")
Line 2,451 ⟶ 3,031:
 
message("Prize-winning point:")
print(max(which(ratios >= .55)))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,467 ⟶ 3,047:
The macro define/memoize1 creates an 1-argument procedure and handles all the details about memorization. We use it to define (conway n) as a transcription of the definition.
 
<langsyntaxhighlight Racketlang="racket">#lang racket/base
 
(define-syntax-rule (define/memoize1 (proc x) body ...)
Line 2,480 ⟶ 3,060:
1
(+ (conway (conway (sub1 n)))
(conway (- n (conway (sub1 n)))))))</langsyntaxhighlight>
 
The macro for/max1 is like for, but the result is the maximum of the values produced by the body. The result also includes the position of the maximum in the sequence. We use this to find the maximum in each power-of-2-sector.
<langsyntaxhighlight Racketlang="racket">(define-syntax-rule (for/max1 ([i sequence]) body ...)
(for/fold ([max -inf.0] [arg-max #f]) ([i sequence])
(define val (begin body ...))
Line 2,495 ⟶ 3,075:
(define-values (max arg-max) (for/max1 ([k (in-range low-b up-b)])
(/ (conway k) k)))
(printf "Max. between 2^~a and 2^~a is ~a at ~a ~n" i (add1 i) (real->decimal-string max 5) arg-max))</langsyntaxhighlight>
 
 
The macro for/prev is like for/and, it stops when it finds the first #f, but the result is previous value produced by the body. We use this to find the first power-of-2-sector that has no ratio avobe .55. The previous result is the Mallows number.
<langsyntaxhighlight Racketlang="racket">(define-syntax-rule (for/prev (sequences ...) body ...)
(for/fold ([prev #f]) (sequences ...)
(define val (let () body ...))
Line 2,512 ⟶ 3,092:
k)))
 
(printf "Mallows number: ~a~n" mallows)</langsyntaxhighlight>
 
'''Sample Output:'''
Line 2,536 ⟶ 3,116:
Max. between 2^19 and 2^20 is 0.53378 at 722589
Mallows number: 1489</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.09}}
Note that <tt>@a</tt> is a lazy array, and the Z variants are "zipwith" operators.
<syntaxhighlight lang="raku" line>my $n = 3;
my @a = (0,1,1, -> $p { @a[$p] + @a[$n++ - $p] } ... *);
@a[2**20]; # pre-calculate sequence
 
my $last55;
for 1..19 -> $power {
my @range := 2**$power .. 2**($power+1)-1;
my @ratios = (@a[@range] Z/ @range) Z=> @range;
my $max = @ratios.max;
($last55 = .value if .key >= .55 for @ratios) if $max.key >= .55;
say $power.fmt('%2d'), @range.min.fmt("%10d"), '..', @range.max.fmt("%-10d"),
$max.key, ' at ', $max.value;
}
say "Mallows' number would appear to be ", $last55;</syntaxhighlight>
{{out}}
<pre> 1 2..3 0.666666666666667 at 3
2 4..7 0.666666666666667 at 6
3 8..15 0.636363636363636 at 11
4 16..31 0.608695652173913 at 23
5 32..63 0.590909090909091 at 44
6 64..127 0.576086956521739 at 92
7 128..255 0.567415730337079 at 178
8 256..511 0.559459459459459 at 370
9 512..1023 0.554937413073713 at 719
10 1024..2047 0.550100874243443 at 1487
11 2048..4095 0.547462892647566 at 2897
12 4096..8191 0.544144747863964 at 5969
13 8192..16383 0.542442708780362 at 11651
14 16384..32767 0.540071097511587 at 22223
15 32768..65535 0.538784020584256 at 45083
16 65536..131071 0.537043656999866 at 89516
17 131072..262143 0.536020067811561 at 181385
18 262144..524287 0.534645431078112 at 353683
19 524288..1048575 0.533779229963368 at 722589
Mallows' number would appear to be 1489</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program solves the Hofstadter─Conway sequence $10,000 prize (puzzle). */
@pref= 'Maximum of a(n) ÷ n between ' /*a prologue for the text of message. */
H.=.; H.1=1; H.2=1; !.=0; @.=0 /*initialize some REXX variables. */
Line 2,560 ⟶ 3,180:
H: procedure expose H.; parse arg z
if H.z==. then do; m=z-1; $=H.m; _=z-$; H.z=H.$+H._; end
return H.z</langsyntaxhighlight>
'''output'''
<pre>
Line 2,588 ⟶ 3,208:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(9)
size = 15
Line 2,610 ⟶ 3,230:
next
see "mallows number is : " + mallows + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,631 ⟶ 3,251:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class HofstadterConway10000
def initialize
@sequence = [nil, 1, 1]
Line 2,659 ⟶ 3,279:
end
 
puts "the mallows number is #{mallows}"</langsyntaxhighlight>
 
{{out}}
Line 2,685 ⟶ 3,305:
</pre>
 
=={{header|Run BASICRust}}==
<syntaxhighlight lang="rust">
<lang runbasic>input "Enter upper limit between 1 and 20 (ENTER 20 gives 2^20):"); uprLim
struct HofstadterConway {
if uprLim < 1 or uprLim > 20 then uprLim = 20
current: usize,
dim a(2^uprLim)
sequence: Vec<usize>,
a(1) = 1
}
a(2) = 1
 
pow2 = 2
impl HofstadterConway {
p2 = 2^pow2
/// Define a constructor for the struct.
p = 0.5
fn new() -> HofstadterConway {
pPos = 0
HofstadterConway {
for n = 3 TO 2^uprLim
current: 0,
a(n) = a(a(n-1)) + a(n-a(n-1))
sequence: vec![1, 1],
r = a(n)/n
if r >= 0.55 THEN Mallows = n}
if r > p THEN }
}
p = r
 
pPos = n
impl Default for HofstadterConway {
end if
if n =fn p2default() THEN-> Self {
Self::new()
print "Maximum between";chr$(9);" 2^";pow2-1;" and 2^";pow2;chr$(9);" is ";p;chr$(9);" at n = ";pPos
}
pow2 = pow2 + 1
}
p2 = 2^pow2
 
p = 0.5
/// Implement the hofstadter q iteration sequence.
end IF
impl Iterator for HofstadterConway {
next n
type Item = usize;
print "Mallows number is ";Mallows</lang>
 
<pre>Enter upper limit between 1 and 20 (ENTER 20 gives 2^20): ?20
/// This gets called to fetch the next item of the iterator.
Maximum between 2^1 and 2^2 is 0.666666698 at n = 3
fn next(&mut self) -> Option<usize> {
Maximum between 2^2 and 2^3 is 0.666666698 at n = 6
let max_index = self.sequence.len() - 1;
Maximum between 2^3 and 2^4 is 0.636363601 at n = 11
let last_value = self.sequence[max_index];
Maximum between 2^4 and 2^5 is 0.608695602 at n = 23
 
Maximum between 2^5 and 2^6 is 0.590909051 at n = 44
if self.current > max_index {
Maximum between 2^6 and 2^7 is 0.57608695 at n = 92
let new_x = self.sequence[last_value - 1] + self.sequence[max_index - last_value + 1];
Maximum between 2^7 and 2^8 is 0.567415714 at n = 178
self.sequence.push(new_x);
Maximum between 2^8 and 2^9 is 0.559459447 at n = 370
}
Maximum between 2^9 and 2^10 is 0.55493741 at n = 719
self.current += 1;
Maximum between 2^10 and 2^11 is 0.550100851 at n = 1487
Some(self.sequence[self.current - 1])
Maximum between 2^11 and 2^12 is 0.547462892 at n = 2897
}
Maximum between 2^12 and 2^13 is 0.544144725 at n = 5969
}
Maximum between 2^13 and 2^14 is 0.542442655 at n = 11651
 
Maximum between 2^14 and 2^15 is 0.540071058 at n = 22223
#[allow(clippy::cast_precision_loss)]
Maximum between 2^15 and 2^16 is 0.538784027 at n = 45083
fn main() {
Maximum between 2^16 and 2^17 is 0.537043619 at n = 89516
let mut hof = HofstadterConway::new();
Maximum between 2^17 and 2^18 is 0.53602004 at n = 181385
let mut winning_num = 0_usize;
Maximum between 2^18 and 2^19 is 0.534645414 at n = 353683
 
Maximum between 2^19 and 2^20 is 0.533779191 at n = 722589
for p in 0..20 {
Mallows number is 1489</pre>
let max_hof = (2_usize.pow(p)..2_usize.pow(p + 1))
.map(|n| (n, hof.next().unwrap() as f64 / n as f64))
.fold(f64::NAN, |a, (n, b)| {
if b >= 0.55 {
winning_num = n;
}
a.max(b)
});
 
println!("2^{:>2}-2^{:>2}, {:>.8}", p, p + 1, max_hof);
}
 
println!("Winning number: {}", winning_num);
}
</syntaxhighlight>
{{out}}
<pre>
2^ 0-2^ 1, 1.00000000
2^ 1-2^ 2, 0.66666667
2^ 2-2^ 3, 0.66666667
2^ 3-2^ 4, 0.63636364
2^ 4-2^ 5, 0.60869565
2^ 5-2^ 6, 0.59090909
2^ 6-2^ 7, 0.57608696
2^ 7-2^ 8, 0.56741573
2^ 8-2^ 9, 0.55945946
2^ 9-2^10, 0.55493741
2^10-2^11, 0.55010087
2^11-2^12, 0.54746289
2^12-2^13, 0.54414475
2^13-2^14, 0.54244271
2^14-2^15, 0.54007110
2^15-2^16, 0.53878402
2^16-2^17, 0.53704366
2^17-2^18, 0.53602007
2^18-2^19, 0.53464543
2^19-2^20, 0.53377923
 
Winning number: 1489
</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object HofstadterConway {
def pow2(n: Int): Int = (Iterator.fill(n)(2)).product
Line 2,762 ⟶ 3,422:
println("Mallow's number = %s".format(mallowsNumber))
}
}</langsyntaxhighlight>
'''Output'''
<pre>Maximum of a(n)/n between 2^1 and 2^2 was 0.6666666666666666 at 3
Line 2,787 ⟶ 3,447:
=={{header|Scheme}}==
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
Line 2,831 ⟶ 3,491:
0.55))
(display (string-append "\np=" (number->string idx) "\n"))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,860 ⟶ 3,520:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">class HofstadterConway10000 {
has sequence = [nil, 1, 1]
method term(n {.is_pos}) {
Line 2,883 ⟶ 3,543:
}
 
say "the mallows number is #{mallows}"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,910 ⟶ 3,570:
=={{header|Swift}}==
{{trans|Java}}
<langsyntaxhighlight Swiftlang="swift">func doSqnc(m:Int) {
var aList = [Int](count: m + 1, repeatedValue: 0)
var k1 = 2
Line 2,938 ⟶ 3,598:
}
 
doSqnc(1 << 20)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,964 ⟶ 3,624:
=={{header|Tcl}}==
The routine to return the ''n''<sup>th</sup> member of the sequence.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
set hofcon10k {1 1}
Line 2,981 ⟶ 3,641:
}
return $c
}</langsyntaxhighlight>
The code to explore the sequence, looking for maxima in the ratio.
<langsyntaxhighlight lang="tcl">for {set p 1} {$p<20} {incr p} {
set end [expr {2**($p+1)}]
set maxI 0; set maxV 0
Line 2,991 ⟶ 3,651:
}
puts "max in 2**$p..2**[expr {$p+1}] at $maxI : $maxV"
}</langsyntaxhighlight>
Output:
<pre>
Line 3,013 ⟶ 3,673:
max in 2**18..2**19 at 353683 : 0.5346454310781124
max in 2**19..2**20 at 722589 : 0.5337792299633678
</pre>
 
=={{header|VBA}}==
{{trans|Phix}} Function q rewritten to sub.
<syntaxhighlight lang="vb">Public q() As Long
Sub make_q()
ReDim q(2 ^ 20)
q(1) = 1
q(2) = 1
Dim l As Long
For l = 3 To 2 ^ 20
q(l) = q(q(l - 1)) + q(l - q(l - 1))
Next l
End Sub
Public Sub hcsequence()
Dim mallows As Long: mallows = -1
Dim max_n As Long, n As Long
Dim l As Long, h As Long
make_q
For p = 0 To 19
max_an = 0.5
l = 2 ^ p: h = l * 2
For n = l To h
an = q(n) / n
If an >= max_an Then
max_an = an
max_n = n
End If
If an > 0.55 Then
mallows = n
End If
Next n
Debug.Print "Maximum in range"; Format(l, "@@@@@@@"); " to"; h; String$(7 - Len(CStr(h)), " ");
Debug.Print "occurs at"; Format(max_n, "@@@@@@@"); ": "; Format(max_an, "0.000000")
Next p
Debug.Print "Mallows number is"; mallows
End Sub</syntaxhighlight>{{out}}
<pre>Maximum in range 1 to 2 occurs at 1: 1,000000
Maximum in range 2 to 4 occurs at 3: 0,666667
Maximum in range 4 to 8 occurs at 6: 0,666667
Maximum in range 8 to 16 occurs at 11: 0,636364
Maximum in range 16 to 32 occurs at 23: 0,608696
Maximum in range 32 to 64 occurs at 44: 0,590909
Maximum in range 64 to 128 occurs at 92: 0,576087
Maximum in range 128 to 256 occurs at 178: 0,567416
Maximum in range 256 to 512 occurs at 370: 0,559459
Maximum in range 512 to 1024 occurs at 719: 0,554937
Maximum in range 1024 to 2048 occurs at 1487: 0,550101
Maximum in range 2048 to 4096 occurs at 2897: 0,547463
Maximum in range 4096 to 8192 occurs at 5969: 0,544145
Maximum in range 8192 to 16384 occurs at 11651: 0,542443
Maximum in range 16384 to 32768 occurs at 22223: 0,540071
Maximum in range 32768 to 65536 occurs at 45083: 0,538784
Maximum in range 65536 to 131072 occurs at 89516: 0,537044
Maximum in range 131072 to 262144 occurs at 181385: 0,536020
Maximum in range 262144 to 524288 occurs at 353683: 0,534645
Maximum in range 524288 to 1048576 occurs at 722589: 0,533779
Mallows number is 1489 </pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn main() {
mut a := [0, 1, 1] // ignore 0 element. work 1 based.
mut x := 1 // last number in list
mut n := 2 // index of last number in list = a.len-1
mut mallow := 0
for p in 1..20 {
mut max := 0.0
for next_pot := n*2; n < next_pot; {
n = a.len // advance n
x = a[x]+a[n-x]
a << x
f := f64(x)/f64(n)
if f > max {
max = f
}
if f >= .55 {
mallow = n
}
}
println("max between 2^$p and 2^${p+1} was ${max:.6}")
}
println("winning number $mallow")
}</syntaxhighlight>
{{out}}
<pre>
max between 2^1 and 2^2 was 0.666667
max between 2^2 and 2^3 was 0.666667
max between 2^3 and 2^4 was 0.636364
max between 2^4 and 2^5 was 0.608696
max between 2^5 and 2^6 was 0.590909
max between 2^6 and 2^7 was 0.576087
max between 2^7 and 2^8 was 0.567416
max between 2^8 and 2^9 was 0.559459
max between 2^9 and 2^10 was 0.554937
max between 2^10 and 2^11 was 0.550101
max between 2^11 and 2^12 was 0.547463
max between 2^12 and 2^13 was 0.544145
max between 2^13 and 2^14 was 0.542443
max between 2^14 and 2^15 was 0.540071
max between 2^15 and 2^16 was 0.538784
max between 2^16 and 2^17 was 0.537044
max between 2^17 and 2^18 was 0.536020
max between 2^18 and 2^19 was 0.534645
max between 2^19 and 2^20 was 0.533779
winning number 1489
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var limit = 1<<20 + 1
var a = List.filled(limit, 0)
a[1] = 1
a[2] = 1
for (n in 3...limit) {
var p = a[n-1]
a[n] = a[p] + a[n-p]
}
 
System.print(" Range Maximum")
System.print("---------------- --------")
var pow2 = 1
var p = 1
var max = a[1]
for (n in 2...limit) {
var r = a[n] / n
if (r > max) max = r
if (n == pow2 * 2) {
Fmt.print("2 ^ $2d to 2 ^ $2d $f", p - 1, p, max)
pow2 = pow2 * 2
p = p + 1
max = r
}
}
 
var prize = 0
for (n in limit-1..1) {
if (a[n]/n >= 0.55) {
prize = n
break
}
}
System.print("\nMallows' number = %(prize)")</syntaxhighlight>
 
{{out}}
<pre>
Range Maximum
---------------- --------
2 ^ 0 to 2 ^ 1 1.000000
2 ^ 1 to 2 ^ 2 0.666667
2 ^ 2 to 2 ^ 3 0.666667
2 ^ 3 to 2 ^ 4 0.636364
2 ^ 4 to 2 ^ 5 0.608696
2 ^ 5 to 2 ^ 6 0.590909
2 ^ 6 to 2 ^ 7 0.576087
2 ^ 7 to 2 ^ 8 0.567416
2 ^ 8 to 2 ^ 9 0.559459
2 ^ 9 to 2 ^ 10 0.554937
2 ^ 10 to 2 ^ 11 0.550101
2 ^ 11 to 2 ^ 12 0.547463
2 ^ 12 to 2 ^ 13 0.544145
2 ^ 13 to 2 ^ 14 0.542443
2 ^ 14 to 2 ^ 15 0.540071
2 ^ 15 to 2 ^ 16 0.538784
2 ^ 16 to 2 ^ 17 0.537044
2 ^ 17 to 2 ^ 18 0.536020
2 ^ 18 to 2 ^ 19 0.534645
2 ^ 19 to 2 ^ 20 0.533779
 
Mallows' number = 1489
</pre>
 
=={{header|X86 Assembly}}==
Using FASM syntax.
<langsyntaxhighlight lang="asm">; Hofstadter-Conway $10,000 sequence
call a.memorization
call Mallows_Number
Line 3,052 ⟶ 3,886:
retn
 
a rd 1 shl 20</langsyntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">int A(1 + 1<<20), N, Power2, WinningN;
real Max, Member;
[A(1):= 1; A(2):= 1;
N:= 3; Power2:= 2; Max:= 0.;
Text(0, " Range Maximum^m^j");
Format(1, 6);
repeat A(N):= A(A(N-1)) + A(N-A(N-1));
Member:= float(A(N)) / float(N);
if Member >= Max then Max:= Member;
if Member >= 0.55 then WinningN:= N;
if N & 1<<Power2 then
[Text(0, "2^^"); IntOut(0, Power2-1);
Text(0, " to 2^^"); IntOut(0, Power2);
ChOut(0, 9\tab\);
RlOut(0, Max);
CrLf(0);
Power2:= Power2+1;
Max:= 0.;
];
N:= N+1;
until N > 1<<20;
IntOut(0, WinningN);
Text(0, " is the winning position.^m^j");
]</syntaxhighlight>
{{out}}
<pre>
Range Maximum
2^1 to 2^2 0.666667
2^2 to 2^3 0.666667
2^3 to 2^4 0.636364
2^4 to 2^5 0.608696
2^5 to 2^6 0.590909
2^6 to 2^7 0.576087
2^7 to 2^8 0.567416
2^8 to 2^9 0.559459
2^9 to 2^10 0.554937
2^10 to 2^11 0.550101
2^11 to 2^12 0.547463
2^12 to 2^13 0.544145
2^13 to 2^14 0.542443
2^14 to 2^15 0.540071
2^15 to 2^16 0.538784
2^16 to 2^17 0.537044
2^17 to 2^18 0.536020
2^18 to 2^19 0.534645
2^19 to 2^20 0.533779
1489 is the winning position.
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn hofstadterConwaySequence(m){
a:=List.createLong(m + 1,0);
a[0]=a[1]=1;
Line 3,074 ⟶ 3,958:
}
hofstadterConwaySequence((2).pow(20));</langsyntaxhighlight>
{{out}}
<pre>
Line 3,098 ⟶ 3,982:
Winning number = 1489
</pre>
 
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
Nine first results.
<lang zxbasic>10 DIM a(2000)
20 LET a(1)=1: LET a(2)=1
30 LET pow2=2: LET p2=2^pow2
40 LET peak=0.5: LET peakpos=0
50 FOR n=3 TO 2000
60 LET a(n)=a(a(n-1))+a(n-a(n-1))
70 LET r=a(n)/n
80 IF r>0.55 THEN LET Mallows=n
90 IF r>peak THEN LET peak=r: LET peakpos=n
100 IF n=p2 THEN PRINT "Maximum (2^";pow2-1;", 2^";pow2;") is ";peak;" at n=";peakpos: LET pow2=pow2+1: LET p2=2^pow2: LET peak=0.5
110 NEXT n
120 PRINT "Mallows number is ";Mallows</lang>
1,983

edits