Hofstadter-Conway $10,000 sequence: Difference between revisions
Content deleted Content added
→{{header|FutureBasic}}: Additional clarifying description for output |
|||
(50 intermediate revisions by 30 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 [
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.
<
HOFSTADT START
B 72(R15) skip savearea
Line 135 ⟶ 171:
A DS 4096F array a(uprdim)
REGEQU
END HOFSTADT</
{{out}}
<pre>
Line 153 ⟶ 189:
=={{header|Ada}}==
<
-- Allocation of arrays on the heap
Line 226 ⟶ 262:
end Conway;
</syntaxhighlight>
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}}
<
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))</
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, p 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}}==
<
CreateLists(2 ** (Max:=20))
Line 364 ⟶ 439:
n_%A_Index% := a_%A_Index% / A_Index
}
}</
Message box shows:
<pre>Maximum between 2^1 and 2^2 is 0.666667 for n = 3
Line 390 ⟶ 465:
Iterative approach:
<
BEGIN {
NN = 20;
Line 411 ⟶ 486:
Q[n] = Q[Q[n-1]]+Q[n-Q[n-1]];
}
} </
Recursive variant:
<
BEGIN {
Q[1] = 1;
Line 454 ⟶ 529:
S[n] = 1;
return (Q[n]);
} </
Output:
Line 478 ⟶ 553:
</pre>
=={{header|
==={{header|BASIC256}}===
<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%</
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}}==
<
=
. !arg:(1|2)&1
Line 580 ⟶ 890:
)
)
)</
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}}==
<
#include <stdlib.h>
Line 633 ⟶ 943:
}
return 1;
}</
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 sharp|C#}}==
{{works with|C#|3.0}}
<
using System;
using System.Linq;
Line 749 ⟶ 1,000:
}
}
</syntaxhighlight>
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}}==
<
(: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</
=={{header|Common Lisp}}==
<
(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)))))</
Sample session:
<pre>ROSETTA> (maxima 20)
Line 867 ⟶ 1,179:
=={{header|D}}==
<
void hofstadterConwaySequence(in int m) {
Line 890 ⟶ 1,202:
void main() {
hofstadterConwaySequence(2 ^^ 20);
}</
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 lang=text>
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}}==
<
(decimals 4)
(cache-size 2000000)
Line 943 ⟶ 1,281:
#:break (> (// (a n) n) 0.55) => n )
)
</syntaxhighlight>
{{out}}
<pre>
Line 969 ⟶ 1,307:
→ 1489 ;; Mallows number
</pre>
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,033 ⟶ 1,370:
end
</syntaxhighlight>
{{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">
-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>
{{out}}
<pre>
Line 1,117 ⟶ 1,454:
=={{header|Euler Math Toolbox}}==
<syntaxhighlight lang="euler math toolbox">
>function hofstadter (n) ...
$v=ones(1,n);
Line 1,145 ⟶ 1,482:
>max(nonzeros(v1>0.55))
1489
</syntaxhighlight>
=={{header|F Sharp|F#}}==
<syntaxhighlight 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
for p = 1 to 19 do
Seq.max [|for i in 1 <<< p .. 1 <<< p+1 -> float a.[i] / float i|]
|> printf "Maximum in %6d..%7d is %g\n" (1 <<< p) (1 <<< p+1)
let mallows, _ = a
|> List.ofSeq
|> List.mapi (fun i n -> i, n)
|> List.rev
|> List.find (fun (i, n) -> float(n) / float(i) > 0.55)
printfn "Mallows number is %d" mallows</syntaxhighlight>
Outputs:
<pre>Maximum in 2.. 4 is 0.666667
Maximum in 4.. 8 is 0.666667
Maximum in 8.. 16 is 0.636364
Maximum in 16.. 32 is 0.608696
Maximum in 32.. 64 is 0.590909
Maximum in 64.. 128 is 0.576087
Maximum in 128.. 256 is 0.567416
Maximum in 256.. 512 is 0.559459
Maximum in 512.. 1024 is 0.554937
Maximum in 1024.. 2048 is 0.550101
Maximum in 2048.. 4096 is 0.547463
Maximum in 4096.. 8192 is 0.544145
Maximum in 8192.. 16384 is 0.542443
Maximum in 16384.. 32768 is 0.540071
Maximum in 32768.. 65536 is 0.538784
Maximum in 65536.. 131072 is 0.537044
Maximum in 131072.. 262144 is 0.53602
Maximum in 262144.. 524288 is 0.534645
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">
program conway
implicit none
Line 1,194 ⟶ 1,610:
end program conway
</syntaxhighlight>
Output:
Line 1,220 ⟶ 1,636:
</pre>
=={{header|
{{FormulaeEntry|page=https://formulae.org/?script=examples/Hofstadter-Conway_%2410%2C000_sequence}}
'''Solution'''
This is the function according to the definition. It is very inefficient:
[[File:Fōrmulæ - Hofstadter–Conway $10,000 sequence 01.png]]
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}}==
<
// Set width of tab
Line 1,302 ⟶ 1,712:
next
print
print "
HandleEvents</syntaxhighlight>
Output:
Line 1,329 ⟶ 1,740:
Maximum of a(n)/n between 2^ 19 and 2^ 20 is 0.53377923 at n = 722589
Dr
</pre>
=={{header|Go}}==
<
import (
Line 1,361 ⟶ 1,772:
}
fmt.Println("winning number", mallow)
}</
Output:
<pre>
Line 1,388 ⟶ 1,799:
=={{header|Haskell}}==
<
import Data.Ord
import Data.Array
Line 1,412 ⟶ 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]</
=={{header|Icon}} and {{header|Unicon}}==
<
m := integer(!args) | 20
nextNum := create put(A := [], 1 | 1 | |A[A[*A]]+A[-A[*A]])[*A]
Line 1,430 ⟶ 1,841:
}
write("Mallows's number is ",\mallows | "NOT found!")
end</
Output:
Line 1,458 ⟶ 1,869:
=={{header|J}}==
'''Solution''' (tacit): <
AnN =: % 1+i.@:# NB. a(n)/n
MxAnN =: >./;.1~ 2 (=<.)@:^. 1+i.@# NB. Maxima of a(n)/n between successive powers of 2</
'''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.
<
expand =: 2+I.@;
tail =: copies&.>^:(<@>:`(<@,@2:))
copies =: >: |.@(#!.1 |.)~ 1 j. #;.1 #^:_1 ::1:~ ]~:{.</
'''Example''': <
1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 ...
AnN A
Line 1,475 ⟶ 1,886:
1 0.666667 0.666667 0.636364 ...
MxAnN@AnN@hc10kE 20
1 0.666667 0.666667 0.636364 ...</
=={{header|Java}}==
Remove translation and provide native Java implementation.
<syntaxhighlight lang="java">
// Title: Hofstadter-Conway $10,000 sequence
public class HofstadterConwaySequence {
private static int MAX = (int) Math.pow(2, 20) + 1;
private static int[]
for ( int
int nm1 = HCS[n - 1];
}
public static void main(String[] args) {
int max = min * 2;
double maxRatio = 0.0;
for ( int n = min ; n <= max ; n ++ ) {
double ratio = (double) HCS[n] / n;
if ( ratio > maxRatio ) {
maxRatio = Math.max(ratio, maxRatio);
nVal = n;
}
if ( ratio >= 0.55 ) {
mNum = n;
}
}
System.out.printf("Max ratio between 2^%d and 2^%d is %f at n = %,d%n", m, m+1, maxRatio, nVal);
}
System.out.printf("Mallow's number is %d.%n", mNum);
}
}
</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}}==
<
var memo = [1, 1];
Line 1,542 ⟶ 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");
}</
Output:
<pre>Maxima between 2^1-2^2 is: 0.6666666666666666
Line 1,553 ⟶ 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}}==
<syntaxhighlight lang="julia"># v0.6
# Task 1
function hofstadterconway(n::Integer)::Vector{Int}
rst = fill(1, n)
for i in 3:n
rst[i] = rst[rst[i - 1]] + rst[i - rst[i - 1]]
end
return rst
end
function hcfraction(n::Integer)::Vector{Float64}
rst = Array{Float64}(n)
for (i, a) in enumerate(hofstadterconway(n))
rst[i] = abs(a) / i
end
return rst
end
# Task 2
seq = hcfraction(2 ^ 20)
for i in 1:20
a, b = 2 ^ (i - 1), 2 ^ i
@printf("max value of a(n)/n in %6i < n < %7i = %5.3f\n", a, b, maximum(seq[a:b]))
end
# Task 3
function lastindex(val::Float64)
r, p = 1, 0
# Find the range of 2 power in which the maximum is < val
seq = hcfraction(2 ^ 15)
while maximum(seq[2^p:2^(p+1)]) > val; p += 1 end
r = 2 ^ (p + 1)
while seq[r] < val; r -= 1 end
return r
end
println("You too might have won \$1000 with the mallows number of ", lastindex(0.55))</syntaxhighlight>
{{out}}
<pre>max value of a(n)/n in 1 < n < 2 = 1.000
max value of a(n)/n in 2 < n < 4 = 0.667
max value of a(n)/n in 4 < n < 8 = 0.667
max value of a(n)/n in 8 < n < 16 = 0.636
max value of a(n)/n in 16 < n < 32 = 0.609
max value of a(n)/n in 32 < n < 64 = 0.591
max value of a(n)/n in 64 < n < 128 = 0.576
max value of a(n)/n in 128 < n < 256 = 0.567
max value of a(n)/n in 256 < n < 512 = 0.559
max value of a(n)/n in 512 < n < 1024 = 0.555
max value of a(n)/n in 1024 < n < 2048 = 0.550
max value of a(n)/n in 2048 < n < 4096 = 0.547
max value of a(n)/n in 4096 < n < 8192 = 0.544
max value of a(n)/n in 8192 < n < 16384 = 0.542
max value of a(n)/n in 16384 < n < 32768 = 0.540
max value of a(n)/n in 32768 < n < 65536 = 0.539
max value of a(n)/n in 65536 < n < 131072 = 0.537
max value of a(n)/n in 131072 < n < 262144 = 0.536
max value of a(n)/n in 262144 < n < 524288 = 0.535
max value of a(n)/n in 524288 < n < 1048576 = 0.534
You too might have won $1000 with the mallows number of 1489</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
fun main(args: Array<String>) {
val limit = (1 shl 20) + 1
val a = IntArray(limit)
a[1] = 1
a[2] = 1
for (n in 3 until limit) {
val p = a[n - 1]
a[n] = a[p] + a[n - p]
}
println(" Range Maximum")
println("---------------- --------")
var pow2 = 1
var p = 1
var max = a[1].toDouble()
for (n in 2 until limit) {
val r = a[n].toDouble() / n
if (r > max) max = r
if (n == pow2 * 2) {
println("2 ^ ${"%2d".format(p - 1)} to 2 ^ ${"%2d".format(p)} ${"%f".format(max)}")
pow2 *= 2
p++
max = r
}
}
var prize = 0
for (n in limit - 1 downTo 1) {
if (a[n].toDouble() / n >= 0.55) {
prize = n
break
}
}
println("\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|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}}==
<
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]</
Outputs:
Line 1,586 ⟶ 2,276:
=={{header|MATLAB}} / {{header|Octave}}==
<
Q = zeros(1,N);
Q(1:2) = 1;
Line 1,592 ⟶ 2,282:
Q(n) = Q(Q(n-1))+Q(n-Q(n-1));
end;
end;</
The function can be tested in this way:
<
Q = HCsequence(2^NN+1);
V = Q./(1:2^NN);
Line 1,602 ⟶ 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; </
Output:
Line 1,627 ⟶ 2,317:
=={{header|Nim}}==
{{trans|C}}
<
const last = 1 shl 20
Line 1,647 ⟶ 2,337:
aMax = 0
inc lg2
k1 = n</
Output:
<pre>Maximum between 2^1 and 2^2 was 0.6666666666666666
Line 1,658 ⟶ 2,348:
=={{header|Objeck}}==
<
class HofCon {
function : Main(args : String[]) ~ Nil {
Line 1,697 ⟶ 2,387:
}
}
</syntaxhighlight>
<pre>
Line 1,720 ⟶ 2,410:
Maximum between 2^19 and 2^20 was 0.53377923
</pre>
=={{header|Oforth}}==
<
| l i |
ListBuffer newSize(n) dup add(1) dup add(1) ->l
Line 1,741 ⟶ 2,430:
"Mallows number ==>" . h reverse detect(#[ first 0.55 >= ], true) println
; </
{{out}}
Line 1,769 ⟶ 2,458:
=={{header|Oz}}==
A direct implementation of the recursive definition with explicit memoization using a mutable map (dictionary):
<
local
Cache = {Dictionary.new}
Line 1,795 ⟶ 2,484:
in
{System.showInfo "Max. between 2^"#I#" and 2^"#I+1#": "#Maximum}
end</
Output:
Line 1,822 ⟶ 2,511:
=={{header|PARI/GP}}==
<
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)))</
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.
<
program HofStadterConway;
const
Line 1,950 ⟶ 2,640:
writeln('Mallows number with limit ',l:10:8,' at ',SearchLastPos(a,l));
freemem(p);
end.</
;output:
<pre>
Line 1,969 ⟶ 2,659:
=={{header|Perl}}==
<
use warnings ;
use strict ;
Line 1,994 ⟶ 2,684:
}
print "The prize would have been won at $mallows !\n"
</syntaxhighlight>
Output:
<pre>Between 2 ^ 1 and 2 ^ 2 the maximum value is 0.666666666666667 at 3 !
Line 2,018 ⟶ 2,708:
</pre>
=={{header|
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<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>
<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>
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|Picat}}==
<syntaxhighlight lang="picat">go =>
foreach(N in 0..19)
[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)
end,
println(mallows_number=mallows_number()),
% The sequence definition
table
a(1) = 1.
a(2) = 1.
a(N) = a(a(N-1))+a(N-a(N-1)).
% argmax: find the (first) index for the max value(s) of L.
argmax(L) = [Max,MaxIxFirst] =>
Max = max(L),
MaxIxFirst = {I : I in 1..L.length, L[I] == Max}.first.
% Calculate the Mallows number separately.
mallows_number() = Mallow =>
foreach(M in 1..19)
NVal = 0,
foreach(N in Min..Max)
Ratio = a(N)/N,
if Ratio > MaxRatio then
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}}==
<
(cache '(NIL) N
(if (>= 2 N)
Line 2,117 ⟶ 2,856:
" (the task requests 'n > p' now)" ) ) )
(sequence (** 2 20))</
Output:
<pre>Maximum between 2 and 4 was 0.66666666666666666667
Line 2,141 ⟶ 2,880:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
/* First part: */
Line 2,150 ⟶ 2,889:
L(i) = L(i-k) + L(1+k);
end;
</syntaxhighlight>
=={{header|Python}}==
<
def maxandmallows(nmaxpower2
nmax = 2**nmaxpower2
mxpow2 = []
mallows = None
# Hofstadter-Conway sequence starts at hc[1],
# hc[0] is not part of the series.
hc = [None, 1, 1]
for n in range(2, nmax + 1):
ratio = hc[n] / n
if ratio > mx[0]:
if ratio >= 0.55:
mallows = n
if ratio == 0.5:
print("In the region %7i < n <= %7i: max a(n)/n = %
mxpow2.append(mx[0])
mx = (ratio, n)
hc.append(hc[hc[n]] + hc[-hc[n]])
return hc, mallows if mxpow2 and mxpow2[-1] < 0.55 and n > 4 else None
Line 2,205 ⟶ 2,924:
if mallows:
print("\nYou too might have won $1000 with the mallows number of %i" % mallows)
</syntaxhighlight>
''Sample output''
Line 2,231 ⟶ 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:
<
{a = c(1, 1)
function(n)
{if (is.na(a[n]))
a[n] <<- f(f(n - 1)) + f(n - f(n - 1))
a[n]}})</
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.
<
for (n in 3 : (2^20))
{hofcon[n] =
hofcon[hofcon[n - 1]] +
hofcon[n - hofcon[n - 1]]}</
We can now quickly finish the task with vectorized operations.
<
message("Maxima:")
Line 2,259 ⟶ 3,031:
message("Prize-winning point:")
print(max(which(ratios >= .55)))</
{{out}}
<pre>
Line 2,275 ⟶ 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.
<
(define-syntax-rule (define/memoize1 (proc x) body ...)
Line 2,288 ⟶ 3,060:
1
(+ (conway (conway (sub1 n)))
(conway (- n (conway (sub1 n)))))))</
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.
<
(for/fold ([max -inf.0] [arg-max #f]) ([i sequence])
(define val (begin body ...))
Line 2,303 ⟶ 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))</
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.
<
(for/fold ([prev #f]) (sequences ...)
(define val (let () body ...))
Line 2,320 ⟶ 3,092:
k)))
(printf "Mallows number: ~a~n" mallows)</
'''Sample Output:'''
Line 2,344 ⟶ 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}}==
<
@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,368 ⟶ 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</
'''output'''
<pre>
Line 2,396 ⟶ 3,208:
=={{header|Ring}}==
<
decimals(9)
size = 15
Line 2,418 ⟶ 3,230:
next
see "mallows number is : " + mallows + nl
</syntaxhighlight>
Output:
<pre>
Line 2,439 ⟶ 3,251:
=={{header|Ruby}}==
<
def initialize
@sequence = [nil, 1, 1]
Line 2,467 ⟶ 3,279:
end
puts "the mallows number is #{mallows}"</
{{out}}
Line 2,493 ⟶ 3,305:
</pre>
=={{header|
<syntaxhighlight lang="rust">
struct HofstadterConway {
current: usize,
sequence: Vec<usize>,
}
impl HofstadterConway {
/// Define a constructor for the struct.
fn new() -> HofstadterConway {
HofstadterConway {
current: 0,
sequence: vec![1, 1],
}
impl Default for HofstadterConway {
Self::new()
}
}
/// Implement the hofstadter q iteration sequence.
impl Iterator for HofstadterConway {
type Item = usize;
/// This gets called to fetch the next item of the iterator.
fn next(&mut self) -> Option<usize> {
let max_index = self.sequence.len() - 1;
let last_value = self.sequence[max_index];
if self.current > max_index {
let new_x = self.sequence[last_value - 1] + self.sequence[max_index - last_value + 1];
self.sequence.push(new_x);
}
self.current += 1;
Some(self.sequence[self.current - 1])
}
}
#[allow(clippy::cast_precision_loss)]
fn main() {
let mut hof = HofstadterConway::new();
let mut winning_num = 0_usize;
for p in 0..20 {
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}}==
<
def pow2(n: Int): Int = (Iterator.fill(n)(2)).product
Line 2,570 ⟶ 3,422:
println("Mallow's number = %s".format(mallowsNumber))
}
}</
'''Output'''
<pre>Maximum of a(n)/n between 2^1 and 2^2 was 0.6666666666666666 at 3
Line 2,592 ⟶ 3,444:
Maximum of a(n)/n between 2^19 and 2^20 was 0.5337792299633678 at 722589
Mallow's number = 1489</pre>
=={{header|Scheme}}==
<syntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
(only (srfi 1) iota))
;; maximum size of sequence to consider, as a power of 2
(define *max-power* 20)
(define *size* (expt 2 *max-power*))
;; Task 1: Generate members of the sequence
(define *seq* (make-vector (+ 1 *size*))) ; add 1, to use 1-indexing into sequence
(vector-set! *seq* 1 1)
(vector-set! *seq* 2 1)
(for-each
(lambda (n)
(let ((x (vector-ref *seq* (- n 1))))
(vector-set! *seq* n (+ (vector-ref *seq* x)
(vector-ref *seq* (- n x))))))
(iota (- *size* 2) 3))
;; Task 2: Show maxima of a(n)/n between successive powers of two
(for-each
(lambda (power)
(let ((start-idx (+ (expt 2 (- power 1)) 1))
(end-idx (expt 2 power)))
(do ((i start-idx (+ 1 i))
(maximum 0 (max maximum (/ (vector-ref *seq* i)
i))))
((> i end-idx)
(display
(string-append
"Maximum between 2^" (number->string (- power 1))
" and 2^" (number->string power)
" = " (number->string (inexact maximum))
"\n"))))))
(iota (- *max-power* 1) 2))
;; Task 3: Find value of p where a(n)/n < 0.55 for all n > p (in our sequence)
(do ((idx *size* (- idx 1)))
((or (zero? idx) ; safety net
(> (/ (vector-ref *seq* idx) idx)
0.55))
(display (string-append "\np=" (number->string idx) "\n"))))
</syntaxhighlight>
{{out}}
<pre>
Maximum between 2^1 and 2^2 = 0.6666666666666666
Maximum between 2^2 and 2^3 = 0.6666666666666666
Maximum between 2^3 and 2^4 = 0.6363636363636364
Maximum between 2^4 and 2^5 = 0.6086956521739131
Maximum between 2^5 and 2^6 = 0.5909090909090909
Maximum between 2^6 and 2^7 = 0.5760869565217391
Maximum between 2^7 and 2^8 = 0.5674157303370787
Maximum between 2^8 and 2^9 = 0.5594594594594594
Maximum between 2^9 and 2^10 = 0.5549374130737135
Maximum between 2^10 and 2^11 = 0.5501008742434432
Maximum between 2^11 and 2^12 = 0.5474628926475664
Maximum between 2^12 and 2^13 = 0.5441447478639638
Maximum between 2^13 and 2^14 = 0.5424427087803622
Maximum between 2^14 and 2^15 = 0.5400710975115871
Maximum between 2^15 and 2^16 = 0.5387840205842557
Maximum between 2^16 and 2^17 = 0.5370436569998659
Maximum between 2^17 and 2^18 = 0.5360200678115611
Maximum between 2^18 and 2^19 = 0.5346454310781124
Maximum between 2^19 and 2^20 = 0.5337792299633678
p=1489
</pre>
=={{header|Sidef}}==
{{trans|Ruby}}
<
has sequence = [nil, 1, 1]
method term(n {.is_pos}) {
var a = sequence
a[n]
}
Line 2,608 ⟶ 3,532:
var mallows = nil
for i in (1.
var j = i+1
var (max_n, max_v) = (-1, -1)
for n in (
var v = (hc.term(n) / n)
(max_n, max_v) = (n, v) if (v > max_v)
mallows = n if (v >= 0.55)
}
say ("maximum between 2^%2d and 2^%2d occurs at%7d:
}
say "the mallows number is #{mallows}"</
{{out}}
<pre>
Line 2,646 ⟶ 3,570:
=={{header|Swift}}==
{{trans|Java}}
<
var aList = [Int](count: m + 1, repeatedValue: 0)
var k1 = 2
Line 2,674 ⟶ 3,598:
}
doSqnc(1 << 20)</
{{out}}
<pre>
Line 2,700 ⟶ 3,624:
=={{header|Tcl}}==
The routine to return the ''n''<sup>th</sup> member of the sequence.
<
set hofcon10k {1 1}
Line 2,717 ⟶ 3,641:
}
return $c
}</
The code to explore the sequence, looking for maxima in the ratio.
<
set end [expr {2**($p+1)}]
set maxI 0; set maxV 0
Line 2,727 ⟶ 3,651:
}
puts "max in 2**$p..2**[expr {$p+1}] at $maxI : $maxV"
}</
Output:
<pre>
Line 2,749 ⟶ 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.
<
call a.memorization
call Mallows_Number
Line 2,788 ⟶ 3,886:
retn
a rd 1 shl 20</
=={{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}}==
<syntaxhighlight lang="zkl">fcn hofstadterConwaySequence(m){
a:=List.createLong(m + 1,0);
a[0]=a[1]=1;
v,p2,lg2, amax, mallow := a[2],4,1, 0.0, Void;
foreach n in ([2 .. m]){
v=a[n]=a[v] + a[n - v];
f:=1.0*v/n;
if(f>=0.55) mallow=n;
amax=amax.max(f);
if(n==p2){
println("Max in [2^%d, 2^%d]: %f".fmt(lg2, lg2+1, amax));
amax,p2 = 0.0, (n+1).nextPowerOf2;
lg2+=1;
}
}
if(mallow) println("Winning number = ",mallow);
}
hofstadterConwaySequence((2).pow(20));</syntaxhighlight>
{{out}}
<pre>
Max in [2^1, 2^2]: 0.666667
Max in [2^2, 2^3]: 0.666667
Max in [2^3, 2^4]: 0.636364
Max in [2^4, 2^5]: 0.608696
Max in [2^5, 2^6]: 0.590909
Max in [2^6, 2^7]: 0.576087
Max in [2^7, 2^8]: 0.567416
Max in [2^8, 2^9]: 0.559459
Max in [2^9, 2^10]: 0.554937
Max in [2^10, 2^11]: 0.550101
Max in [2^11, 2^12]: 0.547463
Max in [2^12, 2^13]: 0.544145
Max in [2^13, 2^14]: 0.542443
Max in [2^14, 2^15]: 0.540071
Max in [2^15, 2^16]: 0.538784
Max in [2^16, 2^17]: 0.537044
Max in [2^17, 2^18]: 0.536020
Max in [2^18, 2^19]: 0.534645
Max in [2^19, 2^20]: 0.533779
Winning number = 1489
</pre>
|