WeixiangYan/CodeTransOcean huggingface dataset:
Task: Sorting algorithms_Bogosort






PHP: function bogosort($l) {
    while (!in_order($l))
        shuffle($l);
    return $l;
}

function in_order($l) {
    for ($i = 1; $i < count($l); $i++)
        if ($l[$i] < $l[$i-1])
            return FALSE;
    return TRUE;
}




COBOL: identification division.
program-id. bogo-sort-program.
data division.
working-storage section.
01  array-to-sort.
    05 item-table.
        10 item          pic 999
            occurs 10 times.
01  randomization.
    05 random-seed       pic 9(8).
    05 random-index      pic 9.
01  flags-counters-etc.
    05 array-index       pic 99.
    05 adjusted-index    pic 99.
    05 temporary-storage pic 999.
    05 shuffles          pic 9(8)
        value zero.
    05 sorted            pic 9.
01  numbers-without-leading-zeros.
    05 item-no-zeros     pic z(4).
    05 shuffles-no-zeros pic z(8).
procedure division.
control-paragraph.
    accept random-seed from time.
    move function random(random-seed) to item(1).
    perform random-item-paragraph varying array-index from 2 by 1
    until array-index is greater than 10.
    display 'BEFORE SORT:' with no advancing.
    perform show-array-paragraph varying array-index from 1 by 1
    until array-index is greater than 10.
    display ''.
    perform shuffle-paragraph through is-it-sorted-paragraph
    until sorted is equal to 1.
    display 'AFTER SORT: ' with no advancing.
    perform show-array-paragraph varying array-index from 1 by 1
    until array-index is greater than 10.
    display ''.
    move shuffles to shuffles-no-zeros.
    display shuffles-no-zeros ' SHUFFLES PERFORMED.'
    stop run.
random-item-paragraph.
    move function random to item(array-index).
show-array-paragraph.
    move item(array-index) to item-no-zeros.
    display item-no-zeros with no advancing.
shuffle-paragraph.
    perform shuffle-items-paragraph,
    varying array-index from 1 by 1
    until array-index is greater than 10.
    add 1 to shuffles.
is-it-sorted-paragraph.
    move 1 to sorted.
    perform item-in-order-paragraph varying array-index from 1 by 1,
    until sorted is equal to zero
    or array-index is equal to 10.
shuffle-items-paragraph.
    move function random to random-index.
    add 1 to random-index giving adjusted-index.
    move item(array-index) to temporary-storage.
    move item(adjusted-index) to item(array-index).
    move temporary-storage to item(adjusted-index).
item-in-order-paragraph.
    add 1 to array-index giving adjusted-index.
    if item(array-index) is greater than item(adjusted-index)
    then move zero to sorted.
Task: Cumulative standard deviation



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);
        }
    }
}









Ada: with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;                        use Ada.Text_IO;
with Ada.Float_Text_IO;                  use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;                use Ada.Integer_Text_IO;

procedure Test_Deviation is
   type Sample is record
      N            : Natural := 0;
      Sum          : Float := 0.0;
      SumOfSquares : Float := 0.0;
   end record;
   procedure Add (Data : in out Sample; Point : Float) is
   begin
      Data.N       := Data.N + 1;
      Data.Sum    := Data.Sum    + Point;
      Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
   end Add;
   function Deviation (Data : Sample) return Float is
   begin
      return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
   end Deviation;

   Data : Sample;
   Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
   for Index in Test'Range loop
      Add (Data, Float(Test(Index)));
      Put("N="); Put(Item => Index, Width => 1);
      Put(" ITEM="); Put(Item => Test(Index), Width => 1);
      Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
      Put("  STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
      New_line;
   end loop;
end Test_Deviation;
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);
}











Ada: with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;                        use Ada.Text_IO;
with Ada.Float_Text_IO;                  use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;                use Ada.Integer_Text_IO;

procedure Test_Deviation is
   type Sample is record
      N            : Natural := 0;
      Sum          : Float := 0.0;
      SumOfSquares : Float := 0.0;
   end record;
   procedure Add (Data : in out Sample; Point : Float) is
   begin
      Data.N       := Data.N + 1;
      Data.Sum    := Data.Sum    + Point;
      Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
   end Add;
   function Deviation (Data : Sample) return Float is
   begin
      return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
   end Deviation;

   Data : Sample;
   Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
   for Index in Test'Range loop
      Add (Data, Float(Test(Index)));
      Put("N="); Put(Item => Index, Width => 1);
      Put(" ITEM="); Put(Item => Test(Index), Width => 1);
      Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
      Put("  STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
      New_line;
   end loop;
end Test_Deviation;
Task: Cumulative standard deviation


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";
	}
}










Ada: with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;                        use Ada.Text_IO;
with Ada.Float_Text_IO;                  use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;                use Ada.Integer_Text_IO;

procedure Test_Deviation is
   type Sample is record
      N            : Natural := 0;
      Sum          : Float := 0.0;
      SumOfSquares : Float := 0.0;
   end record;
   procedure Add (Data : in out Sample; Point : Float) is
   begin
      Data.N       := Data.N + 1;
      Data.Sum    := Data.Sum    + Point;
      Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
   end Add;
   function Deviation (Data : Sample) return Float is
   begin
      return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
   end Deviation;

   Data : Sample;
   Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
   for Index in Test'Range loop
      Add (Data, Float(Test(Index)));
      Put("N="); Put(Item => Index, Width => 1);
      Put(" ITEM="); Put(Item => Test(Index), Width => 1);
      Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
      Put("  STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
      New_line;
   end loop;
end Test_Deviation;
Task: Cumulative standard deviation




Go: package main

import (
    "fmt"
    "math"
)

func newRsdv() func(float64) float64 {
    var n, a, q  float64
    return func(x float64) float64 {
        n++
        a1 := a+(x-a)/n
        q, a = q+(x-a)*(x-a1), a1
        return math.Sqrt(q/n)
    }
}

func main() {
    r := newRsdv()
    for _, x := range []float64{2,4,4,4,5,5,7,9} {
        fmt.Println(r(x))
    }
}








Ada: with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;                        use Ada.Text_IO;
with Ada.Float_Text_IO;                  use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;                use Ada.Integer_Text_IO;

procedure Test_Deviation is
   type Sample is record
      N            : Natural := 0;
      Sum          : Float := 0.0;
      SumOfSquares : Float := 0.0;
   end record;
   procedure Add (Data : in out Sample; Point : Float) is
   begin
      Data.N       := Data.N + 1;
      Data.Sum    := Data.Sum    + Point;
      Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
   end Add;
   function Deviation (Data : Sample) return Float is
   begin
      return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
   end Deviation;

   Data : Sample;
   Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
   for Index in Test'Range loop
      Add (Data, Float(Test(Index)));
      Put("N="); Put(Item => Index, Width => 1);
      Put(" ITEM="); Put(Item => Test(Index), Width => 1);
      Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
      Put("  STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
      New_line;
   end loop;
end Test_Deviation;
Task: Cumulative standard deviation





Java: public class StdDev {
    int n = 0;
    double sum = 0;
    double sum2 = 0;

    public double sd(double x) {
	n++;
	sum += x;
	sum2 += x*x;

	return Math.sqrt(sum2/n - sum*sum/n/n);
    }

    public static void main(String[] args) {
        double[] testData = {2,4,4,4,5,5,7,9};
        StdDev sd = new StdDev();

        for (double x : testData) {
            System.out.println(sd.sd(x));
        }
    }
}







Ada: with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;                        use Ada.Text_IO;
with Ada.Float_Text_IO;                  use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;                use Ada.Integer_Text_IO;

procedure Test_Deviation is
   type Sample is record
      N            : Natural := 0;
      Sum          : Float := 0.0;
      SumOfSquares : Float := 0.0;
   end record;
   procedure Add (Data : in out Sample; Point : Float) is
   begin
      Data.N       := Data.N + 1;
      Data.Sum    := Data.Sum    + Point;
      Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
   end Add;
   function Deviation (Data : Sample) return Float is
   begin
      return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
   end Deviation;

   Data : Sample;
   Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
   for Index in Test'Range loop
      Add (Data, Float(Test(Index)));
      Put("N="); Put(Item => Index, Width => 1);
      Put(" ITEM="); Put(Item => Test(Index), Width => 1);
      Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
      Put("  STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
      New_line;
   end loop;
end Test_Deviation;
Task: Cumulative standard deviation







Python: >>> from math import sqrt
>>> def sd(x):
    sd.sum  += x
    sd.sum2 += x*x
    sd.n    += 1.0
    sum, sum2, n = sd.sum, sd.sum2, sd.n
    return sqrt(sum2/n - sum*sum/n/n)

>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
    print (value, sd(value))

    
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258)
(4, 0.8660254037844386)
(5, 0.97979589711327075)
(5, 1.0)
(7, 1.3997084244475311)
(9, 2.0)
>>>





Ada: with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;                        use Ada.Text_IO;
with Ada.Float_Text_IO;                  use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;                use Ada.Integer_Text_IO;

procedure Test_Deviation is
   type Sample is record
      N            : Natural := 0;
      Sum          : Float := 0.0;
      SumOfSquares : Float := 0.0;
   end record;
   procedure Add (Data : in out Sample; Point : Float) is
   begin
      Data.N       := Data.N + 1;
      Data.Sum    := Data.Sum    + Point;
      Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
   end Add;
   function Deviation (Data : Sample) return Float is
   begin
      return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
   end Deviation;

   Data : Sample;
   Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
   for Index in Test'Range loop
      Add (Data, Float(Test(Index)));
      Put("N="); Put(Item => Index, Width => 1);
      Put(" ITEM="); Put(Item => Test(Index), Width => 1);
      Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
      Put("  STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
      New_line;
   end loop;
end Test_Deviation;
Task: Cumulative standard deviation








VB: data = Array(2,4,4,4,5,5,7,9)

For i = 0 To UBound(data)
	WScript.StdOut.Write "value = " & data(i) &_
		" running sd = " & sd(data,i)
	WScript.StdOut.WriteLine
Next

Function sd(arr,n)
	mean = 0
	variance = 0
	For j = 0 To n
		mean = mean + arr(j)
	Next
	mean = mean/(n+1)
	For k = 0 To n
		variance = variance + ((arr(k)-mean)^2)
	Next
	variance = variance/(n+1)
	sd = FormatNumber(Sqr(variance),6)
End Function




Ada: with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO;                        use Ada.Text_IO;
with Ada.Float_Text_IO;                  use Ada.Float_Text_IO;
with Ada.Integer_Text_IO;                use Ada.Integer_Text_IO;

procedure Test_Deviation is
   type Sample is record
      N            : Natural := 0;
      Sum          : Float := 0.0;
      SumOfSquares : Float := 0.0;
   end record;
   procedure Add (Data : in out Sample; Point : Float) is
   begin
      Data.N       := Data.N + 1;
      Data.Sum    := Data.Sum    + Point;
      Data.SumOfSquares := Data.SumOfSquares + Point ** 2;
   end Add;
   function Deviation (Data : Sample) return Float is
   begin
      return Sqrt (Data.SumOfSquares / Float (Data.N) - (Data.Sum / Float (Data.N)) ** 2);
   end Deviation;

   Data : Sample;
   Test : array (1..8) of Integer := (2, 4, 4, 4, 5, 5, 7, 9);
begin
   for Index in Test'Range loop
      Add (Data, Float(Test(Index)));
      Put("N="); Put(Item => Index, Width => 1);
      Put(" ITEM="); Put(Item => Test(Index), Width => 1);
      Put(" AVG="); Put(Item => Float(Data.Sum)/Float(Index), Fore => 1, Aft => 3, Exp => 0);
      Put("  STDDEV="); Put(Item => Deviation (Data), Fore => 1, Aft => 3, Exp => 0);
      New_line;
   end loop;
end Test_Deviation;
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);
}
Task: Cumulative standard deviation



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 <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";
	}
}
Task: Cumulative standard deviation





Java: public class StdDev {
    int n = 0;
    double sum = 0;
    double sum2 = 0;

    public double sd(double x) {
	n++;
	sum += x;
	sum2 += x*x;

	return Math.sqrt(sum2/n - sum*sum/n/n);
    }

    public static void main(String[] args) {
        double[] testData = {2,4,4,4,5,5,7,9};
        StdDev sd = new StdDev();

        for (double x : testData) {
            System.out.println(sd.sd(x));
        }
    }
}
Task: Cumulative standard deviation







Python: >>> from math import sqrt
>>> def sd(x):
    sd.sum  += x
    sd.sum2 += x*x
    sd.n    += 1.0
    sum, sum2, n = sd.sum, sd.sum2, sd.n
    return sqrt(sum2/n - sum*sum/n/n)

>>> sd.sum = sd.sum2 = sd.n = 0
>>> for value in (2,4,4,4,5,5,7,9):
    print (value, sd(value))

    
(2, 0.0)
(4, 1.0)
(4, 0.94280904158206258)
(4, 0.8660254037844386)
(5, 0.97979589711327075)
(5, 1.0)
(7, 1.3997084244475311)
(9, 2.0)
>>>
Task: Cumulative standard deviation








VB: data = Array(2,4,4,4,5,5,7,9)

For i = 0 To UBound(data)
	WScript.StdOut.Write "value = " & data(i) &_
		" running sd = " & sd(data,i)
	WScript.StdOut.WriteLine
Next

Function sd(arr,n)
	mean = 0
	variance = 0
	For j = 0 To n
		mean = mean + arr(j)
	Next
	mean = mean/(n+1)
	For k = 0 To n
		variance = variance + ((arr(k)-mean)^2)
	Next
	variance = variance/(n+1)
	sd = FormatNumber(Sqr(variance),6)
End Function
Task: Cumulative standard deviation




Go: package main

import (
    "fmt"
    "math"
)

func newRsdv() func(float64) float64 {
    var n, a, q  float64
    return func(x float64) float64 {
        n++
        a1 := a+(x-a)/n
        q, a = q+(x-a)*(x-a1), a1
        return math.Sqrt(q/n)
    }
}

func main() {
    r := newRsdv()
    for _, x := range []float64{2,4,4,4,5,5,7,9} {
        fmt.Println(r(x))
    }
}
Task: Cumulative standard deviation



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);
        }
    }
}








Fortran: program standard_deviation
  implicit none
  integer(kind=4), parameter :: dp = kind(0.0d0)

  real(kind=dp), dimension(:), allocatable :: vals
  integer(kind=4) :: i

  real(kind=dp), dimension(8) :: sample_data = (/ 2, 4, 4, 4, 5, 5, 7, 9 /)

  do i = lbound(sample_data, 1), ubound(sample_data, 1)
    call sample_add(vals, sample_data(i))
    write(*, fmt='(''#'',I1,1X,''value = '',F3.1,1X,''stddev ='',1X,F10.8)') &
      i, sample_data(i), stddev(vals)
  end do

  if (allocated(vals)) deallocate(vals)
contains
  
  subroutine sample_add(population, val)
    real(kind=dp), dimension(:), allocatable, intent (inout) :: population
    real(kind=dp), intent (in) :: val

    real(kind=dp), dimension(:), allocatable :: tmp
    integer(kind=4) :: n

    if (.not. allocated(population)) then
      allocate(population(1))
      population(1) = val
    else
      n = size(population)
      call move_alloc(population, tmp)

      allocate(population(n + 1))
      population(1:n) = tmp
      population(n + 1) = val
    endif
  end subroutine sample_add

  
  real(kind=dp) function stddev(vals)
    real(kind=dp), dimension(:), intent(in) :: vals
    real(kind=dp) :: mean
    integer(kind=4) :: n

    n = size(vals)
    mean = sum(vals)/n
    stddev = sqrt(sum((vals - mean)**2)/n)
  end function stddev
end program standard_deviation