WeixiangYan/CodeTransOcean huggingface dataset:
Task: Pascal's triangle




Go: package main

import "fmt"

func printTriangle(n int) {
    
    if n <= 0 {
        return
    }
    fmt.Println(1)
    if n == 1 {
        return
    }
    
    a := make([]int, (n+1)/2)
    a[0] = 1
    for row, middle := 1, 0; row < n; row++ {
        
        even := row&1 == 0
        if even {
            a[middle+1] = a[middle] * 2
        }
        for i := middle; i > 0; i-- {
            a[i] += a[i-1]
        }
        
        for i := 0; i <= middle; i++ {
            fmt.Print(a[i], " ")
        }
        if even {
            middle++
        }
        for i := middle; i >= 0; i-- {
            fmt.Print(a[i], " ")
        }
        fmt.Println("")
    }
}

func main() {
    printTriangle(4)
}


Java: import java.util.ArrayList;
...
public static void genPyrN(int rows){
	if(rows < 0) return;
	
	ArrayList<Integer> last = new ArrayList<Integer>();
	last.add(1);
	System.out.println(last);
	for(int i= 1;i <= rows;++i){
		
		ArrayList<Integer> thisRow= new ArrayList<Integer>();
		thisRow.add(last.get(0)); 
		for(int j= 1;j < i;++j){
			
			thisRow.add(last.get(j - 1) + last.get(j));
		}
		thisRow.add(last.get(0)); 
		last= thisRow;
		System.out.println(thisRow);
	}
}
Task: Pascal's triangle





Java: import java.util.ArrayList;
...
public static void genPyrN(int rows){
	if(rows < 0) return;
	
	ArrayList<Integer> last = new ArrayList<Integer>();
	last.add(1);
	System.out.println(last);
	for(int i= 1;i <= rows;++i){
		
		ArrayList<Integer> thisRow= new ArrayList<Integer>();
		thisRow.add(last.get(0)); 
		for(int j= 1;j < i;++j){
			
			thisRow.add(last.get(j - 1) + last.get(j));
		}
		thisRow.add(last.get(0)); 
		last= thisRow;
		System.out.println(thisRow);
	}
}



Python: def pascal(n):
   
   row = [1]
   k = [0]
   for x in range(max(n,0)):
      print row
      row=[l+r for l,r in zip(row+k,k+row)]
   return n>=1
Task: Pascal's triangle





Java: import java.util.ArrayList;
...
public static void genPyrN(int rows){
	if(rows < 0) return;
	
	ArrayList<Integer> last = new ArrayList<Integer>();
	last.add(1);
	System.out.println(last);
	for(int i= 1;i <= rows;++i){
		
		ArrayList<Integer> thisRow= new ArrayList<Integer>();
		thisRow.add(last.get(0)); 
		for(int j= 1;j < i;++j){
			
			thisRow.add(last.get(j - 1) + last.get(j));
		}
		thisRow.add(last.get(0)); 
		last= thisRow;
		System.out.println(thisRow);
	}
}




VB: Option Base 1
Private Sub pascal_triangle(n As Integer)
    Dim odd() As String
    Dim eve() As String
    ReDim odd(1)
    ReDim eve(2)
    odd(1) = "  1"
    For i = 1 To n
        If i Mod 2 = 1 Then
            Debug.Print String$(2 * n - 2 * i, " ") & Join(odd, " ")
            eve(1) = "  1"
            ReDim Preserve eve(i + 1)
            For j = 2 To i
                eve(j) = Format(CStr(Val(odd(j - 1)) + Val(odd(j))), "@@@")
            Next j
            eve(i + 1) = "  1"
        Else
            Debug.Print String$(2 * n - 2 * i, " ") & Join(eve, " ")
            odd(1) = "  1"
            ReDim Preserve odd(i + 1)
            For j = 2 To i
                odd(j) = Format(CStr(Val(eve(j - 1)) + Val(eve(j))), "@@@")
            Next j
            odd(i + 1) = "  1"
        End If
    Next i
End Sub
Public Sub main()
    pascal_triangle 13
End Sub
Task: Pascal's triangle


C++: #include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
void Pascal_Triangle(int size) {

	int a[100][100];
	int i, j;

	
	for (i = 1; i <= size; i++) {
		a[i][1] = a[1][i] = 1;
	}
	
	
	for (i = 2; i <= size; i++) {
		for (j = 2; j <= size - i; j++) {
			if (a[i - 1][j] == 0 || a[i][j - 1] == 0) {
				break;
			}
			a[i][j] = a[i - 1][j] + a[i][j - 1];
		}
	}

	
	
	
	
	int row,space;
	for (i = 1; i < size; i++) {
		space=row=i;
		j=1;
		
		while(space<=size+(size-i)+1){
			 cout<<" ";
			 space++;
		 }
		
		while(j<=i){
			if (a[row][j] == 0){
				   break;
			   }
			
			if(j==1){
				printf("%d",a[row--][j++]);
			}
			else
				printf("%6d",a[row--][j++]);
		}
			cout<<"\n\n";
	}
	
}

int main()
{
	
	
	int size;
	cin>>size;
	Pascal_Triangle(size);
}

}


C#: using System;

namespace RosettaCode {

    class PascalsTriangle {

        public static void CreateTriangle(int n) {
            if (n > 0) {
                for (int i = 0; i < n; i++) {
                    int c = 1;
                    Console.Write(" ".PadLeft(2 * (n - 1 - i)));
                    for (int k = 0; k <= i; k++) {
                        Console.Write("{0}", c.ToString().PadLeft(3));
                        c = c * (i - k) / (k + 1);
                    }
                    Console.WriteLine();
                }
            }
        }

        public static void Main() {
            CreateTriangle(8);
        }
    }
}
Task: Pascal's triangle



C#: using System;

namespace RosettaCode {

    class PascalsTriangle {

        public static void CreateTriangle(int n) {
            if (n > 0) {
                for (int i = 0; i < n; i++) {
                    int c = 1;
                    Console.Write(" ".PadLeft(2 * (n - 1 - i)));
                    for (int k = 0; k <= i; k++) {
                        Console.Write("{0}", c.ToString().PadLeft(3));
                        c = c * (i - k) / (k + 1);
                    }
                    Console.WriteLine();
                }
            }
        }

        public static void Main() {
            CreateTriangle(8);
        }
    }
}




PHP: <?php

function tre($n) {
  $ck=1;
  $kn=$n+1;
    
 if($kn%2==0) {
 $kn=$kn/2;
 $i=0;
  }
 else
  {

  $kn+=1;
  $kn=$kn/2;
  $i= 1;
}

 for ($k = 1; $k <= $kn-1; $k++) { 
   $ck = $ck/$k*($n-$k+1);
   $arr[] = $ck;
   echo  "+" . $ck ;
 
  }
 

if ($kn>1) {
  echo $arr[i];
  $arr=array_reverse($arr);
 for ($i; $i<= $kn-1; $i++) {
 echo  "+" . $arr[$i]  ;
     }
 
   }
 
 }

 while ($n<=20) {
 ++$n;
 echo tre($n);
 echo "<br/>";
}
 
 
?>
Task: Pascal's triangle


C++: #include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
void Pascal_Triangle(int size) {

	int a[100][100];
	int i, j;

	
	for (i = 1; i <= size; i++) {
		a[i][1] = a[1][i] = 1;
	}
	
	
	for (i = 2; i <= size; i++) {
		for (j = 2; j <= size - i; j++) {
			if (a[i - 1][j] == 0 || a[i][j - 1] == 0) {
				break;
			}
			a[i][j] = a[i - 1][j] + a[i][j - 1];
		}
	}

	
	
	
	
	int row,space;
	for (i = 1; i < size; i++) {
		space=row=i;
		j=1;
		
		while(space<=size+(size-i)+1){
			 cout<<" ";
			 space++;
		 }
		
		while(j<=i){
			if (a[row][j] == 0){
				   break;
			   }
			
			if(j==1){
				printf("%d",a[row--][j++]);
			}
			else
				printf("%6d",a[row--][j++]);
		}
			cout<<"\n\n";
	}
	
}

int main()
{
	
	
	int size;
	cin>>size;
	Pascal_Triangle(size);
}

}





PHP: <?php

function tre($n) {
  $ck=1;
  $kn=$n+1;
    
 if($kn%2==0) {
 $kn=$kn/2;
 $i=0;
  }
 else
  {

  $kn+=1;
  $kn=$kn/2;
  $i= 1;
}

 for ($k = 1; $k <= $kn-1; $k++) { 
   $ck = $ck/$k*($n-$k+1);
   $arr[] = $ck;
   echo  "+" . $ck ;
 
  }
 

if ($kn>1) {
  echo $arr[i];
  $arr=array_reverse($arr);
 for ($i; $i<= $kn-1; $i++) {
 echo  "+" . $arr[$i]  ;
     }
 
   }
 
 }

 while ($n<=20) {
 ++$n;
 echo tre($n);
 echo "<br/>";
}
 
 
?>
Task: Pascal's triangle




Go: package main

import "fmt"

func printTriangle(n int) {
    
    if n <= 0 {
        return
    }
    fmt.Println(1)
    if n == 1 {
        return
    }
    
    a := make([]int, (n+1)/2)
    a[0] = 1
    for row, middle := 1, 0; row < n; row++ {
        
        even := row&1 == 0
        if even {
            a[middle+1] = a[middle] * 2
        }
        for i := middle; i > 0; i-- {
            a[i] += a[i-1]
        }
        
        for i := 0; i <= middle; i++ {
            fmt.Print(a[i], " ")
        }
        if even {
            middle++
        }
        for i := middle; i >= 0; i-- {
            fmt.Print(a[i], " ")
        }
        fmt.Println("")
    }
}

func main() {
    printTriangle(4)
}



PHP: <?php

function tre($n) {
  $ck=1;
  $kn=$n+1;
    
 if($kn%2==0) {
 $kn=$kn/2;
 $i=0;
  }
 else
  {

  $kn+=1;
  $kn=$kn/2;
  $i= 1;
}

 for ($k = 1; $k <= $kn-1; $k++) { 
   $ck = $ck/$k*($n-$k+1);
   $arr[] = $ck;
   echo  "+" . $ck ;
 
  }
 

if ($kn>1) {
  echo $arr[i];
  $arr=array_reverse($arr);
 for ($i; $i<= $kn-1; $i++) {
 echo  "+" . $arr[$i]  ;
     }
 
   }
 
 }

 while ($n<=20) {
 ++$n;
 echo tre($n);
 echo "<br/>";
}
 
 
?>
Task: Pascal's triangle





Java: import java.util.ArrayList;
...
public static void genPyrN(int rows){
	if(rows < 0) return;
	
	ArrayList<Integer> last = new ArrayList<Integer>();
	last.add(1);
	System.out.println(last);
	for(int i= 1;i <= rows;++i){
		
		ArrayList<Integer> thisRow= new ArrayList<Integer>();
		thisRow.add(last.get(0)); 
		for(int j= 1;j < i;++j){
			
			thisRow.add(last.get(j - 1) + last.get(j));
		}
		thisRow.add(last.get(0)); 
		last= thisRow;
		System.out.println(thisRow);
	}
}


PHP: <?php

function tre($n) {
  $ck=1;
  $kn=$n+1;
    
 if($kn%2==0) {
 $kn=$kn/2;
 $i=0;
  }
 else
  {

  $kn+=1;
  $kn=$kn/2;
  $i= 1;
}

 for ($k = 1; $k <= $kn-1; $k++) { 
   $ck = $ck/$k*($n-$k+1);
   $arr[] = $ck;
   echo  "+" . $ck ;
 
  }
 

if ($kn>1) {
  echo $arr[i];
  $arr=array_reverse($arr);
 for ($i; $i<= $kn-1; $i++) {
 echo  "+" . $arr[$i]  ;
     }
 
   }
 
 }

 while ($n<=20) {
 ++$n;
 echo tre($n);
 echo "<br/>";
}
 
 
?>
Task: Pascal's triangle






PHP: <?php

function tre($n) {
  $ck=1;
  $kn=$n+1;
    
 if($kn%2==0) {
 $kn=$kn/2;
 $i=0;
  }
 else
  {

  $kn+=1;
  $kn=$kn/2;
  $i= 1;
}

 for ($k = 1; $k <= $kn-1; $k++) { 
   $ck = $ck/$k*($n-$k+1);
   $arr[] = $ck;
   echo  "+" . $ck ;
 
  }
 

if ($kn>1) {
  echo $arr[i];
  $arr=array_reverse($arr);
 for ($i; $i<= $kn-1; $i++) {
 echo  "+" . $arr[$i]  ;
     }
 
   }
 
 }

 while ($n<=20) {
 ++$n;
 echo tre($n);
 echo "<br/>";
}
 
 
?>


Python: def pascal(n):
   
   row = [1]
   k = [0]
   for x in range(max(n,0)):
      print row
      row=[l+r for l,r in zip(row+k,k+row)]
   return n>=1
Task: Pascal's triangle






PHP: <?php

function tre($n) {
  $ck=1;
  $kn=$n+1;
    
 if($kn%2==0) {
 $kn=$kn/2;
 $i=0;
  }
 else
  {

  $kn+=1;
  $kn=$kn/2;
  $i= 1;
}

 for ($k = 1; $k <= $kn-1; $k++) { 
   $ck = $ck/$k*($n-$k+1);
   $arr[] = $ck;
   echo  "+" . $ck ;
 
  }
 

if ($kn>1) {
  echo $arr[i];
  $arr=array_reverse($arr);
 for ($i; $i<= $kn-1; $i++) {
 echo  "+" . $arr[$i]  ;
     }
 
   }
 
 }

 while ($n<=20) {
 ++$n;
 echo tre($n);
 echo "<br/>";
}
 
 
?>



VB: Option Base 1
Private Sub pascal_triangle(n As Integer)
    Dim odd() As String
    Dim eve() As String
    ReDim odd(1)
    ReDim eve(2)
    odd(1) = "  1"
    For i = 1 To n
        If i Mod 2 = 1 Then
            Debug.Print String$(2 * n - 2 * i, " ") & Join(odd, " ")
            eve(1) = "  1"
            ReDim Preserve eve(i + 1)
            For j = 2 To i
                eve(j) = Format(CStr(Val(odd(j - 1)) + Val(odd(j))), "@@@")
            Next j
            eve(i + 1) = "  1"
        Else
            Debug.Print String$(2 * n - 2 * i, " ") & Join(eve, " ")
            odd(1) = "  1"
            ReDim Preserve odd(i + 1)
            For j = 2 To i
                odd(j) = Format(CStr(Val(eve(j - 1)) + Val(eve(j))), "@@@")
            Next j
            odd(i + 1) = "  1"
        End If
    Next i
End Sub
Public Sub main()
    pascal_triangle 13
End Sub
Task: Pathological floating point problems

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

void firstCase(){
	mpf_t a,b,c;
	
	mpf_inits(a,b,c,NULL);
	
	mpf_set_str(a,"0.1",10);
	mpf_set_str(b,"0.2",10);
	mpf_add(c,a,b);
	
	gmp_printf("\n0.1 + 0.2 = %.*Ff",20,c);
}

void pathologicalSeries(){
	int n;
	mpf_t v1, v2, vn, a1, a2, a3, t2, t3, prod;
	
	mpf_inits(v1,v2,vn, a1, a2, a3, t2, t3, prod,NULL);
	
	mpf_set_str(v1,"2",10);
	mpf_set_str(v2,"-4",10);
	mpf_set_str(a1,"111",10);
	mpf_set_str(a2,"1130",10);
	mpf_set_str(a3,"3000",10);
	
	for(n=3;n<=100;n++){
		mpf_div(t2,a2,v2);
		mpf_mul(prod,v1,v2);
		mpf_div(t3,a3,prod);
		mpf_add(vn,a1,t3);
		mpf_sub(vn,vn,t2);
		
		if((n>=3&&n<=8) || n==20 || n==30 || n==50 || n==100){
			gmp_printf("\nv_%d : %.*Ff",n,(n==3)?1:(n>=4&&n<=7)?6:(n==8)?7:(n==20)?16:(n==30)?24:(n==50)?40:78,vn);
		}
		
		mpf_set(v1,v2);
		mpf_set(v2,vn);
	}
}

void healthySeries(){
	int n;
	
	mpf_t num,denom,result;
	mpq_t v1, v2, vn, a1, a2, a3, t2, t3, prod;
	
	mpf_inits(num,denom,result,NULL);
	mpq_inits(v1,v2,vn, a1, a2, a3, t2, t3, prod,NULL);
	
	mpq_set_str(v1,"2",10);
	mpq_set_str(v2,"-4",10);
	mpq_set_str(a1,"111",10);
	mpq_set_str(a2,"1130",10);
	mpq_set_str(a3,"3000",10);
	
	for(n=3;n<=100;n++){
		mpq_div(t2,a2,v2);
		mpq_mul(prod,v1,v2);
		mpq_div(t3,a3,prod);
		mpq_add(vn,a1,t3);
		mpq_sub(vn,vn,t2);
		
		if((n>=3&&n<=8) || n==20 || n==30 || n==50 || n==100){
			mpf_set_z(num,mpq_numref(vn));
			mpf_set_z(denom,mpq_denref(vn));
			mpf_div(result,num,denom);

			gmp_printf("\nv_%d : %.*Ff",n,(n==3)?1:(n>=4&&n<=7)?6:(n==8)?7:(n==20)?16:(n==30)?24:(n==50)?40:78,result);
		}
		
		mpq_set(v1,v2);
		mpq_set(v2,vn);
	}
}

int main()
{	
	mpz_t rangeProd;
	
	firstCase();
	
	printf("\n\nPathological Series : ");
	
	pathologicalSeries();
	
	printf("\n\nNow a bit healthier : ");
	
	healthySeries();

	return 0;
}







Python: from fractions import Fraction

def muller_seq(n:int) -> float:
    seq = [Fraction(0), Fraction(2), Fraction(-4)]
    for i in range(3, n+1):
        next_value = (111 - 1130/seq[i-1]
            + 3000/(seq[i-1]*seq[i-2]))
        seq.append(next_value)
    return float(seq[n])

for n in [3, 4, 5, 6, 7, 8, 20, 30, 50, 100]:
    print("{:4d} -> {}".format(n, muller_seq(n)))
Task: Pathological floating point problems



C#: #define USE_BIGRATIONAL
#define BANDED_ROWS
#define INCREASED_LIMITS

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Numerics;
using Numerics;

using static Common;
using static Task1;
using static Task2;
using static Task3;

#if !USE_BIGRATIONAL

struct BigRational
{
    public override string ToString() => "NOT USING BIGRATIONAL";
    public static explicit operator decimal(BigRational value) => -1;
}
#endif

static class Common
{
    public const string FMT_STR = "{0,4}   {1,-15:G9}   {2,-24:G17}   {3,-32}   {4,-32}";
    public static string Headings { get; } =
        string.Format(
            CultureInfo.InvariantCulture,
            FMT_STR,
            new[] { "N", "Single", "Double", "Decimal", "BigRational (rounded as Decimal)" });

    [Conditional("BANDED_ROWS")]
    static void SetConsoleFormat(int n)
    {
        if (n % 2 == 0)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.White;
        }
        else
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
        }
    }

    public static string FormatOutput(int n, (float sn, double db, decimal dm, BigRational br) x)
    {
        SetConsoleFormat(n);
        return string.Format(CultureInfo.CurrentCulture, FMT_STR, n, x.sn, x.db, x.dm, (decimal)x.br);
    }

    static void Main()
    {
        WrongConvergence();

        Console.WriteLine();
        ChaoticBankSociety();

        Console.WriteLine();
        SiegfriedRump();

        SetConsoleFormat(0);
    }
}





Python: from fractions import Fraction

def muller_seq(n:int) -> float:
    seq = [Fraction(0), Fraction(2), Fraction(-4)]
    for i in range(3, n+1):
        next_value = (111 - 1130/seq[i-1]
            + 3000/(seq[i-1]*seq[i-2]))
        seq.append(next_value)
    return float(seq[n])

for n in [3, 4, 5, 6, 7, 8, 20, 30, 50, 100]:
    print("{:4d} -> {}".format(n, muller_seq(n)))
Task: Pathological floating point problems




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    sequence()
    bank()
    rump()
}

func sequence() {
    
    var v, v1 big.Rat
    v1.SetInt64(2)
    v.SetInt64(-4)
    n := 2
    c111 := big.NewRat(111, 1)
    c1130 := big.NewRat(1130, 1)
    c3000 := big.NewRat(3000, 1)
    var t2, t3 big.Rat
    r := func() (vn big.Rat) {
        vn.Add(vn.Sub(c111, t2.Quo(c1130, &v)), t3.Quo(c3000, t3.Mul(&v, &v1)))
        return
    }
    fmt.Println("  n  sequence value")
    for _, x := range []int{3, 4, 5, 6, 7, 8, 20, 30, 50, 100} {
        for ; n < x; n++ {
            v1, v = v, r()
        }
        f, _ := v.Float64()
        fmt.Printf("%3d %19.16f\n", n, f)
    }
}

func bank() {
    
    var balance struct{ e, d big.Int }
    
    balance.e.SetInt64(1)
    balance.d.SetInt64(-1)
    
    var m, one big.Int
    one.SetInt64(1)
    for y := 1; y <= 25; y++ {
        m.SetInt64(int64(y))
        balance.e.Mul(&m, &balance.e)
        balance.d.Mul(&m, &balance.d)
        balance.d.Sub(&balance.d, &one)
    }
    
    var e, ef, df, b big.Float
    e.SetPrec(100).SetString("2.71828182845904523536028747135")
    ef.SetInt(&balance.e)
    df.SetInt(&balance.d)
    b.Add(b.Mul(&e, &ef), &df)
    fmt.Printf("Bank balance after 25 years: $%.2f\n", &b)
}

func rump() {
    a, b := 77617., 33096.
    fmt.Printf("Rump f(%g, %g): %g\n", a, b, f(a, b))
}

func f(a, b float64) float64 {
    
    
    fp := func(x float64) *big.Float { return big.NewFloat(x).SetPrec(128) }
    a1 := fp(a)
    b1 := fp(b)
    a2 := new(big.Float).Mul(a1, a1)
    b2 := new(big.Float).Mul(b1, b1)
    b4 := new(big.Float).Mul(b2, b2)
    b6 := new(big.Float).Mul(b2, b4)
    b8 := new(big.Float).Mul(b4, b4)
    two := fp(2)
    t1 := fp(333.75)
    t1.Mul(t1, b6)
    t21 := fp(11)
    t21.Mul(t21.Mul(t21, a2), b2)
    t23 := fp(121)
    t23.Mul(t23, b4)
    t2 := new(big.Float).Sub(t21, b6)
    t2.Mul(a2, t2.Sub(t2.Sub(t2, t23), two))
    t3 := fp(5.5)
    t3.Mul(t3, b8)
    t4 := new(big.Float).Mul(two, b1)
    t4.Quo(a1, t4)
    s := new(big.Float).Add(t1, t2)
    f64, _ := s.Add(s.Add(s, t3), t4).Float64()
    return f64
}




Python: from fractions import Fraction

def muller_seq(n:int) -> float:
    seq = [Fraction(0), Fraction(2), Fraction(-4)]
    for i in range(3, n+1):
        next_value = (111 - 1130/seq[i-1]
            + 3000/(seq[i-1]*seq[i-2]))
        seq.append(next_value)
    return float(seq[n])

for n in [3, 4, 5, 6, 7, 8, 20, 30, 50, 100]:
    print("{:4d} -> {}".format(n, muller_seq(n)))
Task: Pathological floating point problems







Python: from fractions import Fraction

def muller_seq(n:int) -> float:
    seq = [Fraction(0), Fraction(2), Fraction(-4)]
    for i in range(3, n+1):
        next_value = (111 - 1130/seq[i-1]
            + 3000/(seq[i-1]*seq[i-2]))
        seq.append(next_value)
    return float(seq[n])

for n in [3, 4, 5, 6, 7, 8, 20, 30, 50, 100]:
    print("{:4d} -> {}".format(n, muller_seq(n)))


VB: Imports System.Globalization
Imports System.Numerics
Imports Numerics

#Const USE_BIGRATIONAL = True
#Const BANDED_ROWS = True
#Const INCREASED_LIMITS = True

#If Not USE_BIGRATIONAL Then

Structure BigRational
    Overrides Function ToString() As String
        Return "NOT USING BIGRATIONAL"
    End Function
    Shared Narrowing Operator CType(value As BigRational) As Decimal
        Return -1
    End Operator
End Structure
#End If

Module Common
    Public Const FMT_STR = "{0,4}   {1,-15:G9}   {2,-24:G17}   {3,-32}   {4,-32}"
    Public ReadOnly Property Headings As String =
        String.Format(CultureInfo.InvariantCulture,
                      FMT_STR,
                      {"N", "Single", "Double", "Decimal", "BigRational (rounded as decimal)"})

    <Conditional("BANDED_ROWS")>
    Sub SetConsoleFormat(n As Integer)
        If n Mod 2 = 0 Then
            Console.BackgroundColor = ConsoleColor.Black
            Console.ForegroundColor = ConsoleColor.White
        Else
            Console.BackgroundColor = ConsoleColor.White
            Console.ForegroundColor = ConsoleColor.Black
        End If
    End Sub

    Function FormatOutput(n As Integer, x As (sn As Single, db As Double, dm As Decimal, br As BigRational)) As String
        SetConsoleFormat(n)
        Return String.Format(CultureInfo.CurrentCulture, FMT_STR, n, x.sn, x.db, x.dm, CDec(x.br))
    End Function

    Sub Main()
        WrongConvergence()
        Console.WriteLine()
        ChaoticBankSociety()

        Console.WriteLine()
        SiegfriedRump()

        SetConsoleFormat(0)
    End Sub
End Module
Task: Cumulative standard deviation

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

typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;

typedef struct stat_obj_struct {
   double sum, sum2;
   size_t num;
   Action action; 
} sStatObject, *StatObject;

StatObject NewStatObject( Action action )
{
  StatObject so;

  so = malloc(sizeof(sStatObject));
  so->sum = 0.0;
  so->sum2 = 0.0;
  so->num = 0;
  so->action = action;
  return so;
}
#define FREE_STAT_OBJECT(so) \
   free(so); so = NULL
double stat_obj_value(StatObject so, Action action)
{
  double num, mean, var, stddev;
    
  if (so->num == 0.0) return 0.0;
  num = so->num;
  if (action==COUNT) return num;
  mean = so->sum/num;
  if (action==MEAN) return mean;
  var = so->sum2/num - mean*mean;
  if (action==VAR) return var;
  stddev = sqrt(var);
  if (action==STDDEV) return stddev;
  return 0;
}

double stat_object_add(StatObject so, double v)
{
  so->num++;
  so->sum += v;
  so->sum2 += v*v;
  return stat_obj_value(so, so->action);
}



C#: using System;
using System.Collections.Generic;
using System.Linq;

namespace standardDeviation
{
    class Program
    {
        static void Main(string[] args)
        {
            List<double> nums = new List<double> { 2, 4, 4, 4, 5, 5, 7, 9 };
            for (int i = 1; i <= nums.Count; i++)            
                Console.WriteLine(sdev(nums.GetRange(0, i)));
        }

        static double sdev(List<double> nums)
        {
            List<double> store = new List<double>();
            foreach (double n in nums)
                store.Add((n - nums.Average()) * (n - nums.Average()));           

            return Math.Sqrt(store.Sum() / store.Count);
        }
    }
}
Task: Cumulative standard deviation

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

typedef enum Action { STDDEV, MEAN, VAR, COUNT } Action;

typedef struct stat_obj_struct {
   double sum, sum2;
   size_t num;
   Action action; 
} sStatObject, *StatObject;

StatObject NewStatObject( Action action )
{
  StatObject so;

  so = malloc(sizeof(sStatObject));
  so->sum = 0.0;
  so->sum2 = 0.0;
  so->num = 0;
  so->action = action;
  return so;
}
#define FREE_STAT_OBJECT(so) \
   free(so); so = NULL
double stat_obj_value(StatObject so, Action action)
{
  double num, mean, var, stddev;
    
  if (so->num == 0.0) return 0.0;
  num = so->num;
  if (action==COUNT) return num;
  mean = so->sum/num;
  if (action==MEAN) return mean;
  var = so->sum2/num - mean*mean;
  if (action==VAR) return var;
  stddev = sqrt(var);
  if (action==STDDEV) return stddev;
  return 0;
}

double stat_object_add(StatObject so, double v)
{
  so->num++;
  so->sum += v;
  so->sum2 += v*v;
  return stat_obj_value(so, so->action);
}


C++: #include <assert.h>
#include <cmath>
#include <vector>
#include <iostream>

template<int N> struct MomentsAccumulator_
{
	std::vector<double> m_;
	MomentsAccumulator_() : m_(N + 1, 0.0) {}
	void operator()(double v)
	{
		double inc = 1.0;
		for (auto& mi : m_)
		{
			mi += inc;
			inc *= v;
		}
	}
};

double Stdev(const std::vector<double>& moments)
{
	assert(moments.size() > 2);
	assert(moments[0] > 0.0);
	const double mean = moments[1] / moments[0];
	const double meanSquare = moments[2] / moments[0];
	return sqrt(meanSquare - mean * mean);
}

int main(void)
{
	std::vector<int> data({ 2, 4, 4, 4, 5, 5, 7, 9 });
	MomentsAccumulator_<2> accum;
	for (auto d : data)
	{
		accum(d);
		std::cout << "Running stdev:  " << Stdev(accum.m_) << "\n";
	}
}