Hofstadter-Conway $10,000 sequence: Difference between revisions
Hofstadter-Conway $10,000 sequence (view source)
Revision as of 18:51, 16 January 2024
, 4 months agoAdded Easylang
(Added Easylang) |
|||
(13 intermediate revisions by 10 users not shown) | |||
Line 48:
{{trans|Nim}}
<
V a_list = [0] * (last+1)
a_list[0] = -50'000
Line 67:
amax = 0
lg2++
k1 = n</
{{out}}
Line 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 171:
A DS 4096F array a(uprdim)
REGEQU
END HOFSTADT</
{{out}}
<pre>
Line 189:
=={{header|Ada}}==
<
-- Allocation of arrays on the heap
Line 262:
end Conway;
</syntaxhighlight>
Sample output:
<pre>
Line 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 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 356:
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.
<
script o
property lst : {1, 1}
Line 386:
end HC10000
HC10000(2 ^ 20)</
{{output}}
<
=={{header|AutoHotkey}}==
<
CreateLists(2 ** (Max:=20))
Line 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 465:
Iterative approach:
<
BEGIN {
NN = 20;
Line 486:
Q[n] = Q[Q[n-1]]+Q[n-Q[n-1]];
}
} </
Recursive variant:
<
BEGIN {
Q[1] = 1;
Line 529:
S[n] = 1;
return (Q[n]);
} </
Output:
Line 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 574 ⟶ 602:
ENDIF
NEXT n%
PRINT "Mallows number is ";Mallows%</
Results
Line 598 ⟶ 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 655 ⟶ 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 680 ⟶ 915:
=={{header|C}}==
<
#include <stdlib.h>
Line 708 ⟶ 943:
}
return 1;
}</
Results
<pre>Maximum between 2^1 and 2^2 was 0.666667
Line 721 ⟶ 956:
{{works with|C#|3.0}}
<
using System;
using System.Linq;
Line 765 ⟶ 1,000:
}
}
</syntaxhighlight>
Output:-
<pre>Maximum from 2^1 to 2^2 is 0.666666666666667 at 3
Line 790 ⟶ 1,025:
=={{header|C++}}==
<
#include <deque>
#include <iostream>
Line 824 ⟶ 1,059:
}
}
</syntaxhighlight>
Output:
<pre>
Line 850 ⟶ 1,085:
=={{header|Clojure}}==
<
(:use [clojure.math.numeric-tower :only [expt]]))
Line 873 ⟶ 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 917 ⟶ 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 944 ⟶ 1,179:
=={{header|D}}==
<
void hofstadterConwaySequence(in int m) {
Line 967 ⟶ 1,202:
void main() {
hofstadterConwaySequence(2 ^^ 20);
}</
Output:
Line 989 ⟶ 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}}==
<
(decimals 4)
(cache-size 2000000)
Line 1,020 ⟶ 1,281:
#:break (> (// (a n) n) 0.55) => n )
)
</syntaxhighlight>
{{out}}
<pre>
Line 1,048 ⟶ 1,309:
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,109 ⟶ 1,370:
end
</syntaxhighlight>
{{out}}
As the run time is quite slow, the test output is shown only up to 2^15.
Line 1,130 ⟶ 1,391:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module( hofstadter_conway ).
Line 1,166 ⟶ 1,427:
At_end = dict:fetch( Key - Last_number, Dict ),
dict:store( Key, At_begining + At_end, Dict ).
</syntaxhighlight>
{{out}}
<pre>
Line 1,193 ⟶ 1,454:
=={{header|Euler Math Toolbox}}==
<syntaxhighlight lang="euler math toolbox">
>function hofstadter (n) ...
$v=ones(1,n);
Line 1,221 ⟶ 1,482:
>max(nonzeros(v1>0.55))
1489
</syntaxhighlight>
=={{header|F Sharp|F#}}==
<
while a.Count <= (1 <<< 20) do
a.[a.[a.Count - 1]] + a.[a.Count - a.[a.Count - 1]] |> a.Add
Line 1,235 ⟶ 1,496:
|> List.rev
|> List.find (fun (i, n) -> float(n) / float(i) > 0.55)
printfn "Mallows number is %d" mallows</
Outputs:
<pre>Maximum in 2.. 4 is 0.666667
Line 1,259 ⟶ 1,520:
=={{header|Factor}}==
<
prettyprint sequences splitting ;
Line 1,277 ⟶ 1,538:
]
[ "Mallow's number: " write [ 0.55 >= ] find-last drop 1 + . ]
bi</
{{out}}
<pre>
Line 1,303 ⟶ 1,564:
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
program conway
implicit none
Line 1,349 ⟶ 1,610:
end program conway
</syntaxhighlight>
Output:
Line 1,375 ⟶ 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,480 ⟶ 1,713:
print
print "Dr. Mallow's winning number is:"; Mallows
HandleEvents</syntaxhighlight>
Output:
Line 1,508 ⟶ 1,742:
Dr. Mallow's winning number is: 1489
</pre>
=={{header|Go}}==
<
import (
Line 1,546 ⟶ 1,772:
}
fmt.Println("winning number", mallow)
}</
Output:
<pre>
Line 1,573 ⟶ 1,799:
=={{header|Haskell}}==
<
import Data.Ord
import Data.Array
Line 1,597 ⟶ 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,615 ⟶ 1,841:
}
write("Mallows's number is ",\mallows | "NOT found!")
end</
Output:
Line 1,643 ⟶ 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,660 ⟶ 1,886:
1 0.666667 0.666667 0.636364 ...
MxAnN@AnN@hc10kE 20
1 0.666667 0.666667 0.636364 ...</
=={{header|Java}}==
Line 1,666 ⟶ 1,892:
Remove translation and provide native Java implementation.
<
// Title: Hofstadter-Conway $10,000 sequence
Line 1,705 ⟶ 1,931:
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,731 ⟶ 1,957:
=={{header|JavaScript}}==
<
var memo = [1, 1];
Line 1,755 ⟶ 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,770 ⟶ 1,996:
=={{header|jq}}==
{{works with|jq}}
<
reduce range(3; $limit) as $n ([0,1,1];
.[$n-1] as $p
Line 1,795 ⟶ 2,021:
| "\nMallows' number = \(.)") ;
task( pow(2;20) + 1 )</
{{out}}
<pre>
Line 1,825 ⟶ 2,051:
=={{header|Julia}}==
<
# Task 1
Line 1,862 ⟶ 2,088:
end
println("You too might have won \$1000 with the mallows number of ", lastindex(0.55))</
{{out}}
Line 1,888 ⟶ 2,114:
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 1,924 ⟶ 2,150:
}
println("\nMallows' number = $prize")
}</
{{out}}
Line 1,956 ⟶ 2,182:
=={{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.
<
local fmt, write=string.format,io.write
local hof=coroutine.wrap(function()
Line 1,993 ⟶ 2,219:
end
write("So Mallows number is ", mallows, " with ", fmt("%.4f",mdiv), ", yay, just wire me my $10000 now!\n")
</syntaxhighlight>
{{out}}
<pre>
Line 2,020 ⟶ 2,246:
=={{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 2,050 ⟶ 2,276:
=={{header|MATLAB}} / {{header|Octave}}==
<
Q = zeros(1,N);
Q(1:2) = 1;
Line 2,056 ⟶ 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 2,066 ⟶ 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 2,091 ⟶ 2,317:
=={{header|Nim}}==
{{trans|C}}
<
const last = 1 shl 20
Line 2,111 ⟶ 2,337:
aMax = 0
inc lg2
k1 = n</
Output:
<pre>Maximum between 2^1 and 2^2 was 0.6666666666666666
Line 2,122 ⟶ 2,348:
=={{header|Objeck}}==
<
class HofCon {
function : Main(args : String[]) ~ Nil {
Line 2,161 ⟶ 2,387:
}
}
</syntaxhighlight>
<pre>
Line 2,187 ⟶ 2,413:
=={{header|Oforth}}==
<
| l i |
ListBuffer newSize(n) dup add(1) dup add(1) ->l
Line 2,204 ⟶ 2,430:
"Mallows number ==>" . h reverse detect(#[ first 0.55 >= ], true) println
; </
{{out}}
Line 2,232 ⟶ 2,458:
=={{header|Oz}}==
A direct implementation of the recursive definition with explicit memoization using a mutable map (dictionary):
<
local
Cache = {Dictionary.new}
Line 2,258 ⟶ 2,484:
in
{System.showInfo "Max. between 2^"#I#" and 2^"#I+1#": "#Maximum}
end</
Output:
Line 2,285 ⟶ 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]
Line 2,294 ⟶ 2,520:
=={{header|Pascal}}==
tested with freepascal 3.1.1 64 Bit.
<
program HofStadterConway;
const
Line 2,414 ⟶ 2,640:
writeln('Mallows number with limit ',l:10:8,' at ',SearchLastPos(a,l));
freemem(p);
end.</
;output:
<pre>
Line 2,433 ⟶ 2,659:
=={{header|Perl}}==
<
use warnings ;
use strict ;
Line 2,458 ⟶ 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,483 ⟶ 2,709:
=={{header|Phix}}==
<!--<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}}
Line 2,534 ⟶ 2,763:
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()),
nl.
% 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 =>
Mallow = _,
foreach(M in 1..19)
Min = 2**M,
Max = Min*2,
MaxRatio = 0,
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,564 ⟶ 2,856:
" (the task requests 'n > p' now)" ) ) )
(sequence (** 2 20))</
Output:
<pre>Maximum between 2 and 4 was 0.66666666666666666667
Line 2,588 ⟶ 2,880:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
/* First part: */
Line 2,597 ⟶ 2,889:
L(i) = L(i-k) + L(1+k);
end;
</syntaxhighlight>
=={{header|Python}}==
<
def maxandmallows(nmaxpower2):
Line 2,660 ⟶ 2,924:
if mallows:
print("\nYou too might have won $1000 with the mallows number of %i" % mallows)
</syntaxhighlight>
''Sample output''
Line 2,689 ⟶ 2,953:
=={{header|Quackery}}==
<
[ ' [ 1 1 ]
Line 2,714 ⟶ 2,978:
dup echo
say " is " 10 point$ echo$ cr ]
drop</
{{out}}
Line 2,743 ⟶ 3,007:
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,767 ⟶ 3,031:
message("Prize-winning point:")
print(max(which(ratios >= .55)))</
{{out}}
<pre>
Line 2,783 ⟶ 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,796 ⟶ 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,811 ⟶ 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,828 ⟶ 3,092:
k)))
(printf "Mallows number: ~a~n" mallows)</
'''Sample Output:'''
Line 2,857 ⟶ 3,121:
{{works with|Rakudo|2018.09}}
Note that <tt>@a</tt> is a lazy array, and the Z variants are "zipwith" operators.
<syntaxhighlight lang="raku"
my @a = (0,1,1, -> $p { @a[$p] + @a[$n++ - $p] } ... *);
@a[2**20]; # pre-calculate sequence
Line 2,870 ⟶ 3,134:
$max.key, ' at ', $max.value;
}
say "Mallows' number would appear to be ", $last55;</
{{out}}
<pre> 1 2..3 0.666666666666667 at 3
Line 2,894 ⟶ 3,158:
=={{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,916 ⟶ 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,944 ⟶ 3,208:
=={{header|Ring}}==
<
decimals(9)
size = 15
Line 2,966 ⟶ 3,230:
next
see "mallows number is : " + mallows + nl
</syntaxhighlight>
Output:
<pre>
Line 2,987 ⟶ 3,251:
=={{header|Ruby}}==
<
def initialize
@sequence = [nil, 1, 1]
Line 3,015 ⟶ 3,279:
end
puts "the mallows number is #{mallows}"</
{{out}}
Line 3,040 ⟶ 3,304:
the mallows number is 1489
</pre>
=={{header|Rust}}==
<
struct HofstadterConway {
current: usize,
Line 3,150 ⟶ 3,366:
println!("Winning number: {}", winning_num);
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,178 ⟶ 3,394:
=={{header|Scala}}==
<
def pow2(n: Int): Int = (Iterator.fill(n)(2)).product
Line 3,206 ⟶ 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 3,231 ⟶ 3,447:
=={{header|Scheme}}==
<
(import (scheme base)
(scheme write)
Line 3,275 ⟶ 3,491:
0.55))
(display (string-append "\np=" (number->string idx) "\n"))))
</syntaxhighlight>
{{out}}
Line 3,304 ⟶ 3,520:
=={{header|Sidef}}==
{{trans|Ruby}}
<
has sequence = [nil, 1, 1]
method term(n {.is_pos}) {
Line 3,327 ⟶ 3,543:
}
say "the mallows number is #{mallows}"</
{{out}}
<pre>
Line 3,354 ⟶ 3,570:
=={{header|Swift}}==
{{trans|Java}}
<
var aList = [Int](count: m + 1, repeatedValue: 0)
var k1 = 2
Line 3,382 ⟶ 3,598:
}
doSqnc(1 << 20)</
{{out}}
<pre>
Line 3,408 ⟶ 3,624:
=={{header|Tcl}}==
The routine to return the ''n''<sup>th</sup> member of the sequence.
<
set hofcon10k {1 1}
Line 3,425 ⟶ 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 3,435 ⟶ 3,651:
}
puts "max in 2**$p..2**[expr {$p+1}] at $maxI : $maxV"
}</
Output:
<pre>
Line 3,461 ⟶ 3,677:
=={{header|VBA}}==
{{trans|Phix}} Function q rewritten to sub.
<
Sub make_q()
ReDim q(2 ^ 20)
Line 3,494 ⟶ 3,710:
Next p
Debug.Print "Mallows number is"; mallows
End Sub</
<pre>Maximum in range 1 to 2 occurs at 1: 1,000000
Maximum in range 2 to 4 occurs at 3: 0,666667
Line 3,516 ⟶ 3,732:
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}}
<
var limit = 1<<20 + 1
Line 3,554 ⟶ 3,819:
}
}
System.print("\nMallows' number = %(prize)")</
{{out}}
Line 3,586 ⟶ 3,851:
=={{header|X86 Assembly}}==
Using FASM syntax.
<
call a.memorization
call Mallows_Number
Line 3,621 ⟶ 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}}==
<
a:=List.createLong(m + 1,0);
a[0]=a[1]=1;
Line 3,643 ⟶ 3,958:
}
hofstadterConwaySequence((2).pow(20));</
{{out}}
<pre>
Line 3,667 ⟶ 3,982:
Winning number = 1489
</pre>
|