Task: Stern-Brocot sequence








VB: Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Module1
    Dim l As List(Of Integer) = {1, 1}.ToList()

    Function gcd(ByVal a As Integer, ByVal b As Integer) As Integer
        Return If(a > 0, If(a < b, gcd(b Mod a, a), gcd(a Mod b, b)), b)
    End Function

    Sub Main(ByVal args As String())
        Dim max As Integer = 1000, take As Integer = 15, i As Integer = 1,
            selection As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100}
        Do : l.AddRange({l(i) + l(i - 1), l(i)}.ToList) : i += 1
        Loop While l.Count < max OrElse l(l.Count - 2) <> selection.Last()
        Console.Write("The first {0} items In the Stern-Brocot sequence: ", take)
        Console.WriteLine("{0}" & vbLf, String.Join(", ", l.Take(take)))
        Console.WriteLine("The locations of where the selected numbers (1-to-10, & 100) first appear:")
        For Each ii As Integer In selection
            Dim j As Integer = l.FindIndex(Function(x) x = ii) + 1
            Console.WriteLine("{0,3}: {1:n0}", ii, j)
        Next : Console.WriteLine() : Dim good As Boolean = True : For i = 1 To max
            If gcd(l(i), l(i - 1)) <> 1 Then good = False : Exit For
        Next
        Console.WriteLine("The greatest common divisor of all the two consecutive items of the" &
                          " series up to the {0}th item is {1}always one.", max, If(good, "", "not "))
    End Sub
End Module




Ada: with Ada.Text_IO, Ada.Containers.Vectors;

procedure Sequence is
   
   package Vectors is new 
     Ada.Containers.Vectors(Index_Type => Positive, Element_Type => Positive);
   use type Vectors.Vector;
   
   type Sequence is record
      List: Vectors.Vector;
      Index: Positive;
      
      
      
      
   end record;
   
   function Initialize return Sequence is
      (List => (Vectors.Empty_Vector & 1 & 1), Index => 2);
      
   function Get(Seq: in out Sequence; Location: Positive) return Positive is
      
      
      That: Positive := Seq.List.Element(Seq.Index);
      This: Positive := That + Seq.List.Element(Seq.Index-1);
   begin
      while Seq.List.Last_Index < Location loop 
	 Seq.List := Seq.List & This & That;
	 Seq.Index := Seq.Index + 1;
      end loop;
      return Seq.List.Element(Location);
   end Get;
   
   S: Sequence := Initialize;
   J: Positive;
   
   use Ada.Text_IO;
   
begin
   
   Put("First 15:");
   for I in 1 .. 15 loop
      Put(Integer'Image(Get(S, I)));
   end loop;
   New_Line;
   
   
   for I in 1 .. 10 loop
      J := 1;
      while Get(S, J) /= I loop
	 J := J + 1;
      end loop;
      Put("First" & Integer'Image(I) & " at" & Integer'Image(J) & ";  ");
      if I mod 4 = 0 then
	 New_Line; 
      end if;
   end loop;
   
   
   J := 1;
   while Get(S, J) /= 100 loop
      J := J + 1;
   end loop;
   Put_Line("First 100 at" & Integer'Image(J) & ".");
   
   
   declare
      function GCD (A, B : Integer) return Integer is
	 M : Integer := A;
	 N : Integer := B;
	 T : Integer;
      begin
	 while N /= 0 loop
	    T := M;
	    M := N;
	    N := T mod N;
	 end loop;
	 return M;
      end GCD;
      
      A, B: Positive;
   begin
      for I in 1 .. 999 loop
	 A := Get(S, I);
	 B := Get(S, I+1);
	 if GCD(A, B) /= 1 then
	    raise Constraint_Error;
	 end if;
      end loop;
      Put_Line("Correct: The first 999 consecutive pairs are relative prime!");
   exception
      when Constraint_Error => Put_Line("Some GCD > 1; this is wrong!!!") ;
   end;
end Sequence;