WeixiangYan/CodeTransOcean huggingface dataset:
Task: Thue-Morse



C#: using System;
using System.Text;

namespace ThueMorse
{
    class Program
    {
        static void Main(string[] args)
        {
            Sequence(6);
        }

        public static void Sequence(int steps)
        {
            var sb1 = new StringBuilder("0");
            var sb2 = new StringBuilder("1");
            for (int i = 0; i < steps; i++)
            {
                var tmp = sb1.ToString();
                sb1.Append(sb2);
                sb2.Append(tmp);
            }
            Console.WriteLine(sb1);
            Console.ReadLine();
        }
    }
}







COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. THUE-MORSE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STRINGS.
          03 CURRENT-STATE      PIC X(64).
          03 TEMP               PIC X(64).

       PROCEDURE DIVISION.
       BEGIN.
           MOVE "0" TO CURRENT-STATE.
           PERFORM THUE-MORSE-STEP 8 TIMES.
           DISPLAY CURRENT-STATE.
           STOP RUN.

       THUE-MORSE-STEP.
           MOVE CURRENT-STATE TO TEMP.
           INSPECT TEMP REPLACING ALL '0' BY 'X'.
           INSPECT TEMP REPLACING ALL '1' BY '0'.
           INSPECT TEMP REPLACING ALL 'X' BY '1'.
           STRING CURRENT-STATE DELIMITED BY SPACE,
                  TEMP DELIMITED BY SPACE
                  INTO CURRENT-STATE.
Task: Thue-Morse


C++: #include <iostream>
#include <iterator>
#include <vector>
int main( int argc, char* argv[] ) {
    std::vector<bool> t;
    t.push_back( 0 );
    size_t len = 1;
    std::cout << t[0] << "\n";
    do {
        for( size_t x = 0; x < len; x++ )
            t.push_back( t[x] ? 0 : 1 );
        std::copy( t.begin(), t.end(), std::ostream_iterator<bool>( std::cout ) );
        std::cout << "\n";
        len = t.size();
    } while( len < 60 );
    return 0;
}








COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. THUE-MORSE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STRINGS.
          03 CURRENT-STATE      PIC X(64).
          03 TEMP               PIC X(64).

       PROCEDURE DIVISION.
       BEGIN.
           MOVE "0" TO CURRENT-STATE.
           PERFORM THUE-MORSE-STEP 8 TIMES.
           DISPLAY CURRENT-STATE.
           STOP RUN.

       THUE-MORSE-STEP.
           MOVE CURRENT-STATE TO TEMP.
           INSPECT TEMP REPLACING ALL '0' BY 'X'.
           INSPECT TEMP REPLACING ALL '1' BY '0'.
           INSPECT TEMP REPLACING ALL 'X' BY '1'.
           STRING CURRENT-STATE DELIMITED BY SPACE,
                  TEMP DELIMITED BY SPACE
                  INTO CURRENT-STATE.
Task: Thue-Morse





Java: public class ThueMorse {

    public static void main(String[] args) {
        sequence(6);
    }

    public static void sequence(int steps) {
        StringBuilder sb1 = new StringBuilder("0");
        StringBuilder sb2 = new StringBuilder("1");
        for (int i = 0; i < steps; i++) {
            String tmp = sb1.toString();
            sb1.append(sb2);
            sb2.append(tmp);
        }
        System.out.println(sb1);
    }
}





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. THUE-MORSE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STRINGS.
          03 CURRENT-STATE      PIC X(64).
          03 TEMP               PIC X(64).

       PROCEDURE DIVISION.
       BEGIN.
           MOVE "0" TO CURRENT-STATE.
           PERFORM THUE-MORSE-STEP 8 TIMES.
           DISPLAY CURRENT-STATE.
           STOP RUN.

       THUE-MORSE-STEP.
           MOVE CURRENT-STATE TO TEMP.
           INSPECT TEMP REPLACING ALL '0' BY 'X'.
           INSPECT TEMP REPLACING ALL '1' BY '0'.
           INSPECT TEMP REPLACING ALL 'X' BY '1'.
           STRING CURRENT-STATE DELIMITED BY SPACE,
                  TEMP DELIMITED BY SPACE
                  INTO CURRENT-STATE.
Task: Thue-Morse







Python: m='0'
print(m)
for i in range(0,6):
     m0=m
     m=m.replace('0','a')
     m=m.replace('1','0')
     m=m.replace('a','1')
     m=m0+m
     print(m)



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. THUE-MORSE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STRINGS.
          03 CURRENT-STATE      PIC X(64).
          03 TEMP               PIC X(64).

       PROCEDURE DIVISION.
       BEGIN.
           MOVE "0" TO CURRENT-STATE.
           PERFORM THUE-MORSE-STEP 8 TIMES.
           DISPLAY CURRENT-STATE.
           STOP RUN.

       THUE-MORSE-STEP.
           MOVE CURRENT-STATE TO TEMP.
           INSPECT TEMP REPLACING ALL '0' BY 'X'.
           INSPECT TEMP REPLACING ALL '1' BY '0'.
           INSPECT TEMP REPLACING ALL 'X' BY '1'.
           STRING CURRENT-STATE DELIMITED BY SPACE,
                  TEMP DELIMITED BY SPACE
                  INTO CURRENT-STATE.
Task: Thue-Morse








VB: Option Explicit

Sub Main()
Dim i&, t$
    For i = 1 To 8
        t = Thue_Morse(t)
        Debug.Print i & ":=> " & t
    Next
End Sub

Private Function Thue_Morse(s As String) As String
Dim k$
    If s = "" Then
        k = "0"
    Else
        k = s
        k = Replace(k, "1", "2")
        k = Replace(k, "0", "1")
        k = Replace(k, "2", "0")
    End If
    Thue_Morse = s & k
End Function


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. THUE-MORSE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STRINGS.
          03 CURRENT-STATE      PIC X(64).
          03 TEMP               PIC X(64).

       PROCEDURE DIVISION.
       BEGIN.
           MOVE "0" TO CURRENT-STATE.
           PERFORM THUE-MORSE-STEP 8 TIMES.
           DISPLAY CURRENT-STATE.
           STOP RUN.

       THUE-MORSE-STEP.
           MOVE CURRENT-STATE TO TEMP.
           INSPECT TEMP REPLACING ALL '0' BY 'X'.
           INSPECT TEMP REPLACING ALL '1' BY '0'.
           INSPECT TEMP REPLACING ALL 'X' BY '1'.
           STRING CURRENT-STATE DELIMITED BY SPACE,
                  TEMP DELIMITED BY SPACE
                  INTO CURRENT-STATE.
Task: Thue-Morse




Go: 

package main

import (
    "fmt"
    "bytes"
)



func nextTMSequenceMember( tmBuffer * bytes.Buffer ) {
    
    for b, currLength, currBytes := 0, tmBuffer.Len(), tmBuffer.Bytes() ; b < currLength; b ++ {
        if currBytes[ b ] == '1' {
            tmBuffer.WriteByte( '0' )
        } else {
            tmBuffer.WriteByte( '1' )
        }
    }
}
    
func main() {
    var tmBuffer bytes.Buffer
    
    tmBuffer.WriteByte( '0' )
    fmt.Println( tmBuffer.String() )
    for i := 2; i <= 7; i ++ {
        nextTMSequenceMember( & tmBuffer )
        fmt.Println( tmBuffer.String() )
    }
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. THUE-MORSE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STRINGS.
          03 CURRENT-STATE      PIC X(64).
          03 TEMP               PIC X(64).

       PROCEDURE DIVISION.
       BEGIN.
           MOVE "0" TO CURRENT-STATE.
           PERFORM THUE-MORSE-STEP 8 TIMES.
           DISPLAY CURRENT-STATE.
           STOP RUN.

       THUE-MORSE-STEP.
           MOVE CURRENT-STATE TO TEMP.
           INSPECT TEMP REPLACING ALL '0' BY 'X'.
           INSPECT TEMP REPLACING ALL '1' BY '0'.
           INSPECT TEMP REPLACING ALL 'X' BY '1'.
           STRING CURRENT-STATE DELIMITED BY SPACE,
                  TEMP DELIMITED BY SPACE
                  INTO CURRENT-STATE.
Task: Thue-Morse






PHP: <?php

function thueMorseSequence($length) {
    $sequence = '';
    for ($digit = $n = 0 ; $n < $length ; $n++) {
        $x = $n ^ ($n - 1);
        if (($x ^ ($x >> 1)) & 0x55555555) {
            $digit = 1 - $digit;
        }
        $sequence .= $digit;
    }
    return $sequence;
}

for ($n = 10 ; $n <= 100 ; $n += 10) {
    echo sprintf('%3d', $n), ' : ', thueMorseSequence($n), PHP_EOL;
}
Task: Thue-Morse






PHP: <?php

function thueMorseSequence($length) {
    $sequence = '';
    for ($digit = $n = 0 ; $n < $length ; $n++) {
        $x = $n ^ ($n - 1);
        if (($x ^ ($x >> 1)) & 0x55555555) {
            $digit = 1 - $digit;
        }
        $sequence .= $digit;
    }
    return $sequence;
}

for ($n = 10 ; $n <= 100 ; $n += 10) {
    echo sprintf('%3d', $n), ' : ', thueMorseSequence($n), PHP_EOL;
}






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

procedure Thue_Morse is
   
   function Replace(S: String) return String is
      
      (if S'Length = 0 then "" 
      else (if S(S'First) = '0' then "01" else "10") & 
	Replace(S(S'First+1 .. S'Last)));
      
   function Sequence (N: Natural) return String is
      (if N=0 then "0" else Replace(Sequence(N-1)));   
      
begin
   for I in 0 .. 6 loop
      Ada.Text_IO.Put_Line(Integer'Image(I) & ": " & Sequence(I));
   end loop;
end Thue_Morse;
Task: Thue-Morse






PHP: <?php

function thueMorseSequence($length) {
    $sequence = '';
    for ($digit = $n = 0 ; $n < $length ; $n++) {
        $x = $n ^ ($n - 1);
        if (($x ^ ($x >> 1)) & 0x55555555) {
            $digit = 1 - $digit;
        }
        $sequence .= $digit;
    }
    return $sequence;
}

for ($n = 10 ; $n <= 100 ; $n += 10) {
    echo sprintf('%3d', $n), ' : ', thueMorseSequence($n), PHP_EOL;
}




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. THUE-MORSE.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 STRINGS.
          03 CURRENT-STATE      PIC X(64).
          03 TEMP               PIC X(64).

       PROCEDURE DIVISION.
       BEGIN.
           MOVE "0" TO CURRENT-STATE.
           PERFORM THUE-MORSE-STEP 8 TIMES.
           DISPLAY CURRENT-STATE.
           STOP RUN.

       THUE-MORSE-STEP.
           MOVE CURRENT-STATE TO TEMP.
           INSPECT TEMP REPLACING ALL '0' BY 'X'.
           INSPECT TEMP REPLACING ALL '1' BY '0'.
           INSPECT TEMP REPLACING ALL 'X' BY '1'.
           STRING CURRENT-STATE DELIMITED BY SPACE,
                  TEMP DELIMITED BY SPACE
                  INTO CURRENT-STATE.
Task: Matrix chain multiplication



C#: using System;

class MatrixChainOrderOptimizer {
    private int[,] m;
    private int[,] s;

    void OptimalMatrixChainOrder(int[] dims) {
        int n = dims.Length - 1;
        m = new int[n, n];
        s = new int[n, n];
        for (int len = 1; len < n; ++len) {
            for (int i = 0; i < n - len; ++i) {
                int j = i + len;
                m[i, j] = Int32.MaxValue;
                for (int k = i; k < j; ++k) {
                    int temp = dims[i] * dims[k + 1] * dims[j + 1];
                    int cost = m[i, k] + m[k + 1, j] + temp;
                    if (cost < m[i, j]) {
                        m[i, j] = cost;
                        s[i, j] = k;
                    }
                }
            }
        }
    }

    void PrintOptimalChainOrder(int i, int j) {
        if (i == j)
            Console.Write((char)(i + 65));
        else {
            Console.Write("(");
            PrintOptimalChainOrder(i, s[i, j]);
            PrintOptimalChainOrder(s[i, j] + 1, j);
            Console.Write(")");
        }
    }

    static void Main() {
        var mcoo = new MatrixChainOrderOptimizer();
        var dimsList = new int[3][];
        dimsList[0] = new int[4] {5, 6, 3, 1};
        dimsList[1] = new int[13] {1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2};
        dimsList[2] = new int[12] {1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10};
        for (int i = 0; i < dimsList.Length; ++i) {
            Console.Write("Dims  : [");
            int n = dimsList[i].Length;
            for (int j = 0; j < n; ++j) {
                Console.Write(dimsList[i][j]);
                if (j < n - 1)
                    Console.Write(", ");
                else
                    Console.WriteLine("]");
            }
            mcoo.OptimalMatrixChainOrder(dimsList[i]);
            Console.Write("Order : ");
            mcoo.PrintOptimalChainOrder(0, n - 2);
            Console.WriteLine("\nCost  : {0}\n",  mcoo.m[0, n - 2]);
        }
    }
}









Ada: package mat_chain is
   type Vector is array (Natural range <>) of Integer;
   procedure Chain_Multiplication (Dims : Vector);
end mat_chain;
Task: Matrix chain multiplication

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

int **m;
int **s;

void optimal_matrix_chain_order(int *dims, int n) {
    int len, i, j, k, temp, cost;
    n--;
    m = (int **)malloc(n * sizeof(int *));
    for (i = 0; i < n; ++i) {
        m[i] = (int *)calloc(n, sizeof(int));
    }

    s = (int **)malloc(n * sizeof(int *));
    for (i = 0; i < n; ++i) {
        s[i] = (int *)calloc(n, sizeof(int));
    }

    for (len = 1; len < n; ++len) {
        for (i = 0; i < n - len; ++i) {
            j = i + len;
            m[i][j] = INT_MAX;
            for (k = i; k < j; ++k) {
                temp = dims[i] * dims[k + 1] * dims[j + 1];
                cost = m[i][k] + m[k + 1][j] + temp;
                if (cost < m[i][j]) {
                    m[i][j] = cost;
                    s[i][j] = k;
                }
            }
        }
    }
}

void print_optimal_chain_order(int i, int j) {
    if (i == j)
        printf("%c", i + 65);
    else {
        printf("(");
        print_optimal_chain_order(i, s[i][j]);
        print_optimal_chain_order(s[i][j] + 1, j);
        printf(")");
    }
}

int main() {
    int i, j, n;
    int a1[4]  = {5, 6, 3, 1};
    int a2[13] = {1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2};
    int a3[12] = {1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10};
    int *dims_list[3] = {a1, a2, a3};
    int sizes[3] = {4, 13, 12};
    for (i = 0; i < 3; ++i) {
        printf("Dims  : [");
        n = sizes[i];
        for (j = 0; j < n; ++j) {
            printf("%d", dims_list[i][j]);
            if (j < n - 1) printf(", "); else printf("]\n");
        }        
        optimal_matrix_chain_order(dims_list[i], n);
        printf("Order : ");
        print_optimal_chain_order(0, n - 2);
        printf("\nCost  : %d\n\n", m[0][n - 2]);
        for (j = 0; j <= n - 2; ++j) free(m[j]);
        free(m);
        for (j = 0; j <= n - 2; ++j) free(s[j]);
        free(s);
    }
    return 0;
}











Ada: package mat_chain is
   type Vector is array (Natural range <>) of Integer;
   procedure Chain_Multiplication (Dims : Vector);
end mat_chain;
Task: Matrix chain multiplication




Go: package main

import "fmt"




func PrintMatrixChainOrder(dims []int) {
	n := len(dims) - 1
	m, s := newSquareMatrices(n)

	
	
	
	
	
	for lenMinusOne := 1; lenMinusOne < n; lenMinusOne++ {
		for i := 0; i < n-lenMinusOne; i++ {
			j := i + lenMinusOne
			m[i][j] = -1
			for k := i; k < j; k++ {
				cost := m[i][k] + m[k+1][j] + dims[i]*dims[k+1]*dims[j+1]
				if m[i][j] < 0 || cost < m[i][j] {
					m[i][j] = cost
					s[i][j] = k
				}
			}
		}
	}

	
	const MatrixNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var subprint func(int, int)
	subprint = func(i, j int) {
		if i == j {
			return
		}
		k := s[i][j]
		subprint(i, k)
		subprint(k+1, j)
		fmt.Printf("%*s -> %s × %s%*scost=%d\n",
			n, MatrixNames[i:j+1],
			MatrixNames[i:k+1],
			MatrixNames[k+1:j+1],
			n+i-j, "", m[i][j],
		)
	}
	subprint(0, n-1)
}

func newSquareMatrices(n int) (m, s [][]int) {
	
	
	m = make([][]int, 2*n)
	m, s = m[:n:n], m[n:]
	tmp := make([]int, 2*n*n)
	for i := range m {
		m[i], tmp = tmp[:n:n], tmp[n:]
	}
	for i := range s {
		s[i], tmp = tmp[:n:n], tmp[n:]
	}
	return m, s
}

func main() {
	cases := [...][]int{
		{1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2},
		{1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10},
	}
	for _, tc := range cases {
		fmt.Println("Dimensions:", tc)
		PrintMatrixChainOrder(tc)
		fmt.Println()
	}
}








Ada: package mat_chain is
   type Vector is array (Natural range <>) of Integer;
   procedure Chain_Multiplication (Dims : Vector);
end mat_chain;
Task: Matrix chain multiplication





Java: import java.util.Arrays;

public class MatrixChainMultiplication {

    public static void main(String[] args) {
        runMatrixChainMultiplication(new int[] {5, 6, 3, 1});
        runMatrixChainMultiplication(new int[] {1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2});
        runMatrixChainMultiplication(new int[] {1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10});
    }
    
    private static void runMatrixChainMultiplication(int[] dims) {
        System.out.printf("Array Dimension  = %s%n", Arrays.toString(dims));
        System.out.printf("Cost             = %d%n", matrixChainOrder(dims));
        System.out.printf("Optimal Multiply = %s%n%n", getOptimalParenthesizations());
    }

    private static int[][]cost;
    private static int[][]order;
    
    public static int matrixChainOrder(int[] dims) {
        int n = dims.length - 1;
        cost = new int[n][n];
        order = new int[n][n];

        for (int lenMinusOne = 1 ; lenMinusOne < n ; lenMinusOne++) {
            for (int i = 0; i < n - lenMinusOne; i++) {
                int j = i + lenMinusOne;
                cost[i][j] = Integer.MAX_VALUE;
                for (int k = i; k < j; k++) {
                    int currentCost = cost[i][k] + cost[k+1][j] + dims[i]*dims[k+1]*dims[j+1];
                    if (currentCost < cost[i][j]) {
                        cost[i][j] = currentCost;
                        order[i][j] = k;
                    }
                }
            }
        }
        return cost[0][n-1];
    }

    private static String getOptimalParenthesizations() {
        return getOptimalParenthesizations(order, 0, order.length - 1);
    }
    
    private static String getOptimalParenthesizations(int[][]s, int i, int j) {
        if (i == j) {
            return String.format("%c", i+65);
        }
        else {
            StringBuilder sb = new StringBuilder();
            sb.append("(");
            sb.append(getOptimalParenthesizations(s, i, s[i][j]));
            sb.append(" * ");
            sb.append(getOptimalParenthesizations(s, s[i][j] + 1, j));
            sb.append(")");
            return sb.toString();
        }
    }

}







Ada: package mat_chain is
   type Vector is array (Natural range <>) of Integer;
   procedure Chain_Multiplication (Dims : Vector);
end mat_chain;
Task: Matrix chain multiplication







Python: def parens(n):
    def aux(n, k):
        if n == 1:
            yield k
        elif n == 2:
            yield [k, k + 1]
        else:
            a = []
            for i in range(1, n):
                for u in aux(i, k):
                    for v in aux(n - i, k + i):
                        yield [u, v]
    yield from aux(n, 0)





Ada: package mat_chain is
   type Vector is array (Natural range <>) of Integer;
   procedure Chain_Multiplication (Dims : Vector);
end mat_chain;
Task: Matrix chain multiplication








VB: Option Explicit
Option Base 1
Dim N As Long, U() As Long, V() As Long
Sub Optimize(A As Variant)
    Dim I As Long, J As Long, K As Long, C As Long
    N = UBound(A) - 1
    ReDim U(N, N), V(N, N)
    For I = 1 To N
        U(I, 1) = -1
        V(I, 1) = 0
    Next I
    
    For J = 2 To N
        For I = 1 To N - J + 1
            V(I, J) = &H7FFFFFFF
            For K = 1 To J - 1
                C = V(I, K) + V(I + K, J - K) + A(I) * A(I + K) * A(I + J)
                If C < V(I, J) Then
                    U(I, J) = K
                    V(I, J) = C
                End If
            Next K
        Next I
    Next J

    Debug.Print V(1, N);
    Call Aux(1, N)
    Debug.Print
    Erase U, V
End Sub
Sub Aux(I As Long, J As Long)
    Dim K As Long
    K = U(I, J)
    If K < 0 Then
        Debug.Print CStr(I);
    Else
        Debug.Print "(";
        Call Aux(I, K)
        Debug.Print "*";
        Call Aux(I + K, J - K)
        Debug.Print ")";
    End If
End Sub
Sub Test()
    Call Optimize(Array(5, 6, 3, 1))
    Call Optimize(Array(1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2))
    Call Optimize(Array(1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10))
End Sub




Ada: package mat_chain is
   type Vector is array (Natural range <>) of Integer;
   procedure Chain_Multiplication (Dims : Vector);
end mat_chain;
Task: Matrix chain multiplication




Go: package main

import "fmt"




func PrintMatrixChainOrder(dims []int) {
	n := len(dims) - 1
	m, s := newSquareMatrices(n)

	
	
	
	
	
	for lenMinusOne := 1; lenMinusOne < n; lenMinusOne++ {
		for i := 0; i < n-lenMinusOne; i++ {
			j := i + lenMinusOne
			m[i][j] = -1
			for k := i; k < j; k++ {
				cost := m[i][k] + m[k+1][j] + dims[i]*dims[k+1]*dims[j+1]
				if m[i][j] < 0 || cost < m[i][j] {
					m[i][j] = cost
					s[i][j] = k
				}
			}
		}
	}

	
	const MatrixNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var subprint func(int, int)
	subprint = func(i, j int) {
		if i == j {
			return
		}
		k := s[i][j]
		subprint(i, k)
		subprint(k+1, j)
		fmt.Printf("%*s -> %s × %s%*scost=%d\n",
			n, MatrixNames[i:j+1],
			MatrixNames[i:k+1],
			MatrixNames[k+1:j+1],
			n+i-j, "", m[i][j],
		)
	}
	subprint(0, n-1)
}

func newSquareMatrices(n int) (m, s [][]int) {
	
	
	m = make([][]int, 2*n)
	m, s = m[:n:n], m[n:]
	tmp := make([]int, 2*n*n)
	for i := range m {
		m[i], tmp = tmp[:n:n], tmp[n:]
	}
	for i := range s {
		s[i], tmp = tmp[:n:n], tmp[n:]
	}
	return m, s
}

func main() {
	cases := [...][]int{
		{1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2},
		{1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10},
	}
	for _, tc := range cases {
		fmt.Println("Dimensions:", tc)
		PrintMatrixChainOrder(tc)
		fmt.Println()
	}
}







Fortran: module optim_mod
    implicit none
contains
    subroutine optim(a)
        implicit none
        integer :: a(:), n, i, j, k
        integer, allocatable :: u(:, :)
        integer(8) :: c
        integer(8), allocatable :: v(:, :)

        n = ubound(a, 1) - 1
        allocate (u(n, n), v(n, n))
        v = huge(v)
        u(:, 1) = -1
        v(:, 1) = 0
        do j = 2, n
            do i = 1, n - j + 1
                do k = 1, j - 1
                    c = v(i, k) + v(i + k, j - k) + int(a(i), 8) * int(a(i + k), 8) * int(a(i + j), 8)
                    if (c < v(i, j)) then
                        u(i, j) = k
                        v(i, j) = c
                    end if
                end do
            end do
        end do
        write (*, "(I0,' ')", advance="no") v(1, n)
        call aux(1, n)
        print *
        deallocate (u, v)
    contains
        recursive subroutine aux(i, j)
            integer :: i, j, k

            k = u(i, j)
            if (k < 0) then
                write (*, "(I0)", advance="no") i
            else
                write (*, "('(')", advance="no")
                call aux(i, k)
                write (*, "('*')", advance="no")
                call aux(i + k, j - k)
                write (*, "(')')", advance="no")
            end if
        end subroutine
    end subroutine
end module

program matmulchain
    use optim_mod
    implicit none

    call optim([5, 6, 3, 1])
    call optim([1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2])
    call optim([1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10])
end program