WeixiangYan/CodeTransOcean huggingface dataset:
Task: Sum of elements below main diagonal of matrix




Go: package main

import (
    "fmt"
    "log"
)

func main() {
    m := [][]int{
        {1, 3, 7, 8, 10},
        {2, 4, 16, 14, 4},
        {3, 1, 9, 18, 11},
        {12, 14, 17, 18, 20},
        {7, 1, 3, 9, 5},
    }
    if len(m) != len(m[0]) {
        log.Fatal("Matrix must be square.")
    }
    sum := 0
    for i := 1; i < len(m); i++ {
        for j := 0; j < i; j++ {
            sum = sum + m[i][j]
        }
    }
    fmt.Println("Sum of elements below main diagonal is", sum)
}








Ada: with Ada.Text_Io;
with Ada.Numerics.Generic_Real_Arrays;

procedure Sum_Below_Diagonals is

   type Real is new Float;

   package Real_Arrays
   is new Ada.Numerics.Generic_Real_Arrays (Real);

   function Sum_Below_Diagonal (M : Real_Arrays.Real_Matrix) return Real
   with Pre => M'Length (1) = M'Length (2)
   is
      Sum : Real := 0.0;
   begin
      for Row in 0 .. M'Length (1) - 1 loop
         for Col in 0 .. Row - 1 loop
            Sum := Sum + M (M'First (1) + Row,
                            M'First (2) + Col);
         end loop;
      end loop;
      return Sum;
   end Sum_Below_Diagonal;

   M : constant Real_Arrays.Real_Matrix :=
     (( 1.0,  3.0,  7.0,  8.0, 10.0),
      ( 2.0,  4.0, 16.0, 14.0,  4.0),
      ( 3.0,  1.0,  9.0, 18.0, 11.0),
      (12.0, 14.0, 17.0, 18.0, 20.0),
      ( 7.0,  1.0,  3.0,  9.0,  5.0));
   Sum : constant Real := Sum_Below_Diagonal (M);

   package Real_Io is new Ada.Text_Io.Float_Io (Real);
   use Ada.Text_Io, Real_Io;
begin
   Put ("Sum below diagonal: ");
   Put (Sum, Exp => 0, Aft => 1);
   New_Line;
end Sum_Below_Diagonals;
Task: Rep-string




Go: package main

import (
    "fmt"
    "strings"
)

func rep(s string) int {
    for x := len(s) / 2; x > 0; x-- {
        if strings.HasPrefix(s, s[x:]) {
            return x
        }
    }
    return 0
}

const m = `
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
0100100
101
11
00
1`

func main() {
    for _, s := range strings.Fields(m) {
        if n := rep(s); n > 0 {
            fmt.Printf("%q  %d rep-string %q\n", s, n, s[:n])
        } else {
            fmt.Printf("%q  not a rep-string\n", s)
        }
    }
}








Ada: with Ada.Command_Line, Ada.Text_IO, Ada.Strings.Fixed;

procedure Rep_String is
   
   function Find_Largest_Rep_String(S:String) return String is
      L: Natural := S'Length;
   begin
      for I in reverse 1 .. L/2 loop
	 declare
	    use Ada.Strings.Fixed;
	    T: String := S(S'First .. S'First + I-1); 
	    U: String := (1+(L/I)) * T; 
	 begin 
	    if U(U'First .. U'First + S'Length -1) = S then
	       return T; 
	    end if;
	 end;
      end loop;
      return ""; 
   end Find_Largest_Rep_String;
   
   X: String := Ada.Command_Line.Argument(1);
   Y: String := Find_Largest_Rep_String(X);
   
begin
   if Y="" then
      Ada.Text_IO.Put_Line("No rep-string for """ & X & """");
   else
      Ada.Text_IO.Put_Line("Longest rep-string for """& X &""": """& Y &"""");
   end if;
end Rep_String;
Task: Topswops




Go: 


package main

import "fmt"

const ( 
    maxn = 10 
    maxl = 50 
)

func main() {
    for i := 1; i <= maxn; i++ {
        fmt.Printf("%d: %d\n", i, steps(i))
    }
}

func steps(n int) int {
    var a, b [maxl][maxn + 1]int
    var x [maxl]int
    a[0][0] = 1
    var m int
    for l := 0; ; {
        x[l]++
        k := int(x[l])
        if k >= n {
            if l <= 0 {
                break
            }
            l--
            continue
        }
        if a[l][k] == 0 {
            if b[l][k+1] != 0 {
                continue
            }
        } else if a[l][k] != k+1 {
            continue
        }
        a[l+1] = a[l]
        for j := 1; j <= k; j++ {
            a[l+1][j] = a[l][k-j]
        }
        b[l+1] = b[l]
        a[l+1][0] = k + 1
        b[l+1][k+1] = 1
        if l > m-1 {
            m = l + 1
        }
        l++
        x[l] = 0
    }
    return m
}








Ada: with Ada.Integer_Text_IO, Generic_Perm;

procedure Topswaps is
   
   function Topswaps(Size: Positive) return Natural is
      package Perms is new Generic_Perm(Size);
      P: Perms.Permutation;
      Done: Boolean;
      Max: Natural;
      
      function Swapper_Calls(P: Perms.Permutation) return Natural is
	 Q: Perms.Permutation := P;
	 I: Perms.Element := P(1);
      begin
	 if I = 1 then 
	    return 0;
	 else
	    for Idx in 1 .. I loop
	       Q(Idx) := P(I-Idx+1);
	    end loop;
	    return 1 + Swapper_Calls(Q);
	 end if;
      end Swapper_Calls;
      
   begin
      Perms.Set_To_First(P, Done);
      Max:= Swapper_Calls(P);
      while not Done loop
	 Perms.Go_To_Next(P, Done);
	 Max := natural'Max(Max, Swapper_Calls(P));
      end loop;
      return Max;
   end Topswaps;
   
begin
   for I in 1 .. 10 loop
      Ada.Integer_Text_IO.Put(Item => Topswaps(I), Width => 3);
   end loop;
end Topswaps;
Task: Sequence_ smallest number greater than previous term with exactly n divisors




Go: package main

import "fmt"

func countDivisors(n int) int {
    count := 0
    for i := 1; i*i <= n; i++ {
        if n%i == 0 {
            if i == n/i {
                count++
            } else {
                count += 2
            }
        }
    }
    return count
}

func main() {
    const max = 15
    fmt.Println("The first", max, "terms of the sequence are:")
    for i, next := 1, 1; next <= max; i++ {
        if next == countDivisors(i) {
            fmt.Printf("%d ", i)
            next++
        }
    }
    fmt.Println()
}








Ada: with Ada.Text_IO;

procedure Show_Sequence is

   function Count_Divisors (N : in Natural) return Natural is
      Count : Natural := 0;
      I     : Natural;
   begin
      I := 1;
      while I**2 <= N loop
         if N mod I = 0 then
            if I = N / I then
               Count := Count + 1;
            else
               Count := Count + 2;
            end if;
         end if;
         I := I + 1;
      end loop;

      return Count;
   end Count_Divisors;

   procedure Show (Max : in Natural) is
      use Ada.Text_IO;
      N : Natural := 1;
   Begin
      Put_Line ("The first" & Max'Image & "terms of the sequence are:");
      for Divisors in 1 .. Max loop
         while Count_Divisors (N) /= Divisors loop
            N := N + 1;
         end loop;
         Put (N'Image);
      end loop;
      New_Line;
   end Show;

begin
   Show (15);
end Show_Sequence;
Task: McNuggets problem




Go: package main

import "fmt"

func mcnugget(limit int) {
    sv := make([]bool, limit+1) 
    for s := 0; s <= limit; s += 6 {
        for n := s; n <= limit; n += 9 {
            for t := n; t <= limit; t += 20 {
                sv[t] = true
            }
        }
    }
    for i := limit; i >= 0; i-- {
        if !sv[i] {
            fmt.Println("Maximum non-McNuggets number is", i)
            return
        }
    }
}

func main() {
    mcnugget(100)
}








Ada: with Ada.Text_IO; use Ada.Text_IO;

procedure McNugget is
   Limit : constant                      := 100;
   List  : array (0 .. Limit) of Boolean := (others => False);
   N     : Integer;
begin
   for A in 0 .. Limit / 6 loop
      for B in 0 .. Limit / 9 loop
         for C in 0 .. Limit / 20 loop
            N := A * 6 + B * 9 + C * 20;
            if N <= 100 then
               List (N) := True;
            end if;
         end loop;
      end loop;
   end loop;
   for N in reverse 1 .. Limit loop
      if not List (N) then
         Put_Line ("The largest non McNugget number is:" & Integer'Image (N));
         exit;
      end if;
   end loop;
end McNugget;
Task: Idiomatically determine all the lowercase and uppercase letters




Go: package main

import (
	"fmt"
	"unicode"
)

const (
	lcASCII = "abcdefghijklmnopqrstuvwxyz"
	ucASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

func main() {
	fmt.Println("ASCII lower case:")
	fmt.Println(lcASCII)
	for l := 'a'; l <= 'z'; l++ {
		fmt.Print(string(l))
	}
	fmt.Println()

	fmt.Println("\nASCII upper case:")
	fmt.Println(ucASCII)
	for l := 'A'; l <= 'Z'; l++ {
		fmt.Print(string(l))
	}
	fmt.Println()

	fmt.Println("\nUnicode version " + unicode.Version)
	showRange16("Lower case 16-bit code points:", unicode.Lower.R16)
	showRange32("Lower case 32-bit code points:", unicode.Lower.R32)
	showRange16("Upper case 16-bit code points:", unicode.Upper.R16)
	showRange32("Upper case 32-bit code points:", unicode.Upper.R32)
}

func showRange16(hdr string, rList []unicode.Range16) {
	fmt.Print("\n", hdr, "\n")
	fmt.Printf("%d ranges:\n", len(rList))
	for _, rng := range rList {
		fmt.Printf("%U: ", rng.Lo)
		for r := rng.Lo; r <= rng.Hi; r += rng.Stride {
			fmt.Printf("%c", r)
		}
		fmt.Println()
	}
}

func showRange32(hdr string, rList []unicode.Range32) {
	fmt.Print("\n", hdr, "\n")
	fmt.Printf("%d ranges:\n", len(rList))
	for _, rng := range rList {
		fmt.Printf("%U: ", rng.Lo)
		for r := rng.Lo; r <= rng.Hi; r += rng.Stride {
			fmt.Printf("%c", r)
		}
		fmt.Println()
	}
}








Ada: with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   subtype Lower is Character range 'a' .. 'z';
   subtype Upper is Character range 'A' .. 'Z';
begin
   Put ("Lower: ");
   for c in Lower'range loop
      Put (c);
   end loop;
   New_Line;
   Put ("Upper: ");
   for c in Upper'range loop
      Put (c);
   end loop;
   New_Line;
end Main;
Task: Superellipse




Go: package main

import (
    "github.com/fogleman/gg"
    "math"
)


func superEllipse(dc *gg.Context, n float64, a int) {
    hw := float64(dc.Width() / 2)
    hh := float64(dc.Height() / 2)

    
    y := make([]float64, a+1)
    for x := 0; x <= a; x++ {
        aa := math.Pow(float64(a), n)
        xx := math.Pow(float64(x), n)
        y[x] = math.Pow(aa-xx, 1.0/n)
    }

    
    for x := a; x >= 0; x-- {
        dc.LineTo(hw+float64(x), hh-y[x])
    }
    for x := 0; x <= a; x++ {
        dc.LineTo(hw+float64(x), hh+y[x])
    }
    for x := a; x >= 0; x-- {
        dc.LineTo(hw-float64(x), hh+y[x])
    }
    for x := 0; x <= a; x++ {
        dc.LineTo(hw-float64(x), hh-y[x])
    }

    dc.SetRGB(1, 1, 1) 
    dc.Fill()
}

func main() {
    dc := gg.NewContext(500, 500)
    dc.SetRGB(0, 0, 0) 
    dc.Clear()
    superEllipse(dc, 2.5, 200)
    dc.SavePNG("superellipse.png")
}








Ada: with Ada.Numerics.Elementary_Functions;

with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Events.Events;

procedure Superelipse is

   Width  : constant := 600;
   Height : constant := 600;
   A      : constant := 200.0;
   B      : constant := 200.0;
   N      : constant := 2.5;

   Window   : SDL.Video.Windows.Window;
   Renderer : SDL.Video.Renderers.Renderer;
   Event    : SDL.Events.Events.Events;

   procedure Draw_Superelipse
   is
      use type SDL.C.int;
      use Ada.Numerics.Elementary_Functions;
      Xx, Yy : Float;
      subtype Legal_Range is Float range 0.980 .. 1.020;
   begin
      for Y in 0 .. Height loop
         for X in 0 .. Width loop
            Xx := Float (X - Width  / 2);
            Yy := Float (Y - Height / 2);
            if (abs (Xx / A)) ** N + (abs (Yy / B)) ** N in Legal_Range then
               Renderer.Draw (Point => (X => Width  / 2 + SDL.C.int (Xx),
                                        Y => Height / 2 - SDL.C.int (Yy)));
            end if;

         end loop;
      end loop;
   end Draw_Superelipse;

   procedure Wait is
      use type SDL.Events.Event_Types;
   begin
      loop
         while SDL.Events.Events.Poll (Event) loop
            if Event.Common.Event_Type = SDL.Events.Quit then
               return;
            end if;
         end loop;
         delay 0.100;
      end loop;
   end Wait;

begin
   if not SDL.Initialise (Flags => SDL.Enable_Screen) then
      return;
   end if;

   SDL.Video.Windows.Makers.Create (Win      => Window,
                                    Title    => "Superelipse",
                                    Position => SDL.Natural_Coordinates'(X => 10, Y => 10),
                                    Size     => SDL.Positive_Sizes'(Width, Height),
                                    Flags    => 0);
   SDL.Video.Renderers.Makers.Create (Renderer, Window.Get_Surface);
   Renderer.Set_Draw_Colour ((0, 0, 0, 255));
   Renderer.Fill (Rectangle => (0, 0, Width, Height));
   Renderer.Set_Draw_Colour ((0, 220, 0, 255));

   Draw_Superelipse;
   Window.Update_Surface;

   Wait;
   Window.Finalize;
   SDL.Finalise;
end Superelipse;
Task: Mutex




Go: package main

import (
    "fmt"
    "sync"
    "time"
)

var value int
var m sync.Mutex
var wg sync.WaitGroup

func slowInc() {
    m.Lock()
    v := value
    time.Sleep(1e8)
    value = v+1
    m.Unlock()
    wg.Done()
}

func main() {
    wg.Add(2)
    go slowInc()
    go slowInc()
    wg.Wait()
    fmt.Println(value)
}








Ada: protected type Mutex is
   entry Seize;
   procedure Release;
private
   Owned : Boolean := False;
end Mutex;
Task: Odd word problem




Go: package main

import (
    "bytes"
    "fmt"
    "io"
    "os"
    "unicode"
)

func main() {
    owp(os.Stdout, bytes.NewBufferString("what,is,the;meaning,of:life."))
    fmt.Println()
    owp(os.Stdout, bytes.NewBufferString("we,are;not,in,kansas;any,more."))
    fmt.Println()
}

func owp(dst io.Writer, src io.Reader) {
    byte_in := func () byte {
        bs := make([]byte, 1)
        src.Read(bs)
        return bs[0]
    }
    byte_out := func (b byte) { dst.Write([]byte{b}) }    
    var odd func() byte
    odd = func() byte {
        s := byte_in()
        if unicode.IsPunct(rune(s)) {
            return s
        }
        b := odd()
        byte_out(s)
        return b
    }
    for {
        for {
            b := byte_in()
            byte_out(b)
            if b == '.' {
                return
            }
            if unicode.IsPunct(rune(b)) {
                break
            }
        }
        b := odd()
        byte_out(b)
        if b == '.' {
            return
        }
    }
}








Ada: with Ada.Text_IO;
 
procedure Odd_Word_Problem is
 
   use Ada.Text_IO; 
 
   function Current return Character is
      
      End_Of_Line: Boolean;
      C: Character;
   begin
      Look_Ahead(C, End_Of_Line);
      if End_Of_Line then
         raise Constraint_Error with "end of line before the terminating '.'";
      end if;
      return C;
   end Current;
 
   procedure Skip is
      
      C: Character;
   begin
      Get(C);
   end Skip;
 
   function Is_Alpha(Ch: Character) return Boolean is
   begin
      return (Ch in  'a' .. 'z') or (Ch in 'A' .. 'Z');
   end Is_Alpha;
 
   procedure Odd_Word(C: Character) is
   begin
      if Is_Alpha(C) then
         Skip;
         Odd_Word(Current);
         Put(C);
      end if;
   end Odd_Word;
 
begin 
   Put(Current);
   while Is_Alpha(Current) loop 
      Skip;
      Put(Current);
   end loop;
   if Current /= '.' then 
      Skip;
      Odd_Word(Current);
      Put(Current);
      if Current /= '.' then 
         Skip;
         Odd_Word_Problem;
      end if;
   end if;
end Odd_Word_Problem;
Task: Pseudo-random numbers_PCG32




Go: package main

import (
    "fmt"
    "math"
)

const CONST = 6364136223846793005

type Pcg32 struct{ state, inc uint64 }

func Pcg32New() *Pcg32 { return &Pcg32{0x853c49e6748fea9b, 0xda3e39cb94b95bdb} }

func (pcg *Pcg32) seed(seedState, seedSequence uint64) {
    pcg.state = 0
    pcg.inc = (seedSequence << 1) | 1
    pcg.nextInt()
    pcg.state = pcg.state + seedState
    pcg.nextInt()
}

func (pcg *Pcg32) nextInt() uint32 {
    old := pcg.state
    pcg.state = old*CONST + pcg.inc
    pcgshifted := uint32(((old >> 18) ^ old) >> 27)
    rot := uint32(old >> 59)
    return (pcgshifted >> rot) | (pcgshifted << ((-rot) & 31))
}

func (pcg *Pcg32) nextFloat() float64 {
    return float64(pcg.nextInt()) / (1 << 32)
}

func main() {
    randomGen := Pcg32New()
    randomGen.seed(42, 54)
    for i := 0; i < 5; i++ {
        fmt.Println(randomGen.nextInt())
    }

    var counts [5]int
    randomGen.seed(987654321, 1)
    for i := 0; i < 1e5; i++ {
        j := int(math.Floor(randomGen.nextFloat() * 5))
        counts[j]++
    }
    fmt.Println("\nThe counts for 100,000 repetitions are:")
    for i := 0; i < 5; i++ {
        fmt.Printf("  %d : %d\n", i, counts[i])
    }
}








Ada: with Interfaces; use Interfaces;

package random_pcg32 is
   function Next_Int return Unsigned_32;
   function Next_Float return Long_Float;
   procedure Seed (seed_state : Unsigned_64; seed_sequence : Unsigned_64);
end random_pcg32;
Task: Memory layout of a data structure




Go: package main

import "fmt"

type rs232p9 uint16

const (
	CD9  rs232p9 = 1 << iota 
	RD9                      
	TD9                      
	DTR9                     
	SG9                      
	DSR9                     
	RTS9                     
	CTS9                     
	RI9                      
)

func main() {
	
	p := RI9 | TD9 | CD9
	fmt.Printf("Type=%T value=%#04x\n", p, p)
}








Ada: type Bit is mod 2;
type Rs_232_Layout is record
   Carrier_Detect      : Bit;
   Received_Data       : Bit;
   Transmitted_Data    : Bit;
   Data_Terminal_ready : Bit;
   Signal_Ground       : Bit;
   Data_Set_Ready      : Bit;
   Request_To_Send     : Bit;
   Clear_To_Send       : Bit;
   Ring_Indicator      : Bit;
end record;

for Rs_232_Layout use record
   Carrier_Detect      at 0 range 0..0;
   Received_Data       at 0 range 1..1;
   Transmitted_Data    at 0 range 2..2;
   Data_Terminal_Ready at 0 range 3..3;
   Signal_Ground       at 0 range 4..4;
   Data_Set_Ready      at 0 range 5..5;
   Request_To_Send     at 0 range 6..6;
   Clear_To_Send       at 0 range 7..7;
   Ring_Indicator      at 0 range 8..8;
end record;
Task: Largest proper divisor of n




Go: package main

import "fmt"

func largestProperDivisor(n int) int {
    for i := 2; i*i <= n; i++ {
        if n%i == 0 {
            return n / i
        }
    }
    return 1
}

func main() {
    fmt.Println("The largest proper divisors for numbers in the interval [1, 100] are:")
    fmt.Print(" 1  ")
    for n := 2; n <= 100; n++ {
        if n%2 == 0 {
            fmt.Printf("%2d  ", n/2)
        } else {
            fmt.Printf("%2d  ", largestProperDivisor(n))
        }
        if n%10 == 0 {
            fmt.Println()
        }
    }
}








Ada: with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   subtype param_type is Integer range 1 .. 100;
   function lpd (n : in param_type) return param_type is
      result : param_type := 1;
   begin
      for divisor in reverse 1 .. n / 2 loop
         if n rem divisor = 0 then
            result := divisor;
            exit;
         end if;
      end loop;
      return result;
   end lpd;

begin
   for I in param_type loop
      Put (Item => lpd (I), Width => 3);
      if I rem 10 = 0 then
         New_Line;
      end if;
   end loop;
end Main;
Task: Koch curve




Go: package main

import (
    "github.com/fogleman/gg"
    "math"
)

var dc = gg.NewContext(512, 512)

func koch(x1, y1, x2, y2 float64, iter int) {
    angle := math.Pi / 3 
    x3 := (x1*2 + x2) / 3
    y3 := (y1*2 + y2) / 3
    x4 := (x1 + x2*2) / 3
    y4 := (y1 + y2*2) / 3
    x5 := x3 + (x4-x3)*math.Cos(angle) + (y4-y3)*math.Sin(angle)
    y5 := y3 - (x4-x3)*math.Sin(angle) + (y4-y3)*math.Cos(angle)
    if iter > 0 {
        iter--
        koch(x1, y1, x3, y3, iter)
        koch(x3, y3, x5, y5, iter)
        koch(x5, y5, x4, y4, iter)
        koch(x4, y4, x2, y2, iter)
    } else {
        dc.LineTo(x1, y1)
        dc.LineTo(x3, y3)
        dc.LineTo(x5, y5)
        dc.LineTo(x4, y4)
        dc.LineTo(x2, y2)
    }
}

func main() {
    dc.SetRGB(1, 1, 1) 
    dc.Clear()
    koch(100, 100, 400, 400, 4)
    dc.SetRGB(0, 0, 1) 
    dc.SetLineWidth(2)
    dc.Stroke()
    dc.SavePNG("koch.png")
}








Ada: with Ada.Command_Line;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO;
with PDF_Out;

procedure Koch_Curve is

   package Real_Math is
     new Ada.Numerics.Generic_Elementary_Functions (PDF_Out.Real);
   use Real_Math, PDF_Out, Ada.Command_Line, Ada.Text_IO;

   subtype Angle_Deg  is Real;
   type    Level_Type is range 0 .. 7;

   Purple : constant Color_Type := (0.7, 0.0, 0.5);
   Length : constant Real       := 400.0;
   Corner : constant Point      := (90.0, 580.0);

   Level     : Level_Type;
   Current   : Point      := (0.0, 0.0);
   Direction : Angle_Deg  := Angle_Deg'(60.0);
   Doc       : PDF_Out_File;

   procedure Koch (Level : Level_Type; Length : Real) is
   begin
      if Level = 0 then
         Current := Current + Length * Point'(Sin (Direction, 360.0),
                                              Cos (Direction, 360.0));
         Doc.Line (Corner + Current);
      else
         Koch (Level - 1, Length / 3.0);  Direction := Direction -  60.0;
         Koch (Level - 1, Length / 3.0);  Direction := Direction + 120.0;
         Koch (Level - 1, Length / 3.0);  Direction := Direction -  60.0;
         Koch (Level - 1, Length / 3.0);
      end if;
   end Koch;

begin
   if Argument_Count /= 1 then
      Put_Line ("koch_curve <level>");
      Put_Line ("  <level>   0 .. 7");
      Put_Line ("open koch.pdf to view ouput");
      return;
   end if;
   Level := Level_Type'Value (Argument (1));

   Doc.Create ("koch.pdf");
   Doc.Page_Setup (A4_Portrait);
   Doc.Margins (Margins_Type'(Left   => Cm_2_5,
                              others => One_cm));
   Doc.Color (Purple);
   Doc.Move (Corner);
   for A in 1 .. 3 loop
      Koch (Level, Length);
      Direction := Direction + 120.0;
   end loop;
   Doc.Finish_Path (Close_Path => True,
                    Rendering  => Fill,
                    Rule       => Even_Odd);
   Doc.Close;
end Koch_Curve;
Task: Pseudo-random numbers_Xorshift star




Go: package main

import (
    "fmt"
    "math"
)

const CONST = 0x2545F4914F6CDD1D

type XorshiftStar struct{ state uint64 }

func XorshiftStarNew(state uint64) *XorshiftStar { return &XorshiftStar{state} }

func (xor *XorshiftStar) seed(state uint64) { xor.state = state }

func (xor *XorshiftStar) nextInt() uint32 {
    x := xor.state
    x = x ^ (x >> 12)
    x = x ^ (x << 25)
    x = x ^ (x >> 27)
    xor.state = x
    return uint32((x * CONST) >> 32)
}

func (xor *XorshiftStar) nextFloat() float64 {
    return float64(xor.nextInt()) / (1 << 32)
}

func main() {
    randomGen := XorshiftStarNew(1234567)
    for i := 0; i < 5; i++ {
        fmt.Println(randomGen.nextInt())
    }

    var counts [5]int
    randomGen.seed(987654321)
    for i := 0; i < 1e5; i++ {
        j := int(math.Floor(randomGen.nextFloat() * 5))
        counts[j]++
    }
    fmt.Println("\nThe counts for 100,000 repetitions are:")
    for i := 0; i < 5; i++ {
        fmt.Printf("  %d : %d\n", i, counts[i])
    }
}








Ada: with Interfaces;  use Interfaces;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   const : constant Unsigned_64 := 16#2545_F491_4F6C_DD1D#;
   state : Unsigned_64          := 0;
   Unseeded_Error : exception;

   procedure seed (num : Unsigned_64) is
   begin
      state := num;
   end seed;

   function Next_Int return Unsigned_32 is
      x : Unsigned_64 := state;
   begin
      if state = 0 then
         raise Unseeded_Error;
      end if;

      x     := x xor (x / 2**12);
      x     := x xor (x * 2**25);
      x     := x xor (x / 2**27);
      state := x;
      return Unsigned_32 ((x * const) / 2**32);
   end Next_Int;

   function Next_Float return Long_Float is
   begin
      return Long_Float (Next_Int) / 2.0**32;
   end Next_Float;

   counts : array (0 .. 4) of Natural := (others => 0);
   J      : Natural;
begin
   seed (1_234_567);
   for I in 1 .. 5 loop
      Put_Line (Unsigned_32'Image (Next_Int));
   end loop;

   seed (987_654_321);
   for I in 1 .. 100_000 loop
      J          := Natural (Long_Float'Floor (Next_Float * 5.0));
      counts (J) := counts (J) + 1;
   end loop;

   New_Line;
   for I in counts'Range loop
      Put_Line (I'Image & " :" & counts (I)'Image);
   end loop;

end Main;