WeixiangYan/CodeTransOcean huggingface dataset:
Task: Permutations


C++: #include <algorithm>
#include <string>
#include <vector>
#include <iostream>

template<class T>
void print(const std::vector<T> &vec)
{
    for (typename std::vector<T>::const_iterator i = vec.begin(); i != vec.end(); ++i)
    {
        std::cout << *i;
        if ((i + 1) != vec.end())
            std::cout << ",";
    }
    std::cout << std::endl;
}

int main()
{
    
    std::string example("Hello");
    std::sort(example.begin(), example.end());
    do {
        std::cout << example << '\n';
    } while (std::next_permutation(example.begin(), example.end()));

    
    std::vector<int> another;
    another.push_back(1234);
    another.push_back(4321);
    another.push_back(1234);
    another.push_back(9999);

    std::sort(another.begin(), another.end());
    do {
        print(another);
    } while (std::next_permutation(another.begin(), another.end()));

    return 0;
}










Ada: generic
   N: positive;
package Generic_Perm is
   subtype Element is Positive range 1 .. N;
   type Permutation is array(Element) of Element;
   
   procedure Set_To_First(P: out Permutation; Is_Last: out Boolean);
   procedure Go_To_Next(P: in out Permutation; Is_Last: out Boolean); 
end Generic_Perm;
Task: Permutations




Go: package main

import "fmt"

func main() {
    demoPerm(3)
}

func demoPerm(n int) {
    
    s := make([]int, n)
    for i := range s {
        s[i] = i + 1
    }
    
    
    permute(s, func(p []int) { fmt.Println(p) })
}



func permute(s []int, emit func([]int)) {
    if len(s) == 0 {
        emit(s)
        return
    }
    
    
    
    
    
    var rc func(int)
    rc = func(np int) {
        if np == 1 {
            emit(s)
            return
        }
        np1 := np - 1
        pp := len(s) - np1
        
        rc(np1)
        for i := pp; i > 0; i-- {
            s[i], s[i-1] = s[i-1], s[i]
            rc(np1)
        }
        
        w := s[0]
        copy(s, s[1:pp+1])
        s[pp] = w
    }
    rc(len(s))
}








Ada: generic
   N: positive;
package Generic_Perm is
   subtype Element is Positive range 1 .. N;
   type Permutation is array(Element) of Element;
   
   procedure Set_To_First(P: out Permutation; Is_Last: out Boolean);
   procedure Go_To_Next(P: in out Permutation; Is_Last: out Boolean); 
end Generic_Perm;
Task: Permutations





Java: 
module Permutations
    {
    static Int[][] permut(Int items)
        {
        if (items <= 1)
            {
            
            return items == 1 ? [[0]] : [];
            }

        
        
        Int[][] pattern = permut(items - 1);

        
        
        Int[][] result = new Int[][];
        for (Int prefix : 0 ..< items)
            {
            for (Int[] suffix : pattern)
                {
                result.add(new Int[items](i -> i == 0 ? prefix : (prefix + suffix[i-1] + 1) % items));
                }
            }
        return result;
        }

    void run()
        {
        @Inject Console console;
        console.print($"permut(3) = {permut(3)}");
        }
    }







Ada: generic
   N: positive;
package Generic_Perm is
   subtype Element is Positive range 1 .. N;
   type Permutation is array(Element) of Element;
   
   procedure Set_To_First(P: out Permutation; Is_Last: out Boolean);
   procedure Go_To_Next(P: in out Permutation; Is_Last: out Boolean); 
end Generic_Perm;
Task: Permutations







Python: import itertools
for values in itertools.permutations([1,2,3]):
    print (values)





Ada: generic
   N: positive;
package Generic_Perm is
   subtype Element is Positive range 1 .. N;
   type Permutation is array(Element) of Element;
   
   procedure Set_To_First(P: out Permutation; Is_Last: out Boolean);
   procedure Go_To_Next(P: in out Permutation; Is_Last: out Boolean); 
end Generic_Perm;
Task: Permutations








VB: Public Sub Permute(n As Integer, Optional printem As Boolean = True)

 
Dim P() As Integer
Dim t As Integer, i As Integer, j As Integer, k As Integer
Dim count As Long
Dim Last As Boolean
 
If n <= 1 Then

  Debug.Print "Please give a number greater than 1"
  Exit Sub

End If


ReDim P(n)

For i = 1 To n
  P(i) = i
Next

count = 0
Last = False
 
Do While Not Last
   
   If printem Then

      For t = 1 To n
        Debug.Print P(t);
      Next

      Debug.Print

   End If
   
count = count + 1

Last = True
i = n - 1

   Do While i > 0

     If P(i) < P(i + 1) Then

       Last = False
       Exit Do

     End If

     i = i - 1
   Loop

  j = i + 1
  k = n
  
  While j < k
    
    t = P(j)
    P(j) = P(k)
    P(k) = t
    j = j + 1
    k = k - 1
  Wend
  
  j = n
  
  While P(j) > P(i)
    j = j - 1
  Wend
  
  j = j + 1
  
  t = P(i)
  P(i) = P(j)
  P(j) = t
Loop 
 
Debug.Print "Number of permutations: "; count
 
End Sub




Ada: generic
   N: positive;
package Generic_Perm is
   subtype Element is Positive range 1 .. N;
   type Permutation is array(Element) of Element;
   
   procedure Set_To_First(P: out Permutation; Is_Last: out Boolean);
   procedure Go_To_Next(P: in out Permutation; Is_Last: out Boolean); 
end Generic_Perm;
Task: Permutations

C: #include <stdio.h>
int main (int argc, char *argv[]) {

	if (argc < 2) {
        printf("Enter an argument. Example 1234 or dcba:\n");
        return 0;
	}

        int x;
        for (x = 0; argv[1][x] != '\0'; x++);

	int f, v, m;
	 for(f=0; f < x; f++) {
    	 for(v = x-1; v > f; v-- ) {
     	 if (argv[1][v-1] > argv[1][v]) {
	m=argv[1][v-1];
	argv[1][v-1]=argv[1][v];
	argv[1][v]=m;
    }
  }
}


    char a[x];
	int k=0;
	int fact=k+1;
             while (k!=x) {
                   a[k]=argv[1][k];
               	   k++;
		  fact = k*fact;
                   }
                   a[k]='\0';

           int i, j;
           int y=0;
           char c;
          while (y != fact) {
          printf("%s\n", a);
          i=x-2;
          while(a[i] > a[i+1] ) i--;
          j=x-1;
          while(a[j] < a[i] ) j--;
      c=a[j];
      a[j]=a[i];
      a[i]=c;
i++;
for (j = x-1; j > i; i++, j--) {
  c = a[i];
  a[i] = a[j];
  a[j] = c;
      }
y++;
   }
}
Task: Permutations



C#: public static class Extension
{
    public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> values) where T : IComparable<T>
    {
        if (values.Count() == 1)
            return new[] { values };
        return values.SelectMany(v => Permutations(values.Where(x => x.CompareTo(v) != 0)), (v, p) => p.Prepend(v));
    }
}
Task: Permutations


C++: #include <algorithm>
#include <string>
#include <vector>
#include <iostream>

template<class T>
void print(const std::vector<T> &vec)
{
    for (typename std::vector<T>::const_iterator i = vec.begin(); i != vec.end(); ++i)
    {
        std::cout << *i;
        if ((i + 1) != vec.end())
            std::cout << ",";
    }
    std::cout << std::endl;
}

int main()
{
    
    std::string example("Hello");
    std::sort(example.begin(), example.end());
    do {
        std::cout << example << '\n';
    } while (std::next_permutation(example.begin(), example.end()));

    
    std::vector<int> another;
    another.push_back(1234);
    another.push_back(4321);
    another.push_back(1234);
    another.push_back(9999);

    std::sort(another.begin(), another.end());
    do {
        print(another);
    } while (std::next_permutation(another.begin(), another.end()));

    return 0;
}
Task: Permutations





Java: 
module Permutations
    {
    static Int[][] permut(Int items)
        {
        if (items <= 1)
            {
            
            return items == 1 ? [[0]] : [];
            }

        
        
        Int[][] pattern = permut(items - 1);

        
        
        Int[][] result = new Int[][];
        for (Int prefix : 0 ..< items)
            {
            for (Int[] suffix : pattern)
                {
                result.add(new Int[items](i -> i == 0 ? prefix : (prefix + suffix[i-1] + 1) % items));
                }
            }
        return result;
        }

    void run()
        {
        @Inject Console console;
        console.print($"permut(3) = {permut(3)}");
        }
    }
Task: Permutations








VB: Public Sub Permute(n As Integer, Optional printem As Boolean = True)

 
Dim P() As Integer
Dim t As Integer, i As Integer, j As Integer, k As Integer
Dim count As Long
Dim Last As Boolean
 
If n <= 1 Then

  Debug.Print "Please give a number greater than 1"
  Exit Sub

End If


ReDim P(n)

For i = 1 To n
  P(i) = i
Next

count = 0
Last = False
 
Do While Not Last
   
   If printem Then

      For t = 1 To n
        Debug.Print P(t);
      Next

      Debug.Print

   End If
   
count = count + 1

Last = True
i = n - 1

   Do While i > 0

     If P(i) < P(i + 1) Then

       Last = False
       Exit Do

     End If

     i = i - 1
   Loop

  j = i + 1
  k = n
  
  While j < k
    
    t = P(j)
    P(j) = P(k)
    P(k) = t
    j = j + 1
    k = k - 1
  Wend
  
  j = n
  
  While P(j) > P(i)
    j = j - 1
  Wend
  
  j = j + 1
  
  t = P(i)
  P(i) = P(j)
  P(j) = t
Loop 
 
Debug.Print "Number of permutations: "; count
 
End Sub
Task: Permutations




Go: package main

import "fmt"

func main() {
    demoPerm(3)
}

func demoPerm(n int) {
    
    s := make([]int, n)
    for i := range s {
        s[i] = i + 1
    }
    
    
    permute(s, func(p []int) { fmt.Println(p) })
}



func permute(s []int, emit func([]int)) {
    if len(s) == 0 {
        emit(s)
        return
    }
    
    
    
    
    
    var rc func(int)
    rc = func(np int) {
        if np == 1 {
            emit(s)
            return
        }
        np1 := np - 1
        pp := len(s) - np1
        
        rc(np1)
        for i := pp; i > 0; i-- {
            s[i], s[i-1] = s[i-1], s[i]
            rc(np1)
        }
        
        w := s[0]
        copy(s, s[1:pp+1])
        s[pp] = w
    }
    rc(len(s))
}
Task: Permutations







Python: import itertools
for values in itertools.permutations([1,2,3]):
    print (values)
Task: Permutations



C#: public static class Extension
{
    public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> values) where T : IComparable<T>
    {
        if (values.Count() == 1)
            return new[] { values };
        return values.SelectMany(v => Permutations(values.Where(x => x.CompareTo(v) != 0)), (v, p) => p.Prepend(v));
    }
}








Fortran: program permutations

  implicit none
  integer, parameter :: value_min = 1
  integer, parameter :: value_max = 3
  integer, parameter :: position_min = value_min
  integer, parameter :: position_max = value_max
  integer, dimension (position_min : position_max) :: permutation

  call generate (position_min)

contains

  recursive subroutine generate (position)

    implicit none
    integer, intent (in) :: position
    integer :: value

    if (position > position_max) then
      write (*, *) permutation
    else
      do value = value_min, value_max
        if (.not. any (permutation (: position - 1) == value)) then
          permutation (position) = value
          call generate (position + 1)
        end if
      end do
    end if

  end subroutine generate

end program permutations
Task: Permutations


C++: #include <algorithm>
#include <string>
#include <vector>
#include <iostream>

template<class T>
void print(const std::vector<T> &vec)
{
    for (typename std::vector<T>::const_iterator i = vec.begin(); i != vec.end(); ++i)
    {
        std::cout << *i;
        if ((i + 1) != vec.end())
            std::cout << ",";
    }
    std::cout << std::endl;
}

int main()
{
    
    std::string example("Hello");
    std::sort(example.begin(), example.end());
    do {
        std::cout << example << '\n';
    } while (std::next_permutation(example.begin(), example.end()));

    
    std::vector<int> another;
    another.push_back(1234);
    another.push_back(4321);
    another.push_back(1234);
    another.push_back(9999);

    std::sort(another.begin(), another.end());
    do {
        print(another);
    } while (std::next_permutation(another.begin(), another.end()));

    return 0;
}









Fortran: program permutations

  implicit none
  integer, parameter :: value_min = 1
  integer, parameter :: value_max = 3
  integer, parameter :: position_min = value_min
  integer, parameter :: position_max = value_max
  integer, dimension (position_min : position_max) :: permutation

  call generate (position_min)

contains

  recursive subroutine generate (position)

    implicit none
    integer, intent (in) :: position
    integer :: value

    if (position > position_max) then
      write (*, *) permutation
    else
      do value = value_min, value_max
        if (.not. any (permutation (: position - 1) == value)) then
          permutation (position) = value
          call generate (position + 1)
        end if
      end do
    end if

  end subroutine generate

end program permutations
Task: Permutations

C: #include <stdio.h>
int main (int argc, char *argv[]) {

	if (argc < 2) {
        printf("Enter an argument. Example 1234 or dcba:\n");
        return 0;
	}

        int x;
        for (x = 0; argv[1][x] != '\0'; x++);

	int f, v, m;
	 for(f=0; f < x; f++) {
    	 for(v = x-1; v > f; v-- ) {
     	 if (argv[1][v-1] > argv[1][v]) {
	m=argv[1][v-1];
	argv[1][v-1]=argv[1][v];
	argv[1][v]=m;
    }
  }
}


    char a[x];
	int k=0;
	int fact=k+1;
             while (k!=x) {
                   a[k]=argv[1][k];
               	   k++;
		  fact = k*fact;
                   }
                   a[k]='\0';

           int i, j;
           int y=0;
           char c;
          while (y != fact) {
          printf("%s\n", a);
          i=x-2;
          while(a[i] > a[i+1] ) i--;
          j=x-1;
          while(a[j] < a[i] ) j--;
      c=a[j];
      a[j]=a[i];
      a[i]=c;
i++;
for (j = x-1; j > i; i++, j--) {
  c = a[i];
  a[i] = a[j];
  a[j] = c;
      }
y++;
   }
}










Fortran: program permutations

  implicit none
  integer, parameter :: value_min = 1
  integer, parameter :: value_max = 3
  integer, parameter :: position_min = value_min
  integer, parameter :: position_max = value_max
  integer, dimension (position_min : position_max) :: permutation

  call generate (position_min)

contains

  recursive subroutine generate (position)

    implicit none
    integer, intent (in) :: position
    integer :: value

    if (position > position_max) then
      write (*, *) permutation
    else
      do value = value_min, value_max
        if (.not. any (permutation (: position - 1) == value)) then
          permutation (position) = value
          call generate (position + 1)
        end if
      end do
    end if

  end subroutine generate

end program permutations
Task: Permutations





Java: 
module Permutations
    {
    static Int[][] permut(Int items)
        {
        if (items <= 1)
            {
            
            return items == 1 ? [[0]] : [];
            }

        
        
        Int[][] pattern = permut(items - 1);

        
        
        Int[][] result = new Int[][];
        for (Int prefix : 0 ..< items)
            {
            for (Int[] suffix : pattern)
                {
                result.add(new Int[items](i -> i == 0 ? prefix : (prefix + suffix[i-1] + 1) % items));
                }
            }
        return result;
        }

    void run()
        {
        @Inject Console console;
        console.print($"permut(3) = {permut(3)}");
        }
    }






Fortran: program permutations

  implicit none
  integer, parameter :: value_min = 1
  integer, parameter :: value_max = 3
  integer, parameter :: position_min = value_min
  integer, parameter :: position_max = value_max
  integer, dimension (position_min : position_max) :: permutation

  call generate (position_min)

contains

  recursive subroutine generate (position)

    implicit none
    integer, intent (in) :: position
    integer :: value

    if (position > position_max) then
      write (*, *) permutation
    else
      do value = value_min, value_max
        if (.not. any (permutation (: position - 1) == value)) then
          permutation (position) = value
          call generate (position + 1)
        end if
      end do
    end if

  end subroutine generate

end program permutations
Task: Permutations







Python: import itertools
for values in itertools.permutations([1,2,3]):
    print (values)




Fortran: program permutations

  implicit none
  integer, parameter :: value_min = 1
  integer, parameter :: value_max = 3
  integer, parameter :: position_min = value_min
  integer, parameter :: position_max = value_max
  integer, dimension (position_min : position_max) :: permutation

  call generate (position_min)

contains

  recursive subroutine generate (position)

    implicit none
    integer, intent (in) :: position
    integer :: value

    if (position > position_max) then
      write (*, *) permutation
    else
      do value = value_min, value_max
        if (.not. any (permutation (: position - 1) == value)) then
          permutation (position) = value
          call generate (position + 1)
        end if
      end do
    end if

  end subroutine generate

end program permutations
Task: Permutations








VB: Public Sub Permute(n As Integer, Optional printem As Boolean = True)

 
Dim P() As Integer
Dim t As Integer, i As Integer, j As Integer, k As Integer
Dim count As Long
Dim Last As Boolean
 
If n <= 1 Then

  Debug.Print "Please give a number greater than 1"
  Exit Sub

End If


ReDim P(n)

For i = 1 To n
  P(i) = i
Next

count = 0
Last = False
 
Do While Not Last
   
   If printem Then

      For t = 1 To n
        Debug.Print P(t);
      Next

      Debug.Print

   End If
   
count = count + 1

Last = True
i = n - 1

   Do While i > 0

     If P(i) < P(i + 1) Then

       Last = False
       Exit Do

     End If

     i = i - 1
   Loop

  j = i + 1
  k = n
  
  While j < k
    
    t = P(j)
    P(j) = P(k)
    P(k) = t
    j = j + 1
    k = k - 1
  Wend
  
  j = n
  
  While P(j) > P(i)
    j = j - 1
  Wend
  
  j = j + 1
  
  t = P(i)
  P(i) = P(j)
  P(j) = t
Loop 
 
Debug.Print "Number of permutations: "; count
 
End Sub



Fortran: program permutations

  implicit none
  integer, parameter :: value_min = 1
  integer, parameter :: value_max = 3
  integer, parameter :: position_min = value_min
  integer, parameter :: position_max = value_max
  integer, dimension (position_min : position_max) :: permutation

  call generate (position_min)

contains

  recursive subroutine generate (position)

    implicit none
    integer, intent (in) :: position
    integer :: value

    if (position > position_max) then
      write (*, *) permutation
    else
      do value = value_min, value_max
        if (.not. any (permutation (: position - 1) == value)) then
          permutation (position) = value
          call generate (position + 1)
        end if
      end do
    end if

  end subroutine generate

end program permutations
Task: Proper divisors



C#: namespace RosettaCode.ProperDivisors
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal static class Program
    {
        private static IEnumerable<int> ProperDivisors(int number)
        {
            return
                Enumerable.Range(1, number / 2)
                    .Where(divisor => number % divisor == 0);
        }

        private static void Main()
        {
            foreach (var number in Enumerable.Range(1, 10))
            {
                Console.WriteLine("{0}: {{{1}}}", number,
                    string.Join(", ", ProperDivisors(number)));
            }

            var record = Enumerable.Range(1, 20000).Select(number => new
            {
                Number = number,
                Count = ProperDivisors(number).Count()
            }).OrderByDescending(currentRecord => currentRecord.Count).First();
            Console.WriteLine("{0}: {1}", record.Number, record.Count);
        }
    }
}









Ada: generic
   type Result_Type (<>) is limited private;
   None: Result_Type;
   with function One(X: Positive) return Result_Type;
   with function Add(X, Y: Result_Type) return Result_Type 
      is <>;
package Generic_Divisors is
  
  function Process
    (N: Positive; First: Positive := 1) return Result_Type is      
      (if First**2 > N or First = N then None 
      elsif (N mod First)=0 then 
	(if First = 1 or First*First = N 
	   then Add(One(First), Process(N, First+1))
	   else Add(One(First), 
		    Add(One((N/First)), Process(N, First+1))))
      else Process(N, First+1));
      
end Generic_Divisors;
Task: Proper divisors

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

int proper_divisors(const int n, bool print_flag)
{
    int count = 0;

    for (int i = 1; i < n; ++i) {
        if (n % i == 0) {
            count++;
            if (print_flag)
                printf("%d ", i);
        }
    }

    if (print_flag)
        printf("\n");

    return count;
}

int main(void)
{
    for (int i = 1; i <= 10; ++i) {
        printf("%d: ", i);
        proper_divisors(i, true);
    }

    int max = 0;
    int max_i = 1;

    for (int i = 1; i <= 20000; ++i) {
        int v = proper_divisors(i, false);
        if (v >= max) {
            max = v;
            max_i = i;
        }
    }

    printf("%d with %d divisors\n", max_i, max);
    return 0;
}











Ada: generic
   type Result_Type (<>) is limited private;
   None: Result_Type;
   with function One(X: Positive) return Result_Type;
   with function Add(X, Y: Result_Type) return Result_Type 
      is <>;
package Generic_Divisors is
  
  function Process
    (N: Positive; First: Positive := 1) return Result_Type is      
      (if First**2 > N or First = N then None 
      elsif (N mod First)=0 then 
	(if First = 1 or First*First = N 
	   then Add(One(First), Process(N, First+1))
	   else Add(One(First), 
		    Add(One((N/First)), Process(N, First+1))))
      else Process(N, First+1));
      
end Generic_Divisors;
Task: Proper divisors


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

std::vector<int> properDivisors ( int number ) {
   std::vector<int> divisors ;
   for ( int i = 1 ; i < number / 2 + 1 ; i++ )
      if ( number % i == 0 )
	 divisors.push_back( i ) ;
   return divisors ;
}

int main( ) {
   std::vector<int> divisors ;
   unsigned int maxdivisors = 0 ;
   int corresponding_number = 0 ;
   for ( int i = 1 ; i < 11 ; i++ ) {
      divisors =  properDivisors ( i ) ;
      std::cout << "Proper divisors of " << i << ":\n" ;
      for ( int number : divisors ) { 
	 std::cout << number << " " ;
      }
      std::cout << std::endl ;
      divisors.clear( ) ;
   }
   for ( int i = 11 ; i < 20001 ; i++ ) {
      divisors =  properDivisors ( i ) ;
      if ( divisors.size( ) > maxdivisors ) {
	 maxdivisors = divisors.size( ) ;
	 corresponding_number = i ;
      }
      divisors.clear( ) ;
   }

   std::cout << "Most divisors has " << corresponding_number <<
      " , it has " << maxdivisors << " divisors!\n" ; 
   return 0 ;
}










Ada: generic
   type Result_Type (<>) is limited private;
   None: Result_Type;
   with function One(X: Positive) return Result_Type;
   with function Add(X, Y: Result_Type) return Result_Type 
      is <>;
package Generic_Divisors is
  
  function Process
    (N: Positive; First: Positive := 1) return Result_Type is      
      (if First**2 > N or First = N then None 
      elsif (N mod First)=0 then 
	(if First = 1 or First*First = N 
	   then Add(One(First), Process(N, First+1))
	   else Add(One(First), 
		    Add(One((N/First)), Process(N, First+1))))
      else Process(N, First+1));
      
end Generic_Divisors;