WeixiangYan/CodeTransOcean huggingface dataset:
Task: Modular inverse


C++: #include <iostream>
 
int mul_inv(int a, int b)
{
	int b0 = b, t, q;
	int x0 = 0, x1 = 1;
	if (b == 1) return 1;
	while (a > 1) {
		q = a / b;
		t = b, b = a % b, a = t;
		t = x0, x0 = x1 - q * x0, x1 = t;
	}
	if (x1 < 0) x1 += b0;
	return x1;
}
 
int main(void) {
	std::cout << mul_inv(42, 2017) << std::endl;
	return 0;
}









Fortran: program modular_inverse_task

  implicit none

  write (*,*) inverse (42, 2017)

contains

  
  
  
  function inverse (a, n) result (inverse_value)
    integer, intent(in) :: a, n
    integer :: inverse_value

    integer :: t, newt
    integer :: r, newr
    integer :: quotient, remainder, tmp

    if (n <= 0) error stop
    t = 0;  newt = 1
    r = n;  newr = a
    do while (newr /= 0)
       remainder = modulo (r, newr) 
       quotient = (r - remainder) / newr
       tmp = newt;  newt = t - (quotient * newt);  t = tmp
       r   = newr;  newr = remainder
    end do
    if (r > 1) then
       inverse_value = -1
    else if (t < 0) then
       inverse_value = t + n
    else
       inverse_value = t
    end if
  end function inverse

end program modular_inverse_task
Task: Modular inverse

C: #include <stdio.h>

int mul_inv(int a, int b)
{
	int b0 = b, t, q;
	int x0 = 0, x1 = 1;
	if (b == 1) return 1;
	while (a > 1) {
		q = a / b;
		t = b, b = a % b, a = t;
		t = x0, x0 = x1 - q * x0, x1 = t;
	}
	if (x1 < 0) x1 += b0;
	return x1;
}

int main(void) {
	printf("%d\n", mul_inv(42, 2017));
	return 0;
}










Fortran: program modular_inverse_task

  implicit none

  write (*,*) inverse (42, 2017)

contains

  
  
  
  function inverse (a, n) result (inverse_value)
    integer, intent(in) :: a, n
    integer :: inverse_value

    integer :: t, newt
    integer :: r, newr
    integer :: quotient, remainder, tmp

    if (n <= 0) error stop
    t = 0;  newt = 1
    r = n;  newr = a
    do while (newr /= 0)
       remainder = modulo (r, newr) 
       quotient = (r - remainder) / newr
       tmp = newt;  newt = t - (quotient * newt);  t = tmp
       r   = newr;  newr = remainder
    end do
    if (r > 1) then
       inverse_value = -1
    else if (t < 0) then
       inverse_value = t + n
    else
       inverse_value = t
    end if
  end function inverse

end program modular_inverse_task
Task: Modular inverse





Java: System.out.println(BigInteger.valueOf(42).modInverse(BigInteger.valueOf(2017)));






Fortran: program modular_inverse_task

  implicit none

  write (*,*) inverse (42, 2017)

contains

  
  
  
  function inverse (a, n) result (inverse_value)
    integer, intent(in) :: a, n
    integer :: inverse_value

    integer :: t, newt
    integer :: r, newr
    integer :: quotient, remainder, tmp

    if (n <= 0) error stop
    t = 0;  newt = 1
    r = n;  newr = a
    do while (newr /= 0)
       remainder = modulo (r, newr) 
       quotient = (r - remainder) / newr
       tmp = newt;  newt = t - (quotient * newt);  t = tmp
       r   = newr;  newr = remainder
    end do
    if (r > 1) then
       inverse_value = -1
    else if (t < 0) then
       inverse_value = t + n
    else
       inverse_value = t
    end if
  end function inverse

end program modular_inverse_task
Task: Modular inverse







Python: >>> def extended_gcd(aa, bb):
    lastremainder, remainder = abs(aa), abs(bb)
    x, lastx, y, lasty = 0, 1, 1, 0
    while remainder:
        lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder)
        x, lastx = lastx - quotient*x, x
        y, lasty = lasty - quotient*y, y
    return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1)

>>> def modinv(a, m):
	g, x, y = extended_gcd(a, m)
	if g != 1:
		raise ValueError
	return x % m

>>> modinv(42, 2017)
1969
>>>




Fortran: program modular_inverse_task

  implicit none

  write (*,*) inverse (42, 2017)

contains

  
  
  
  function inverse (a, n) result (inverse_value)
    integer, intent(in) :: a, n
    integer :: inverse_value

    integer :: t, newt
    integer :: r, newr
    integer :: quotient, remainder, tmp

    if (n <= 0) error stop
    t = 0;  newt = 1
    r = n;  newr = a
    do while (newr /= 0)
       remainder = modulo (r, newr) 
       quotient = (r - remainder) / newr
       tmp = newt;  newt = t - (quotient * newt);  t = tmp
       r   = newr;  newr = remainder
    end do
    if (r > 1) then
       inverse_value = -1
    else if (t < 0) then
       inverse_value = t + n
    else
       inverse_value = t
    end if
  end function inverse

end program modular_inverse_task
Task: Modular inverse








VB: Private Function mul_inv(a As Long, n As Long) As Variant
    If n < 0 Then n = -n
    If a < 0 Then a = n - ((-a) Mod n)
    Dim t As Long: t = 0
    Dim nt As Long: nt = 1
    Dim r As Long: r = n
    Dim nr As Long: nr = a
    Dim q As Long
    Do While nr <> 0
        q = r \ nr
        tmp = t
        t = nt
        nt = tmp - q * nt
        tmp = r
        r = nr
        nr = tmp - q * nr
    Loop
    If r > 1 Then
        mul_inv = "a is not invertible"
    Else
        If t < 0 Then t = t + n
        mul_inv = t
    End If
End Function
Public Sub mi()
    Debug.Print mul_inv(42, 2017)
    Debug.Print mul_inv(40, 1)
    Debug.Print mul_inv(52, -217) 
    Debug.Print mul_inv(-486, 217)
    Debug.Print mul_inv(40, 2018)
End Sub



Fortran: program modular_inverse_task

  implicit none

  write (*,*) inverse (42, 2017)

contains

  
  
  
  function inverse (a, n) result (inverse_value)
    integer, intent(in) :: a, n
    integer :: inverse_value

    integer :: t, newt
    integer :: r, newr
    integer :: quotient, remainder, tmp

    if (n <= 0) error stop
    t = 0;  newt = 1
    r = n;  newr = a
    do while (newr /= 0)
       remainder = modulo (r, newr) 
       quotient = (r - remainder) / newr
       tmp = newt;  newt = t - (quotient * newt);  t = tmp
       r   = newr;  newr = remainder
    end do
    if (r > 1) then
       inverse_value = -1
    else if (t < 0) then
       inverse_value = t + n
    else
       inverse_value = t
    end if
  end function inverse

end program modular_inverse_task
Task: Modular inverse






PHP: <?php
function invmod($a,$n){
        if ($n < 0) $n = -$n;
        if ($a < 0) $a = $n - (-$a % $n);
	$t = 0; $nt = 1; $r = $n; $nr = $a % $n;
	while ($nr != 0) {
		$quot= intval($r/$nr);
		$tmp = $nt;  $nt = $t - $quot*$nt;  $t = $tmp;
		$tmp = $nr;  $nr = $r - $quot*$nr;  $r = $tmp;
	}
	if ($r > 1) return -1;
	if ($t < 0) $t += $n;
	return $t;
}
	printf("%d\n", invmod(42, 2017));
?>





Fortran: program modular_inverse_task

  implicit none

  write (*,*) inverse (42, 2017)

contains

  
  
  
  function inverse (a, n) result (inverse_value)
    integer, intent(in) :: a, n
    integer :: inverse_value

    integer :: t, newt
    integer :: r, newr
    integer :: quotient, remainder, tmp

    if (n <= 0) error stop
    t = 0;  newt = 1
    r = n;  newr = a
    do while (newr /= 0)
       remainder = modulo (r, newr) 
       quotient = (r - remainder) / newr
       tmp = newt;  newt = t - (quotient * newt);  t = tmp
       r   = newr;  newr = remainder
    end do
    if (r > 1) then
       inverse_value = -1
    else if (t < 0) then
       inverse_value = t + n
    else
       inverse_value = t
    end if
  end function inverse

end program modular_inverse_task
Task: Modular inverse






PHP: <?php
function invmod($a,$n){
        if ($n < 0) $n = -$n;
        if ($a < 0) $a = $n - (-$a % $n);
	$t = 0; $nt = 1; $r = $n; $nr = $a % $n;
	while ($nr != 0) {
		$quot= intval($r/$nr);
		$tmp = $nt;  $nt = $t - $quot*$nt;  $t = $tmp;
		$tmp = $nr;  $nr = $r - $quot*$nr;  $r = $tmp;
	}
	if ($r > 1) return -1;
	if ($t < 0) $t += $n;
	return $t;
}
	printf("%d\n", invmod(42, 2017));
?>
Task: Modular inverse






PHP: <?php
function invmod($a,$n){
        if ($n < 0) $n = -$n;
        if ($a < 0) $a = $n - (-$a % $n);
	$t = 0; $nt = 1; $r = $n; $nr = $a % $n;
	while ($nr != 0) {
		$quot= intval($r/$nr);
		$tmp = $nt;  $nt = $t - $quot*$nt;  $t = $tmp;
		$tmp = $nr;  $nr = $r - $quot*$nr;  $r = $tmp;
	}
	if ($r > 1) return -1;
	if ($t < 0) $t += $n;
	return $t;
}
	printf("%d\n", invmod(42, 2017));
?>






Ada: with Ada.Text_IO;use Ada.Text_IO;
procedure modular_inverse is
  
  
  function inv_mod (a : Integer; n : Positive) return Integer with post=> (a * inv_mod'Result) mod n = 1 is 
    
    
    function inverse (a, b, u, v : Integer) return Integer is
     (if b=0 then u else inverse (b, a mod b, v, u-(v*a)/b));
  begin
    return inverse (a, n, 1, 0);
  end inv_mod;
begin 
  
  Put_Line (inv_mod (42,2017)'img);
  
  Put_Line (inv_mod (42,77)'img);
  exception when others => Put_Line ("The inverse doesn't exist."); 
end modular_inverse;
Task: Kernighans large earthquake problem



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

public class Program
{
    static void Main() {
        foreach (var earthquake in LargeEarthquakes("data.txt", 6))
            Console.WriteLine(string.Join(" ", earthquake));
    }

    static IEnumerable<string[]> LargeEarthquakes(string filename, double limit) =>
        from line in File.ReadLines(filename)
        let parts = line.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries)
        where double.Parse(parts[2]) > limit
        select parts;

}









Ada: 
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
use Ada.Strings;

procedure Main is
   Inpt_File : File_Type;
   Space     : Natural;
begin
   Open (File => Inpt_File, Mode => In_File, Name => "data.txt");
   while not End_Of_File (Inpt_File) loop
      declare
         Line : String :=
           Trim (Source => Get_Line (File => Inpt_File), Side => Both);
      begin

         if Line'Length > 0 then
            Space := Line'Last;
            loop
               exit when Line (Space) = ' ' or else Space = 0;
               Space := Space - 1;
            end loop;

            if Space > 0 then
               if Float'Value (Line (Space .. Line'Last)) > 6.0 then
                  Put_Line (Line);
               end if;
            end if;
         end if;
      end;
   end loop;
   Close (Inpt_File);
end Main;
Task: Kernighans large earthquake problem

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

int main() {
    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    char *lw, *lt;
    fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("Unable to open file\n");
        exit(1);
    }
    printf("Those earthquakes with a magnitude > 6.0 are:\n\n");
    while ((read = getline(&line, &len, fp)) != EOF) {
        if (read < 2) continue;   
        lw = strrchr(line, ' ');  
        lt = strrchr(line, '\t'); 
        if (!lw && !lt) continue; 
        if (lt > lw) lw = lt;     
        if (atof(lw + 1) > 6.0) printf("%s", line);
    }
    fclose(fp);
    if (line) free(line);
    return 0;
}











Ada: 
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
use Ada.Strings;

procedure Main is
   Inpt_File : File_Type;
   Space     : Natural;
begin
   Open (File => Inpt_File, Mode => In_File, Name => "data.txt");
   while not End_Of_File (Inpt_File) loop
      declare
         Line : String :=
           Trim (Source => Get_Line (File => Inpt_File), Side => Both);
      begin

         if Line'Length > 0 then
            Space := Line'Last;
            loop
               exit when Line (Space) = ' ' or else Space = 0;
               Space := Space - 1;
            end loop;

            if Space > 0 then
               if Float'Value (Line (Space .. Line'Last)) > 6.0 then
                  Put_Line (Line);
               end if;
            end if;
         end if;
      end;
   end loop;
   Close (Inpt_File);
end Main;
Task: Kernighans large earthquake problem


C++: 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream file("../include/earthquake.txt");

    int count_quake = 0;
    int column = 1;
    string value;
    double size_quake;
    string row = "";


    while(file >> value)
    {
        if(column == 3)
        {
            size_quake = stod(value);

            if(size_quake>6.0)
            {
                count_quake++;
                row += value + "\t";
                cout << row << endl;
            }

            column = 1;
            row = "";
        }
        else
        {
            column++;
            row+=value + "\t";
        }
    }

    cout << "\nNumber of quakes greater than 6 is " << count_quake << endl;

    return 0;
}










Ada: 
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
use Ada.Strings;

procedure Main is
   Inpt_File : File_Type;
   Space     : Natural;
begin
   Open (File => Inpt_File, Mode => In_File, Name => "data.txt");
   while not End_Of_File (Inpt_File) loop
      declare
         Line : String :=
           Trim (Source => Get_Line (File => Inpt_File), Side => Both);
      begin

         if Line'Length > 0 then
            Space := Line'Last;
            loop
               exit when Line (Space) = ' ' or else Space = 0;
               Space := Space - 1;
            end loop;

            if Space > 0 then
               if Float'Value (Line (Space .. Line'Last)) > 6.0 then
                  Put_Line (Line);
               end if;
            end if;
         end if;
      end;
   end loop;
   Close (Inpt_File);
end Main;
Task: Kernighans large earthquake problem




Go: package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    f, err := os.Open("data.txt")
    if err != nil {
        fmt.Println("Unable to open the file")
        return
    }
    defer f.Close()
    fmt.Println("Those earthquakes with a magnitude > 6.0 are:\n")
    input := bufio.NewScanner(f)
    for input.Scan() {
        line := input.Text()
        fields := strings.Fields(line)
        mag, err := strconv.ParseFloat(fields[2], 64)
        if err != nil {
            fmt.Println("Unable to parse magnitude of an earthquake")
            return
        }
        if mag > 6.0 {
            fmt.Println(line)
        }
    }
}








Ada: 
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
use Ada.Strings;

procedure Main is
   Inpt_File : File_Type;
   Space     : Natural;
begin
   Open (File => Inpt_File, Mode => In_File, Name => "data.txt");
   while not End_Of_File (Inpt_File) loop
      declare
         Line : String :=
           Trim (Source => Get_Line (File => Inpt_File), Side => Both);
      begin

         if Line'Length > 0 then
            Space := Line'Last;
            loop
               exit when Line (Space) = ' ' or else Space = 0;
               Space := Space - 1;
            end loop;

            if Space > 0 then
               if Float'Value (Line (Space .. Line'Last)) > 6.0 then
                  Put_Line (Line);
               end if;
            end if;
         end if;
      end;
   end loop;
   Close (Inpt_File);
end Main;
Task: Kernighans large earthquake problem





Java: import java.io.BufferedReader;
import java.io.FileReader;

public class KernighansLargeEarthquakeProblem {

    public static void main(String[] args) throws Exception {
        try (BufferedReader reader  = new BufferedReader(new FileReader("data.txt")); ) {
            String inLine = null;
            while ( (inLine = reader.readLine()) != null ) {
                String[] split = inLine.split("\\s+");
                double magnitude = Double.parseDouble(split[2]);
                if ( magnitude > 6 ) {
                    System.out.println(inLine);
                }
            }
        }

    }

}







Ada: 
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
use Ada.Strings;

procedure Main is
   Inpt_File : File_Type;
   Space     : Natural;
begin
   Open (File => Inpt_File, Mode => In_File, Name => "data.txt");
   while not End_Of_File (Inpt_File) loop
      declare
         Line : String :=
           Trim (Source => Get_Line (File => Inpt_File), Side => Both);
      begin

         if Line'Length > 0 then
            Space := Line'Last;
            loop
               exit when Line (Space) = ' ' or else Space = 0;
               Space := Space - 1;
            end loop;

            if Space > 0 then
               if Float'Value (Line (Space .. Line'Last)) > 6.0 then
                  Put_Line (Line);
               end if;
            end if;
         end if;
      end;
   end loop;
   Close (Inpt_File);
end Main;
Task: Kernighans large earthquake problem







Python: python -c '
with open("data.txt") as f:
    for ln in f:
        if float(ln.strip().split()[2]) > 6:
            print(ln.strip())'





Ada: 
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
use Ada.Strings;

procedure Main is
   Inpt_File : File_Type;
   Space     : Natural;
begin
   Open (File => Inpt_File, Mode => In_File, Name => "data.txt");
   while not End_Of_File (Inpt_File) loop
      declare
         Line : String :=
           Trim (Source => Get_Line (File => Inpt_File), Side => Both);
      begin

         if Line'Length > 0 then
            Space := Line'Last;
            loop
               exit when Line (Space) = ' ' or else Space = 0;
               Space := Space - 1;
            end loop;

            if Space > 0 then
               if Float'Value (Line (Space .. Line'Last)) > 6.0 then
                  Put_Line (Line);
               end if;
            end if;
         end if;
      end;
   end loop;
   Close (Inpt_File);
end Main;
Task: Kernighans large earthquake problem








VB: Imports System.IO

Module Module1

    Function LargeEarthquakes(filename As String, limit As Double) As IEnumerable(Of String())
        Return From line In File.ReadLines(filename)
               Let parts = line.Split(CType(Nothing, Char()), StringSplitOptions.RemoveEmptyEntries)
               Where Double.Parse(parts(2)) > limit
               Select parts
    End Function

    Sub Main()
        For Each earthquake In LargeEarthquakes("data.txt", 6)
            Console.WriteLine(String.Join(" ", earthquake))
        Next
    End Sub

End Module




Ada: 
with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
use Ada.Strings;

procedure Main is
   Inpt_File : File_Type;
   Space     : Natural;
begin
   Open (File => Inpt_File, Mode => In_File, Name => "data.txt");
   while not End_Of_File (Inpt_File) loop
      declare
         Line : String :=
           Trim (Source => Get_Line (File => Inpt_File), Side => Both);
      begin

         if Line'Length > 0 then
            Space := Line'Last;
            loop
               exit when Line (Space) = ' ' or else Space = 0;
               Space := Space - 1;
            end loop;

            if Space > 0 then
               if Float'Value (Line (Space .. Line'Last)) > 6.0 then
                  Put_Line (Line);
               end if;
            end if;
         end if;
      end;
   end loop;
   Close (Inpt_File);
end Main;
Task: Kernighans large earthquake problem

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

int main() {
    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    char *lw, *lt;
    fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("Unable to open file\n");
        exit(1);
    }
    printf("Those earthquakes with a magnitude > 6.0 are:\n\n");
    while ((read = getline(&line, &len, fp)) != EOF) {
        if (read < 2) continue;   
        lw = strrchr(line, ' ');  
        lt = strrchr(line, '\t'); 
        if (!lw && !lt) continue; 
        if (lt > lw) lw = lt;     
        if (atof(lw + 1) > 6.0) printf("%s", line);
    }
    fclose(fp);
    if (line) free(line);
    return 0;
}
Task: Kernighans large earthquake problem



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

public class Program
{
    static void Main() {
        foreach (var earthquake in LargeEarthquakes("data.txt", 6))
            Console.WriteLine(string.Join(" ", earthquake));
    }

    static IEnumerable<string[]> LargeEarthquakes(string filename, double limit) =>
        from line in File.ReadLines(filename)
        let parts = line.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries)
        where double.Parse(parts[2]) > limit
        select parts;

}
Task: Kernighans large earthquake problem


C++: 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream file("../include/earthquake.txt");

    int count_quake = 0;
    int column = 1;
    string value;
    double size_quake;
    string row = "";


    while(file >> value)
    {
        if(column == 3)
        {
            size_quake = stod(value);

            if(size_quake>6.0)
            {
                count_quake++;
                row += value + "\t";
                cout << row << endl;
            }

            column = 1;
            row = "";
        }
        else
        {
            column++;
            row+=value + "\t";
        }
    }

    cout << "\nNumber of quakes greater than 6 is " << count_quake << endl;

    return 0;
}
Task: Kernighans large earthquake problem





Java: import java.io.BufferedReader;
import java.io.FileReader;

public class KernighansLargeEarthquakeProblem {

    public static void main(String[] args) throws Exception {
        try (BufferedReader reader  = new BufferedReader(new FileReader("data.txt")); ) {
            String inLine = null;
            while ( (inLine = reader.readLine()) != null ) {
                String[] split = inLine.split("\\s+");
                double magnitude = Double.parseDouble(split[2]);
                if ( magnitude > 6 ) {
                    System.out.println(inLine);
                }
            }
        }

    }

}
Task: Kernighans large earthquake problem







Python: python -c '
with open("data.txt") as f:
    for ln in f:
        if float(ln.strip().split()[2]) > 6:
            print(ln.strip())'
Task: Kernighans large earthquake problem








VB: Imports System.IO

Module Module1

    Function LargeEarthquakes(filename As String, limit As Double) As IEnumerable(Of String())
        Return From line In File.ReadLines(filename)
               Let parts = line.Split(CType(Nothing, Char()), StringSplitOptions.RemoveEmptyEntries)
               Where Double.Parse(parts(2)) > limit
               Select parts
    End Function

    Sub Main()
        For Each earthquake In LargeEarthquakes("data.txt", 6)
            Console.WriteLine(String.Join(" ", earthquake))
        Next
    End Sub

End Module
Task: Kernighans large earthquake problem




Go: package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

func main() {
    f, err := os.Open("data.txt")
    if err != nil {
        fmt.Println("Unable to open the file")
        return
    }
    defer f.Close()
    fmt.Println("Those earthquakes with a magnitude > 6.0 are:\n")
    input := bufio.NewScanner(f)
    for input.Scan() {
        line := input.Text()
        fields := strings.Fields(line)
        mag, err := strconv.ParseFloat(fields[2], 64)
        if err != nil {
            fmt.Println("Unable to parse magnitude of an earthquake")
            return
        }
        if mag > 6.0 {
            fmt.Println(line)
        }
    }
}