WeixiangYan/CodeTransOcean huggingface dataset:
Task: Hofstadter-Conway $10,000 sequence




Go: package main

import (
    "fmt"
)

func main() {
    a := []int{0, 1, 1} 
    x := 1  
    n := 2  
    mallow := 0
    for p := 1; p < 20; p++ {
        max := 0.
        for nextPot := n*2; n < nextPot; {
            n = len(a) 
            x = a[x]+a[n-x]
            a = append(a, x)
            f := float64(x)/float64(n)
            if f > max {
                max = f
            }
            if f >= .55 {
                mallow = n
            }
        }
        fmt.Printf("max between 2^%d and 2^%d was %f\n", p, p+1, max)
    }
    fmt.Println("winning number", mallow)
}








Ada: 


with Ada.Text_IO; use Ada.Text_IO;
with Unchecked_Deallocation;

procedure Conway is

   package Real_io is new Float_IO (Float);

   Maxrange : constant := 2 ** 20;

   type Sequence is array (Positive range 1 .. Maxrange) of Positive;
   type Sequence_Ptr is access all Sequence;
   procedure Free is new Unchecked_Deallocation (Sequence, Sequence_Ptr);

   S : Sequence_Ptr := new Sequence;

   type Ratio_Array is array (Positive range 1 .. Maxrange) of Float;
   type Ratio_Ptr is access all Ratio_Array;
   procedure Free is new Unchecked_Deallocation (Ratio_Array, Ratio_Ptr);

   Ratio : Ratio_Ptr := new Ratio_Array;

   Mallows : Positive;
   M       : Natural := 0;
begin
   S (1) := 1;
   S (2) := 1;
   for K in 3 .. Maxrange loop
      S (K) := S (S (K - 1)) + S (K - S (K - 1));
   end loop;

   for k in 1 .. Maxrange loop
      Ratio (k) := Float (S (k)) / Float (k);
   end loop;

   for N in 1 .. 19 loop
      declare
         Max   : Float := 0.0;
         Where : Positive;
      begin
         for K in 2 ** N .. 2 ** (N + 1) loop
            if Max < Ratio (K) then
               Max   := Ratio (K);
               Where := K;
            end if;
         end loop;
         if (M = 0 and Max < 0.55) then
            M := N - 1;
         end if;
         Put
           ("Maximun of a(n)/n between 2^" &
            Integer'Image (N) &
            " and 2^" &
            Integer'Image (N + 1) &
            " was ");
         Real_io.Put (Max, Fore => 1, Aft => 8, Exp => 0);
         Put_Line (" at" & Integer'Image (Where));
      end;
   end loop;
   
   for I in reverse 2 ** M .. 2 ** (M + 1) loop
      if (Ratio (I) > 0.55) then
         Mallows := I;
         exit;
      end if;
   end loop;
   Put_Line ("Mallows number" & Integer'Image (Mallows));
   Free (S);
   Free (Ratio);
end Conway;
Task: Hofstadter-Conway $10,000 sequence





Java: 

public class HofstadterConwaySequence {

    private static int MAX = (int) Math.pow(2, 20) + 1;
    private static int[] HCS = new int[MAX];
    static {
        HCS[1] = 1;
        HCS[2] = 1;
        for ( int n = 3 ; n < MAX ; n++ ) {
            int nm1 = HCS[n - 1];
            HCS[n] = HCS[nm1] + HCS[n - nm1];
        }
    }
    
    public static void main(String[] args) {
        int mNum = 0;
        for ( int m = 1 ; m < 20 ; m++ ) {
            int min = (int) Math.pow(2, m);
            int max = min * 2;
            double maxRatio = 0.0;
            int nVal = 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);
    }

}







Ada: 


with Ada.Text_IO; use Ada.Text_IO;
with Unchecked_Deallocation;

procedure Conway is

   package Real_io is new Float_IO (Float);

   Maxrange : constant := 2 ** 20;

   type Sequence is array (Positive range 1 .. Maxrange) of Positive;
   type Sequence_Ptr is access all Sequence;
   procedure Free is new Unchecked_Deallocation (Sequence, Sequence_Ptr);

   S : Sequence_Ptr := new Sequence;

   type Ratio_Array is array (Positive range 1 .. Maxrange) of Float;
   type Ratio_Ptr is access all Ratio_Array;
   procedure Free is new Unchecked_Deallocation (Ratio_Array, Ratio_Ptr);

   Ratio : Ratio_Ptr := new Ratio_Array;

   Mallows : Positive;
   M       : Natural := 0;
begin
   S (1) := 1;
   S (2) := 1;
   for K in 3 .. Maxrange loop
      S (K) := S (S (K - 1)) + S (K - S (K - 1));
   end loop;

   for k in 1 .. Maxrange loop
      Ratio (k) := Float (S (k)) / Float (k);
   end loop;

   for N in 1 .. 19 loop
      declare
         Max   : Float := 0.0;
         Where : Positive;
      begin
         for K in 2 ** N .. 2 ** (N + 1) loop
            if Max < Ratio (K) then
               Max   := Ratio (K);
               Where := K;
            end if;
         end loop;
         if (M = 0 and Max < 0.55) then
            M := N - 1;
         end if;
         Put
           ("Maximun of a(n)/n between 2^" &
            Integer'Image (N) &
            " and 2^" &
            Integer'Image (N + 1) &
            " was ");
         Real_io.Put (Max, Fore => 1, Aft => 8, Exp => 0);
         Put_Line (" at" & Integer'Image (Where));
      end;
   end loop;
   
   for I in reverse 2 ** M .. 2 ** (M + 1) loop
      if (Ratio (I) > 0.55) then
         Mallows := I;
         exit;
      end if;
   end loop;
   Put_Line ("Mallows number" & Integer'Image (Mallows));
   Free (S);
   Free (Ratio);
end Conway;
Task: Hofstadter-Conway $10,000 sequence







Python: from __future__ import division

def maxandmallows(nmaxpower2):
    nmax = 2**nmaxpower2
    mx = (0.5, 2)
    mxpow2 = []
    mallows = None

    
    
    hc = [None, 1, 1]

    for n in range(2, nmax + 1):
        ratio = hc[n] / n
        if ratio > mx[0]:
            mx = (ratio, n)
        if ratio >= 0.55:
            mallows = n
        if ratio == 0.5:
            print("In the region %7i < n <= %7i: max a(n)/n = %6.4f at  n = %i" %
		  (n//2, n, mx[0], mx[1]))
            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

if __name__ == '__main__':
    hc, mallows = maxandmallows(20)
    if mallows:
        print("\nYou too might have won $1000 with the mallows number of %i" % mallows)





Ada: 


with Ada.Text_IO; use Ada.Text_IO;
with Unchecked_Deallocation;

procedure Conway is

   package Real_io is new Float_IO (Float);

   Maxrange : constant := 2 ** 20;

   type Sequence is array (Positive range 1 .. Maxrange) of Positive;
   type Sequence_Ptr is access all Sequence;
   procedure Free is new Unchecked_Deallocation (Sequence, Sequence_Ptr);

   S : Sequence_Ptr := new Sequence;

   type Ratio_Array is array (Positive range 1 .. Maxrange) of Float;
   type Ratio_Ptr is access all Ratio_Array;
   procedure Free is new Unchecked_Deallocation (Ratio_Array, Ratio_Ptr);

   Ratio : Ratio_Ptr := new Ratio_Array;

   Mallows : Positive;
   M       : Natural := 0;
begin
   S (1) := 1;
   S (2) := 1;
   for K in 3 .. Maxrange loop
      S (K) := S (S (K - 1)) + S (K - S (K - 1));
   end loop;

   for k in 1 .. Maxrange loop
      Ratio (k) := Float (S (k)) / Float (k);
   end loop;

   for N in 1 .. 19 loop
      declare
         Max   : Float := 0.0;
         Where : Positive;
      begin
         for K in 2 ** N .. 2 ** (N + 1) loop
            if Max < Ratio (K) then
               Max   := Ratio (K);
               Where := K;
            end if;
         end loop;
         if (M = 0 and Max < 0.55) then
            M := N - 1;
         end if;
         Put
           ("Maximun of a(n)/n between 2^" &
            Integer'Image (N) &
            " and 2^" &
            Integer'Image (N + 1) &
            " was ");
         Real_io.Put (Max, Fore => 1, Aft => 8, Exp => 0);
         Put_Line (" at" & Integer'Image (Where));
      end;
   end loop;
   
   for I in reverse 2 ** M .. 2 ** (M + 1) loop
      if (Ratio (I) > 0.55) then
         Mallows := I;
         exit;
      end if;
   end loop;
   Put_Line ("Mallows number" & Integer'Image (Mallows));
   Free (S);
   Free (Ratio);
end Conway;
Task: Hofstadter-Conway $10,000 sequence








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




Ada: 


with Ada.Text_IO; use Ada.Text_IO;
with Unchecked_Deallocation;

procedure Conway is

   package Real_io is new Float_IO (Float);

   Maxrange : constant := 2 ** 20;

   type Sequence is array (Positive range 1 .. Maxrange) of Positive;
   type Sequence_Ptr is access all Sequence;
   procedure Free is new Unchecked_Deallocation (Sequence, Sequence_Ptr);

   S : Sequence_Ptr := new Sequence;

   type Ratio_Array is array (Positive range 1 .. Maxrange) of Float;
   type Ratio_Ptr is access all Ratio_Array;
   procedure Free is new Unchecked_Deallocation (Ratio_Array, Ratio_Ptr);

   Ratio : Ratio_Ptr := new Ratio_Array;

   Mallows : Positive;
   M       : Natural := 0;
begin
   S (1) := 1;
   S (2) := 1;
   for K in 3 .. Maxrange loop
      S (K) := S (S (K - 1)) + S (K - S (K - 1));
   end loop;

   for k in 1 .. Maxrange loop
      Ratio (k) := Float (S (k)) / Float (k);
   end loop;

   for N in 1 .. 19 loop
      declare
         Max   : Float := 0.0;
         Where : Positive;
      begin
         for K in 2 ** N .. 2 ** (N + 1) loop
            if Max < Ratio (K) then
               Max   := Ratio (K);
               Where := K;
            end if;
         end loop;
         if (M = 0 and Max < 0.55) then
            M := N - 1;
         end if;
         Put
           ("Maximun of a(n)/n between 2^" &
            Integer'Image (N) &
            " and 2^" &
            Integer'Image (N + 1) &
            " was ");
         Real_io.Put (Max, Fore => 1, Aft => 8, Exp => 0);
         Put_Line (" at" & Integer'Image (Where));
      end;
   end loop;
   
   for I in reverse 2 ** M .. 2 ** (M + 1) loop
      if (Ratio (I) > 0.55) then
         Mallows := I;
         exit;
      end if;
   end loop;
   Put_Line ("Mallows number" & Integer'Image (Mallows));
   Free (S);
   Free (Ratio);
end Conway;
Task: Hofstadter-Conway $10,000 sequence

C: #include <stdio.h>
#include <stdlib.h>

int a_list[1<<20 + 1];

int doSqnc( int m)
{
    int max_df = 0;
    int p2_max = 2;
    int v, n;
    int k1 = 2;
    int lg2 = 1;
    double amax = 0;
    a_list[0] = -50000;
    a_list[1] = a_list[2] = 1;
    v = a_list[2];

    for (n=3; n <= m;  n++) {
        v = a_list[n] = a_list[v] + a_list[n-v];
        if ( amax < v*1.0/n) amax = v*1.0/n;
        if ( 0 == (k1&n)) {
            printf("Maximum between 2^%d and 2^%d was %f\n", lg2,lg2+1, amax);
            amax = 0;
            lg2++;
        }
        k1 = n;
    }
    return 1;
}
Task: Hofstadter-Conway $10,000 sequence



C#: using System;
using System.Linq;

namespace HofstadterConway
{
    class Program
    {
        static int[] GenHofstadterConway(int max)
        {
            int[] result = new int[max];
            result[0]=result[1]=1;
            for (int ix = 2; ix < max; ix++)
                result[ix] = result[result[ix - 1] - 1] + result[ix - result[ix - 1]];
            return result;
        }

        static void Main(string[] args)
        {
            double[] adiv = new double[1 << 20];
            {
                int[] a = GenHofstadterConway(1 << 20);
                for (int i = 0; i < 1 << 20; i++)
                    adiv[i] = a[i] / (double)(i + 1);
            }
            for (int p = 2; p <= 20; p++)
            {
                var max = Enumerable.Range(
                     (1 << (p - 1)) - 1,
                     (1 << p) - (1 << (p - 1))
                     )
                     .Select(ix => new { I = ix + 1, A = adiv[ix] })
                     .OrderByDescending(x => x.A)
                     .First();
                Console.WriteLine("Maximum from 2^{0} to 2^{1} is {2} at {3}",
                    p - 1, p, max.A, max.I);
            }
            Console.WriteLine("The winning number is {0}.",
                Enumerable.Range(0, 1 << 20)
                    .Last(i => (adiv[i] > 0.55)) + 1
                );
        }
    }
}
Task: Hofstadter-Conway $10,000 sequence


C++: #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;
  }
}
Task: Hofstadter-Conway $10,000 sequence





Java: 

public class HofstadterConwaySequence {

    private static int MAX = (int) Math.pow(2, 20) + 1;
    private static int[] HCS = new int[MAX];
    static {
        HCS[1] = 1;
        HCS[2] = 1;
        for ( int n = 3 ; n < MAX ; n++ ) {
            int nm1 = HCS[n - 1];
            HCS[n] = HCS[nm1] + HCS[n - nm1];
        }
    }
    
    public static void main(String[] args) {
        int mNum = 0;
        for ( int m = 1 ; m < 20 ; m++ ) {
            int min = (int) Math.pow(2, m);
            int max = min * 2;
            double maxRatio = 0.0;
            int nVal = 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);
    }

}
Task: Hofstadter-Conway $10,000 sequence







Python: from __future__ import division

def maxandmallows(nmaxpower2):
    nmax = 2**nmaxpower2
    mx = (0.5, 2)
    mxpow2 = []
    mallows = None

    
    
    hc = [None, 1, 1]

    for n in range(2, nmax + 1):
        ratio = hc[n] / n
        if ratio > mx[0]:
            mx = (ratio, n)
        if ratio >= 0.55:
            mallows = n
        if ratio == 0.5:
            print("In the region %7i < n <= %7i: max a(n)/n = %6.4f at  n = %i" %
		  (n//2, n, mx[0], mx[1]))
            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

if __name__ == '__main__':
    hc, mallows = maxandmallows(20)
    if mallows:
        print("\nYou too might have won $1000 with the mallows number of %i" % mallows)
Task: Hofstadter-Conway $10,000 sequence








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
Task: Hofstadter-Conway $10,000 sequence




Go: package main

import (
    "fmt"
)

func main() {
    a := []int{0, 1, 1} 
    x := 1  
    n := 2  
    mallow := 0
    for p := 1; p < 20; p++ {
        max := 0.
        for nextPot := n*2; n < nextPot; {
            n = len(a) 
            x = a[x]+a[n-x]
            a = append(a, x)
            f := float64(x)/float64(n)
            if f > max {
                max = f
            }
            if f >= .55 {
                mallow = n
            }
        }
        fmt.Printf("max between 2^%d and 2^%d was %f\n", p, p+1, max)
    }
    fmt.Println("winning number", mallow)
}
Task: Hofstadter-Conway $10,000 sequence



C#: using System;
using System.Linq;

namespace HofstadterConway
{
    class Program
    {
        static int[] GenHofstadterConway(int max)
        {
            int[] result = new int[max];
            result[0]=result[1]=1;
            for (int ix = 2; ix < max; ix++)
                result[ix] = result[result[ix - 1] - 1] + result[ix - result[ix - 1]];
            return result;
        }

        static void Main(string[] args)
        {
            double[] adiv = new double[1 << 20];
            {
                int[] a = GenHofstadterConway(1 << 20);
                for (int i = 0; i < 1 << 20; i++)
                    adiv[i] = a[i] / (double)(i + 1);
            }
            for (int p = 2; p <= 20; p++)
            {
                var max = Enumerable.Range(
                     (1 << (p - 1)) - 1,
                     (1 << p) - (1 << (p - 1))
                     )
                     .Select(ix => new { I = ix + 1, A = adiv[ix] })
                     .OrderByDescending(x => x.A)
                     .First();
                Console.WriteLine("Maximum from 2^{0} to 2^{1} is {2} at {3}",
                    p - 1, p, max.A, max.I);
            }
            Console.WriteLine("The winning number is {0}.",
                Enumerable.Range(0, 1 << 20)
                    .Last(i => (adiv[i] > 0.55)) + 1
                );
        }
    }
}








Fortran: program conway
  implicit none
  integer :: a(2**20)  
  real    :: b(2**20)  
  real    :: v         
  integer :: nl(1)     
  integer :: i, N, first, second, last, m

  
  a(1:2) = [1, 1]
  b(1:2) = [1.0e0, 0.5e0]
  N = 2
  do i=1,2**20
     last = a(N)
     first = a(last)
     second = a(N-last+1)
     N = N+1
     a(N:N) = first + second
     b(N:N) = a(N:N)/real(N)
  end do

  
  m = 0
  do i=1,19
     v = maxval(b(2**i:2**(i+1)))
     nl = maxloc(b(2**i:2**(i+1)))
     write(*,'(2(a,i0),a,f8.6,a,i0)') &
          'Max. between 2**', i,      &
          ' and 2**', (i+1),          &
          ' is ', v,                  &
          ' at n = ', 2**i+nl(1)-1
     if (m == 0 .and. v < 0.55e0) then
        m = i-1
     end if
  end do

  
  do i=2**(m+1), 2**m,-1
     if (b(i) > 0.55e0) then
        exit
     end if
  end do
  write(*,'(a,i0)') 'Mallows number = ',i

end program conway
Task: Hofstadter-Conway $10,000 sequence


C++: #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;
  }
}









Fortran: program conway
  implicit none
  integer :: a(2**20)  
  real    :: b(2**20)  
  real    :: v         
  integer :: nl(1)     
  integer :: i, N, first, second, last, m

  
  a(1:2) = [1, 1]
  b(1:2) = [1.0e0, 0.5e0]
  N = 2
  do i=1,2**20
     last = a(N)
     first = a(last)
     second = a(N-last+1)
     N = N+1
     a(N:N) = first + second
     b(N:N) = a(N:N)/real(N)
  end do

  
  m = 0
  do i=1,19
     v = maxval(b(2**i:2**(i+1)))
     nl = maxloc(b(2**i:2**(i+1)))
     write(*,'(2(a,i0),a,f8.6,a,i0)') &
          'Max. between 2**', i,      &
          ' and 2**', (i+1),          &
          ' is ', v,                  &
          ' at n = ', 2**i+nl(1)-1
     if (m == 0 .and. v < 0.55e0) then
        m = i-1
     end if
  end do

  
  do i=2**(m+1), 2**m,-1
     if (b(i) > 0.55e0) then
        exit
     end if
  end do
  write(*,'(a,i0)') 'Mallows number = ',i

end program conway
Task: Hofstadter-Conway $10,000 sequence

C: #include <stdio.h>
#include <stdlib.h>

int a_list[1<<20 + 1];

int doSqnc( int m)
{
    int max_df = 0;
    int p2_max = 2;
    int v, n;
    int k1 = 2;
    int lg2 = 1;
    double amax = 0;
    a_list[0] = -50000;
    a_list[1] = a_list[2] = 1;
    v = a_list[2];

    for (n=3; n <= m;  n++) {
        v = a_list[n] = a_list[v] + a_list[n-v];
        if ( amax < v*1.0/n) amax = v*1.0/n;
        if ( 0 == (k1&n)) {
            printf("Maximum between 2^%d and 2^%d was %f\n", lg2,lg2+1, amax);
            amax = 0;
            lg2++;
        }
        k1 = n;
    }
    return 1;
}










Fortran: program conway
  implicit none
  integer :: a(2**20)  
  real    :: b(2**20)  
  real    :: v         
  integer :: nl(1)     
  integer :: i, N, first, second, last, m

  
  a(1:2) = [1, 1]
  b(1:2) = [1.0e0, 0.5e0]
  N = 2
  do i=1,2**20
     last = a(N)
     first = a(last)
     second = a(N-last+1)
     N = N+1
     a(N:N) = first + second
     b(N:N) = a(N:N)/real(N)
  end do

  
  m = 0
  do i=1,19
     v = maxval(b(2**i:2**(i+1)))
     nl = maxloc(b(2**i:2**(i+1)))
     write(*,'(2(a,i0),a,f8.6,a,i0)') &
          'Max. between 2**', i,      &
          ' and 2**', (i+1),          &
          ' is ', v,                  &
          ' at n = ', 2**i+nl(1)-1
     if (m == 0 .and. v < 0.55e0) then
        m = i-1
     end if
  end do

  
  do i=2**(m+1), 2**m,-1
     if (b(i) > 0.55e0) then
        exit
     end if
  end do
  write(*,'(a,i0)') 'Mallows number = ',i

end program conway