WeixiangYan/CodeTransOcean huggingface dataset:
Task: Count the coins


C++: #include <iostream>
#include <stack>
#include <vector>

struct DataFrame {
  int sum;
  std::vector<int> coins;
  std::vector<int> avail_coins;
};

int main() {
  std::stack<DataFrame> s;
  s.push({ 100, {}, { 25, 10, 5, 1 } });
  int ways = 0;
  while (!s.empty()) {
    DataFrame top = s.top();
    s.pop();
    if (top.sum < 0) continue;
    if (top.sum == 0) {
      ++ways;
      continue;
    }
    if (top.avail_coins.empty()) continue;
    DataFrame d = top;
    d.sum -= top.avail_coins[0];
    d.coins.push_back(top.avail_coins[0]);
    s.push(d);
    d = top;
    d.avail_coins.erase(std::begin(d.avail_coins));
    s.push(d);
  }
  std::cout << ways << std::endl;
  return 0;
}
Task: Count the coins





Java: import java.util.Arrays;
import java.math.BigInteger;

class CountTheCoins {
    private static BigInteger countChanges(int amount, int[] coins){
        final int n = coins.length;
        int cycle = 0;
        for (int c : coins)
            if (c <= amount && c >= cycle)
                cycle = c + 1;
        cycle *= n;
        BigInteger[] table = new BigInteger[cycle];
        Arrays.fill(table, 0, n, BigInteger.ONE);
        Arrays.fill(table, n, cycle, BigInteger.ZERO);

        int pos = n;
        for (int s = 1; s <= amount; s++) {
            for (int i = 0; i < n; i++) {
                if (i == 0 && pos >= cycle)
                    pos = 0;
                if (coins[i] <= s) {
                    final int q = pos - (coins[i] * n);
                    table[pos] = (q >= 0) ? table[q] : table[q + cycle];
                }
                if (i != 0)
                    table[pos] = table[pos].add(table[pos - 1]);
                pos++;
            }
        }

        return table[pos - 1];
    }

    public static void main(String[] args) {
        final int[][] coinsUsEu = {{100, 50, 25, 10, 5, 1},
                                   {200, 100, 50, 20, 10, 5, 2, 1}};

        for (int[] coins : coinsUsEu) {
            System.out.println(countChanges(     100,
                Arrays.copyOfRange(coins, 2, coins.length)));
            System.out.println(countChanges(  100000, coins));
            System.out.println(countChanges( 1000000, coins));
            System.out.println(countChanges(10000000, coins) + "\n");
        }
    }
}
Task: Count the coins







Python: def changes(amount, coins):
    ways = [0] * (amount + 1)
    ways[0] = 1
    for coin in coins:
        for j in xrange(coin, amount + 1):
            ways[j] += ways[j - coin]
    return ways[amount]

print changes(100, [1, 5, 10, 25])
print changes(100000, [1, 5, 10, 25, 50, 100])
Task: Count the coins








VB: Private Function coin_count(coins As Variant, amount As Long) As Variant 
    
    Dim s As Variant
    ReDim s(amount + 1)
    Dim c As Integer
    s(1) = CDec(1)
    For c = 1 To UBound(coins)
        For n = coins(c) To amount
            s(n + 1) = CDec(s(n + 1) + s(n - coins(c) + 1))
        Next n
    Next c
    coin_count = s(amount + 1)
End Function
Public Sub main2()
    Dim us_commons_coins As Variant
    
    us_common_coins = [{25, 10, 5, 1}]
    Debug.Print coin_count(us_common_coins, 100)
    Dim us_coins As Variant
    us_coins = [{100,50,25, 10, 5, 1}]
    Debug.Print coin_count(us_coins, 100000)
End Sub
Task: Count the coins




Go: package main

import "fmt"

func main() {
    amount := 100
    fmt.Println("amount, ways to make change:", amount, countChange(amount))
}

func countChange(amount int) int64 {
    return cc(amount, 4)
}

func cc(amount, kindsOfCoins int) int64 {
    switch {
    case amount == 0:
        return 1
    case amount < 0 || kindsOfCoins == 0:
        return 0
    }
    return cc(amount, kindsOfCoins-1) +
        cc(amount - firstDenomination(kindsOfCoins), kindsOfCoins)
}

func firstDenomination(kindsOfCoins int) int {
    switch kindsOfCoins {
    case 1:
        return 1
    case 2:
        return 5
    case 3:
        return 10
    case 4:
        return 25
    }
    panic(kindsOfCoins)
}
Task: Count the coins

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



typedef struct { uint64_t x[2]; } i128;


void show(i128 v) {
	uint32_t x[4] = {v.x[0], v.x[0] >> 32, v.x[1], v.x[1] >> 32};
	int i, j = 0, len = 4;
	char buf[100];
	do {
		uint64_t c = 0;
		for (i = len; i--; ) {
			c = (c << 32) + x[i];
			x[i] = c / 10, c %= 10;
		}

		buf[j++] = c + '0';
		for (len = 4; !x[len - 1]; len--);
	} while (len);

	while (j--) putchar(buf[j]);
	putchar('\n');
}

i128 count(int sum, int *coins)
{
	int n, i, k;
	for (n = 0; coins[n]; n++);

	i128 **v = malloc(sizeof(int*) * n);
	int *idx = malloc(sizeof(int) * n);

	for (i = 0; i < n; i++) {
		idx[i] = coins[i];
		
		v[i] = calloc(sizeof(i128), coins[i]);
	}

	v[0][coins[0] - 1] = (i128) {{1, 0}};

	for (k = 0; k <= sum; k++) {
		for (i = 0; i < n; i++)
			if (!idx[i]--) idx[i] = coins[i] - 1;

		i128 c = v[0][ idx[0] ];

		for (i = 1; i < n; i++) {
			i128 *p = v[i] + idx[i];

			
			p->x[0] += c.x[0];
			p->x[1] += c.x[1];
			if (p->x[0] < c.x[0]) 
				p->x[1] ++;
			c = *p;
		}
	}

	i128 r = v[n - 1][idx[n-1]];

	for (i = 0; i < n; i++) free(v[i]);
	free(v);
	free(idx);

	return r;
}


int count2(int sum, int *coins)
{
	if (!*coins || sum < 0) return 0;
	if (!sum) return 1;
	return count2(sum - *coins, coins) + count2(sum, coins + 1);
}

int main(void)
{
	int us_coins[] = { 100, 50, 25, 10, 5, 1, 0 };
	int eu_coins[] = { 200, 100, 50, 20, 10, 5, 2, 1, 0 };

	show(count(   100, us_coins + 2));
	show(count(  1000, us_coins));

	show(count(  1000 * 100, us_coins));
	show(count( 10000 * 100, us_coins));
	show(count(100000 * 100, us_coins));

	putchar('\n');

	show(count(     1 * 100, eu_coins));
	show(count(  1000 * 100, eu_coins));
	show(count( 10000 * 100, eu_coins));
	show(count(100000 * 100, eu_coins));

	return 0;
}









COBOL:        identification division.
       program-id. CountCoins.

       data division.
       working-storage section.
       77  i                      pic 9(3).
       77  j                      pic 9(3).  
       77  m                      pic 9(3) value 4.
       77  n                      pic 9(3) value 100.  
       77  edited-value           pic z(18).
       01  coins-table            value "01051025".
           05 coin                pic 9(2) occurs 4.
       01  ways-table.
           05 way                 pic 9(18) occurs 100.

       procedure division.
       main.
           perform calc-count
           move way(n) to edited-value
           display function trim(edited-value)
           stop run
           .
       calc-count.
           initialize ways-table
           move 1 to way(1)
           perform varying i from 1 by 1 until i > m
              perform varying j from coin(i) by 1 until j > n
                 add way(j - coin(i)) to way(j)
              end-perform
           end-perform
           .
Task: Count the coins



C#:     
    class Program
    {
        static long Count(int[] C, int m, int n)
        {
            var table = new long[n + 1];
            table[0] = 1;
            for (int i = 0; i < m; i++)
                for (int j = C[i]; j <= n; j++)
                    table[j] += table[j - C[i]];
            return table[n];
        }
        static void Main(string[] args)
        {
            var C = new int[] { 1, 5, 10, 25 };
            int m = C.Length;
            int n = 100;
            Console.WriteLine(Count(C, m, n));  
            Console.ReadLine();
        }
    }







COBOL:        identification division.
       program-id. CountCoins.

       data division.
       working-storage section.
       77  i                      pic 9(3).
       77  j                      pic 9(3).  
       77  m                      pic 9(3) value 4.
       77  n                      pic 9(3) value 100.  
       77  edited-value           pic z(18).
       01  coins-table            value "01051025".
           05 coin                pic 9(2) occurs 4.
       01  ways-table.
           05 way                 pic 9(18) occurs 100.

       procedure division.
       main.
           perform calc-count
           move way(n) to edited-value
           display function trim(edited-value)
           stop run
           .
       calc-count.
           initialize ways-table
           move 1 to way(1)
           perform varying i from 1 by 1 until i > m
              perform varying j from coin(i) by 1 until j > n
                 add way(j - coin(i)) to way(j)
              end-perform
           end-perform
           .
Task: Count the coins


C++: #include <iostream>
#include <stack>
#include <vector>

struct DataFrame {
  int sum;
  std::vector<int> coins;
  std::vector<int> avail_coins;
};

int main() {
  std::stack<DataFrame> s;
  s.push({ 100, {}, { 25, 10, 5, 1 } });
  int ways = 0;
  while (!s.empty()) {
    DataFrame top = s.top();
    s.pop();
    if (top.sum < 0) continue;
    if (top.sum == 0) {
      ++ways;
      continue;
    }
    if (top.avail_coins.empty()) continue;
    DataFrame d = top;
    d.sum -= top.avail_coins[0];
    d.coins.push_back(top.avail_coins[0]);
    s.push(d);
    d = top;
    d.avail_coins.erase(std::begin(d.avail_coins));
    s.push(d);
  }
  std::cout << ways << std::endl;
  return 0;
}








COBOL:        identification division.
       program-id. CountCoins.

       data division.
       working-storage section.
       77  i                      pic 9(3).
       77  j                      pic 9(3).  
       77  m                      pic 9(3) value 4.
       77  n                      pic 9(3) value 100.  
       77  edited-value           pic z(18).
       01  coins-table            value "01051025".
           05 coin                pic 9(2) occurs 4.
       01  ways-table.
           05 way                 pic 9(18) occurs 100.

       procedure division.
       main.
           perform calc-count
           move way(n) to edited-value
           display function trim(edited-value)
           stop run
           .
       calc-count.
           initialize ways-table
           move 1 to way(1)
           perform varying i from 1 by 1 until i > m
              perform varying j from coin(i) by 1 until j > n
                 add way(j - coin(i)) to way(j)
              end-perform
           end-perform
           .
Task: Count the coins





Java: import java.util.Arrays;
import java.math.BigInteger;

class CountTheCoins {
    private static BigInteger countChanges(int amount, int[] coins){
        final int n = coins.length;
        int cycle = 0;
        for (int c : coins)
            if (c <= amount && c >= cycle)
                cycle = c + 1;
        cycle *= n;
        BigInteger[] table = new BigInteger[cycle];
        Arrays.fill(table, 0, n, BigInteger.ONE);
        Arrays.fill(table, n, cycle, BigInteger.ZERO);

        int pos = n;
        for (int s = 1; s <= amount; s++) {
            for (int i = 0; i < n; i++) {
                if (i == 0 && pos >= cycle)
                    pos = 0;
                if (coins[i] <= s) {
                    final int q = pos - (coins[i] * n);
                    table[pos] = (q >= 0) ? table[q] : table[q + cycle];
                }
                if (i != 0)
                    table[pos] = table[pos].add(table[pos - 1]);
                pos++;
            }
        }

        return table[pos - 1];
    }

    public static void main(String[] args) {
        final int[][] coinsUsEu = {{100, 50, 25, 10, 5, 1},
                                   {200, 100, 50, 20, 10, 5, 2, 1}};

        for (int[] coins : coinsUsEu) {
            System.out.println(countChanges(     100,
                Arrays.copyOfRange(coins, 2, coins.length)));
            System.out.println(countChanges(  100000, coins));
            System.out.println(countChanges( 1000000, coins));
            System.out.println(countChanges(10000000, coins) + "\n");
        }
    }
}





COBOL:        identification division.
       program-id. CountCoins.

       data division.
       working-storage section.
       77  i                      pic 9(3).
       77  j                      pic 9(3).  
       77  m                      pic 9(3) value 4.
       77  n                      pic 9(3) value 100.  
       77  edited-value           pic z(18).
       01  coins-table            value "01051025".
           05 coin                pic 9(2) occurs 4.
       01  ways-table.
           05 way                 pic 9(18) occurs 100.

       procedure division.
       main.
           perform calc-count
           move way(n) to edited-value
           display function trim(edited-value)
           stop run
           .
       calc-count.
           initialize ways-table
           move 1 to way(1)
           perform varying i from 1 by 1 until i > m
              perform varying j from coin(i) by 1 until j > n
                 add way(j - coin(i)) to way(j)
              end-perform
           end-perform
           .
Task: Count the coins







Python: def changes(amount, coins):
    ways = [0] * (amount + 1)
    ways[0] = 1
    for coin in coins:
        for j in xrange(coin, amount + 1):
            ways[j] += ways[j - coin]
    return ways[amount]

print changes(100, [1, 5, 10, 25])
print changes(100000, [1, 5, 10, 25, 50, 100])



COBOL:        identification division.
       program-id. CountCoins.

       data division.
       working-storage section.
       77  i                      pic 9(3).
       77  j                      pic 9(3).  
       77  m                      pic 9(3) value 4.
       77  n                      pic 9(3) value 100.  
       77  edited-value           pic z(18).
       01  coins-table            value "01051025".
           05 coin                pic 9(2) occurs 4.
       01  ways-table.
           05 way                 pic 9(18) occurs 100.

       procedure division.
       main.
           perform calc-count
           move way(n) to edited-value
           display function trim(edited-value)
           stop run
           .
       calc-count.
           initialize ways-table
           move 1 to way(1)
           perform varying i from 1 by 1 until i > m
              perform varying j from coin(i) by 1 until j > n
                 add way(j - coin(i)) to way(j)
              end-perform
           end-perform
           .
Task: Count the coins








VB: Private Function coin_count(coins As Variant, amount As Long) As Variant 
    
    Dim s As Variant
    ReDim s(amount + 1)
    Dim c As Integer
    s(1) = CDec(1)
    For c = 1 To UBound(coins)
        For n = coins(c) To amount
            s(n + 1) = CDec(s(n + 1) + s(n - coins(c) + 1))
        Next n
    Next c
    coin_count = s(amount + 1)
End Function
Public Sub main2()
    Dim us_commons_coins As Variant
    
    us_common_coins = [{25, 10, 5, 1}]
    Debug.Print coin_count(us_common_coins, 100)
    Dim us_coins As Variant
    us_coins = [{100,50,25, 10, 5, 1}]
    Debug.Print coin_count(us_coins, 100000)
End Sub


COBOL:        identification division.
       program-id. CountCoins.

       data division.
       working-storage section.
       77  i                      pic 9(3).
       77  j                      pic 9(3).  
       77  m                      pic 9(3) value 4.
       77  n                      pic 9(3) value 100.  
       77  edited-value           pic z(18).
       01  coins-table            value "01051025".
           05 coin                pic 9(2) occurs 4.
       01  ways-table.
           05 way                 pic 9(18) occurs 100.

       procedure division.
       main.
           perform calc-count
           move way(n) to edited-value
           display function trim(edited-value)
           stop run
           .
       calc-count.
           initialize ways-table
           move 1 to way(1)
           perform varying i from 1 by 1 until i > m
              perform varying j from coin(i) by 1 until j > n
                 add way(j - coin(i)) to way(j)
              end-perform
           end-perform
           .
Task: Count the coins




Go: package main

import "fmt"

func main() {
    amount := 100
    fmt.Println("amount, ways to make change:", amount, countChange(amount))
}

func countChange(amount int) int64 {
    return cc(amount, 4)
}

func cc(amount, kindsOfCoins int) int64 {
    switch {
    case amount == 0:
        return 1
    case amount < 0 || kindsOfCoins == 0:
        return 0
    }
    return cc(amount, kindsOfCoins-1) +
        cc(amount - firstDenomination(kindsOfCoins), kindsOfCoins)
}

func firstDenomination(kindsOfCoins int) int {
    switch kindsOfCoins {
    case 1:
        return 1
    case 2:
        return 5
    case 3:
        return 10
    case 4:
        return 25
    }
    panic(kindsOfCoins)
}






COBOL:        identification division.
       program-id. CountCoins.

       data division.
       working-storage section.
       77  i                      pic 9(3).
       77  j                      pic 9(3).  
       77  m                      pic 9(3) value 4.
       77  n                      pic 9(3) value 100.  
       77  edited-value           pic z(18).
       01  coins-table            value "01051025".
           05 coin                pic 9(2) occurs 4.
       01  ways-table.
           05 way                 pic 9(18) occurs 100.

       procedure division.
       main.
           perform calc-count
           move way(n) to edited-value
           display function trim(edited-value)
           stop run
           .
       calc-count.
           initialize ways-table
           move 1 to way(1)
           perform varying i from 1 by 1 until i > m
              perform varying j from coin(i) by 1 until j > n
                 add way(j - coin(i)) to way(j)
              end-perform
           end-perform
           .
Task: First-class functions_Use numbers analogously



C#: using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        double x, xi, y, yi, z, zi;
        x = 2.0;
        xi = 0.5;
        y = 4.0;
        yi = 0.25;
        z = x + y;
        zi = 1.0 / (x + y);

        var numlist = new[] { x, y, z };
        var numlisti = new[] { xi, yi, zi };
        var multiplied = numlist.Zip(numlisti, (n1, n2) =>
                       {
                           Func<double, double> multiplier = m => n1 * n2 * m;
                           return multiplier;
                       });

        foreach (var multiplier in multiplied)
            Console.WriteLine(multiplier(0.5));
    }
}









Ada: with Ada.Text_IO;
procedure Firstclass is
   generic
      n1, n2 : Float;
   function Multiplier (m : Float) return Float;
   function Multiplier (m : Float) return Float is
   begin
      return n1 * n2 * m;
   end Multiplier;
   
   num, inv : array (1 .. 3) of Float;
begin
   num := (2.0, 4.0, 6.0);
   inv := (1.0/2.0, 1.0/4.0, 1.0/6.0);
   for i in num'Range loop
      declare
         function new_function is new Multiplier (num (i), inv (i));
      begin
         Ada.Text_IO.Put_Line (Float'Image (new_function (0.5)));
      end;
   end loop;
end Firstclass;
Task: First-class functions_Use numbers analogously


C++: #include <array>
#include <iostream>

int main()
{
  double x  = 2.0;
  double xi = 0.5;
  double y  = 4.0;
  double yi = 0.25;
  double z  = x + y;
  double zi = 1.0 / ( x + y );

  const std::array values{x, y, z};
  const std::array inverses{xi, yi, zi};

  auto multiplier = [](double a, double b)
  {
    return [=](double m){return a * b * m;};
  };

  for(size_t i = 0; i < values.size(); ++i)
  {
    auto new_function = multiplier(values[i], inverses[i]);
    double value = new_function(i + 1.0);
    std::cout << value << "\n"; 
  }
}










Ada: with Ada.Text_IO;
procedure Firstclass is
   generic
      n1, n2 : Float;
   function Multiplier (m : Float) return Float;
   function Multiplier (m : Float) return Float is
   begin
      return n1 * n2 * m;
   end Multiplier;
   
   num, inv : array (1 .. 3) of Float;
begin
   num := (2.0, 4.0, 6.0);
   inv := (1.0/2.0, 1.0/4.0, 1.0/6.0);
   for i in num'Range loop
      declare
         function new_function is new Multiplier (num (i), inv (i));
      begin
         Ada.Text_IO.Put_Line (Float'Image (new_function (0.5)));
      end;
   end loop;
end Firstclass;
Task: First-class functions_Use numbers analogously




Go: package main

import "fmt"

func main() {
    x := 2.
    xi := .5
    y := 4.
    yi := .25
    z := x + y
    zi := 1 / (x + y)
    

    numbers := []float64{x, y, z}
    inverses := []float64{xi, yi, zi}
    

    mfs := make([]func(float64) float64, len(numbers))
    for i := range mfs {
        mfs[i] = multiplier(numbers[i], inverses[i])
    }
    

    for _, mf := range mfs {
        fmt.Println(mf(1))
    }
}

func multiplier(n1, n2 float64) func(float64) float64 {
    
    n1n2 := n1 * n2
    
    return func(m float64) float64 {
        return n1n2 * m
    }
}








Ada: with Ada.Text_IO;
procedure Firstclass is
   generic
      n1, n2 : Float;
   function Multiplier (m : Float) return Float;
   function Multiplier (m : Float) return Float is
   begin
      return n1 * n2 * m;
   end Multiplier;
   
   num, inv : array (1 .. 3) of Float;
begin
   num := (2.0, 4.0, 6.0);
   inv := (1.0/2.0, 1.0/4.0, 1.0/6.0);
   for i in num'Range loop
      declare
         function new_function is new Multiplier (num (i), inv (i));
      begin
         Ada.Text_IO.Put_Line (Float'Image (new_function (0.5)));
      end;
   end loop;
end Firstclass;
Task: First-class functions_Use numbers analogously







Python: IDLE 2.6.1      
>>> 
>>> x,xi, y,yi = 2.0,0.5, 4.0,0.25
>>> 
>>> z  = x + y
>>> zi = 1.0 / (x + y)
>>> 
>>> multiplier = lambda n1, n2: (lambda m: n1 * n2 * m)
>>> 
>>> numlist = [x, y, z]
>>> numlisti = [xi, yi, zi]
>>> 
>>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)]
[0.5, 0.5, 0.5]
>>>





Ada: with Ada.Text_IO;
procedure Firstclass is
   generic
      n1, n2 : Float;
   function Multiplier (m : Float) return Float;
   function Multiplier (m : Float) return Float is
   begin
      return n1 * n2 * m;
   end Multiplier;
   
   num, inv : array (1 .. 3) of Float;
begin
   num := (2.0, 4.0, 6.0);
   inv := (1.0/2.0, 1.0/4.0, 1.0/6.0);
   for i in num'Range loop
      declare
         function new_function is new Multiplier (num (i), inv (i));
      begin
         Ada.Text_IO.Put_Line (Float'Image (new_function (0.5)));
      end;
   end loop;
end Firstclass;
Task: First-class functions_Use numbers analogously



C#: using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        double x, xi, y, yi, z, zi;
        x = 2.0;
        xi = 0.5;
        y = 4.0;
        yi = 0.25;
        z = x + y;
        zi = 1.0 / (x + y);

        var numlist = new[] { x, y, z };
        var numlisti = new[] { xi, yi, zi };
        var multiplied = numlist.Zip(numlisti, (n1, n2) =>
                       {
                           Func<double, double> multiplier = m => n1 * n2 * m;
                           return multiplier;
                       });

        foreach (var multiplier in multiplied)
            Console.WriteLine(multiplier(0.5));
    }
}
Task: First-class functions_Use numbers analogously


C++: #include <array>
#include <iostream>

int main()
{
  double x  = 2.0;
  double xi = 0.5;
  double y  = 4.0;
  double yi = 0.25;
  double z  = x + y;
  double zi = 1.0 / ( x + y );

  const std::array values{x, y, z};
  const std::array inverses{xi, yi, zi};

  auto multiplier = [](double a, double b)
  {
    return [=](double m){return a * b * m;};
  };

  for(size_t i = 0; i < values.size(); ++i)
  {
    auto new_function = multiplier(values[i], inverses[i]);
    double value = new_function(i + 1.0);
    std::cout << value << "\n"; 
  }
}
Task: First-class functions_Use numbers analogously







Python: IDLE 2.6.1      
>>> 
>>> x,xi, y,yi = 2.0,0.5, 4.0,0.25
>>> 
>>> z  = x + y
>>> zi = 1.0 / (x + y)
>>> 
>>> multiplier = lambda n1, n2: (lambda m: n1 * n2 * m)
>>> 
>>> numlist = [x, y, z]
>>> numlisti = [xi, yi, zi]
>>> 
>>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)]
[0.5, 0.5, 0.5]
>>>