WeixiangYan/CodeTransOcean huggingface dataset:
Task: Mersenne primes


C++: #include <iostream>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (n % 2 == 0) return n == 2;
    if (n % 3 == 0) return n == 3;

    uint64_t test = 5;
    while (test * test < n) {
        if (n % test == 0) return false;
        test += 2;
        if (n % test == 0) return false;
        test += 4;
    }

    return true;
}

int main() {
    uint64_t base = 2;

    for (int pow = 1; pow <= 32; pow++) {
        if (isPrime(base - 1)) {
            std::cout << "2 ^ " << pow << " - 1\n";
        }
        base *= 2;
    }

    return 0;
}






Python: import random


def MillerRabinPrimalityTest(number):
    
    if number == 2:
        return True
    elif number == 1 or number % 2 == 0:
        return False
    
    
    
    
    oddPartOfNumber = number - 1
    
    
    timesTwoDividNumber = 0
    
    
    while oddPartOfNumber % 2 == 0:
        oddPartOfNumber = oddPartOfNumber / 2
        timesTwoDividNumber = timesTwoDividNumber + 1 
     
    
    for time in range(3):
        
        
        while True:
            
            randomNumber = random.randint(2, number)-1
            if randomNumber != 0 and randomNumber != 1:
                break
        
        
        randomNumberWithPower = pow(randomNumber, oddPartOfNumber, number)
        
        
        if (randomNumberWithPower != 1) and (randomNumberWithPower != number - 1):
            
            iterationNumber = 1
            
            
            while (iterationNumber <= timesTwoDividNumber - 1) and (randomNumberWithPower != number - 1):
                
                randomNumberWithPower = pow(randomNumberWithPower, 2, number)
                
                
                iterationNumber = iterationNumber + 1
            
            if (randomNumberWithPower != (number - 1)):
                return False
            
    
    return True


MAX = 20
p = 2
count = 0
while True:
    m = (2 << (p - 1)) - 1
    if MillerRabinPrimalityTest(m):
        print "2 ^ {} - 1".format(p)
        count = count + 1
        if count == MAX:
            break
    
    while True:
        p = p + 2 if (p > 2) else 3
        if MillerRabinPrimalityTest(p):
            break
print "done"
Task: Mersenne primes


C++: #include <iostream>

bool isPrime(uint64_t n) {
    if (n < 2) return false;
    if (n % 2 == 0) return n == 2;
    if (n % 3 == 0) return n == 3;

    uint64_t test = 5;
    while (test * test < n) {
        if (n % test == 0) return false;
        test += 2;
        if (n % test == 0) return false;
        test += 4;
    }

    return true;
}

int main() {
    uint64_t base = 2;

    for (int pow = 1; pow <= 32; pow++) {
        if (isPrime(base - 1)) {
            std::cout << "2 ^ " << pow << " - 1\n";
        }
        base *= 2;
    }

    return 0;
}







VB: Imports System.Numerics

Module Module1

    Function Sqrt(x As BigInteger) As BigInteger
        If x < 0 Then
            Throw New ArgumentException("Negative argument.")
        End If
        If x < 2 Then
            Return x
        End If
        Dim y = x / 2
        While y > (x / y)
            y = ((x / y) + y) / 2
        End While
        Return y
    End Function

    Function IsPrime(bi As BigInteger) As Boolean
        If bi < 2 Then
            Return False
        End If
        If bi Mod 2 = 0 Then
            Return bi = 2
        End If
        If bi Mod 3 = 0 Then
            Return bi = 3
        End If
        If bi Mod 5 = 0 Then
            Return bi = 5
        End If
        If bi Mod 7 = 0 Then
            Return bi = 7
        End If
        If bi Mod 11 = 0 Then
            Return bi = 11
        End If
        If bi Mod 13 = 0 Then
            Return bi = 13
        End If
        If bi Mod 17 = 0 Then
            Return bi = 17
        End If
        If bi Mod 19 = 0 Then
            Return bi = 19
        End If

        Dim limit = Sqrt(bi)
        Dim test As BigInteger = 23
        While test < limit
            If bi Mod test = 0 Then
                Return False
            End If
            test += 2
            If bi Mod test = 0 Then
                Return False
            End If
            test += 4
        End While

        Return True
    End Function

    Sub Main()
        Const MAX = 9

        Dim pow = 2
        Dim count = 0

        While True
            If IsPrime(pow) Then
                Dim p = BigInteger.Pow(2, pow) - 1
                If IsPrime(p) Then
                    Console.WriteLine("2 ^ {0} - 1", pow)
                    count += 1
                    If count >= MAX Then
                        Exit While
                    End If
                End If
            End If
            pow += 1
        End While
    End Sub

End Module
Task: Mersenne primes



C#: using System;
using System.Numerics;

namespace MersennePrimes {
    class Program {
        static BigInteger Sqrt(BigInteger x) {
            if (x < 0) throw new ArgumentException("Negative argument.");
            if (x < 2) return x;
            BigInteger y = x / 2;
            while (y > x / y) {
                y = ((x / y) + y) / 2;
            }
            return y;
        }

        static bool IsPrime(BigInteger bi) {
            if (bi < 2) return false;
            if (bi % 2 == 0) return bi == 2;
            if (bi % 3 == 0) return bi == 3;
            if (bi % 5 == 0) return bi == 5;
            if (bi % 7 == 0) return bi == 7;
            if (bi % 11 == 0) return bi == 11;
            if (bi % 13 == 0) return bi == 13;
            if (bi % 17 == 0) return bi == 17;
            if (bi % 19 == 0) return bi == 19;

            BigInteger limit = Sqrt(bi);
            BigInteger test = 23;
            while (test < limit) {
                if (bi % test == 0) return false;
                test += 2;
                if (bi % test == 0) return false;
                test += 4;
            }

            return true;
        }

        static void Main(string[] args) {
            const int MAX = 9;

            int pow = 2;
            int count = 0;

            while (true) {
                if (IsPrime(pow)) {
                    BigInteger p = BigInteger.Pow(2, pow) - 1;
                    if (IsPrime(p)) {
                        Console.WriteLine("2 ^ {0} - 1", pow);
                        if (++count >= MAX) {
                            break;
                        }
                    }
                }
                pow++;
            }
        }
    }
}


Go: package main

import (
	"fmt"
	"time"

	
	"math/big"
	
)

func main() {
	start := time.Now()
	one := big.NewInt(1)
	mp := big.NewInt(0)
	bp := big.NewInt(0)
	const max = 22
	for count, p := 0, uint(2); count < max; {
		mp.Lsh(one, p)
		mp.Sub(mp, one)
		if mp.ProbablyPrime(0) {
			elapsed := time.Since(start).Seconds()
			if elapsed >= 0.01 {
				fmt.Printf("2 ^ %-4d - 1 took %6.2f secs\n", p, elapsed)
			} else {
				fmt.Printf("2 ^ %-4d - 1\n", p)
			}
			count++
		}
		for {
			if p > 2 {
				p += 2
			} else {
				p = 3
			}
			bp.SetUint64(uint64(p))
			if bp.ProbablyPrime(0) {
				break
			}
		}
	}
}
Task: Mersenne primes



C#: using System;
using System.Numerics;

namespace MersennePrimes {
    class Program {
        static BigInteger Sqrt(BigInteger x) {
            if (x < 0) throw new ArgumentException("Negative argument.");
            if (x < 2) return x;
            BigInteger y = x / 2;
            while (y > x / y) {
                y = ((x / y) + y) / 2;
            }
            return y;
        }

        static bool IsPrime(BigInteger bi) {
            if (bi < 2) return false;
            if (bi % 2 == 0) return bi == 2;
            if (bi % 3 == 0) return bi == 3;
            if (bi % 5 == 0) return bi == 5;
            if (bi % 7 == 0) return bi == 7;
            if (bi % 11 == 0) return bi == 11;
            if (bi % 13 == 0) return bi == 13;
            if (bi % 17 == 0) return bi == 17;
            if (bi % 19 == 0) return bi == 19;

            BigInteger limit = Sqrt(bi);
            BigInteger test = 23;
            while (test < limit) {
                if (bi % test == 0) return false;
                test += 2;
                if (bi % test == 0) return false;
                test += 4;
            }

            return true;
        }

        static void Main(string[] args) {
            const int MAX = 9;

            int pow = 2;
            int count = 0;

            while (true) {
                if (IsPrime(pow)) {
                    BigInteger p = BigInteger.Pow(2, pow) - 1;
                    if (IsPrime(p)) {
                        Console.WriteLine("2 ^ {0} - 1", pow);
                        if (++count >= MAX) {
                            break;
                        }
                    }
                }
                pow++;
            }
        }
    }
}



Java: import java.math.BigInteger;

public class MersennePrimes {
    private static final int MAX = 20;

    private static final BigInteger ONE = BigInteger.ONE;
    private static final BigInteger TWO = BigInteger.valueOf(2);

    private static boolean isPrime(int n) {
        if (n < 2) return false;
        if (n % 2 == 0) return n == 2;
        if (n % 3 == 0) return n == 3;
        int d = 5;
        while (d * d <= n) {
            if (n % d == 0) return false;
            d += 2;
            if (n % d == 0) return false;
            d += 4;
        }
        return true;
    }

    public static void main(String[] args) {
        int count = 0;
        int p = 2;
        while (true) {
            BigInteger m = TWO.shiftLeft(p - 1).subtract(ONE);
            if (m.isProbablePrime(10)) {
                System.out.printf("2 ^ %d - 1\n", p);
                if (++count == MAX) break;
            }
            
            do {
                p = (p > 2) ? p + 2 : 3;
            } while (!isPrime(p));
        }
    }
}
Task: Mersenne primes




Go: package main

import (
	"fmt"
	"time"

	
	"math/big"
	
)

func main() {
	start := time.Now()
	one := big.NewInt(1)
	mp := big.NewInt(0)
	bp := big.NewInt(0)
	const max = 22
	for count, p := 0, uint(2); count < max; {
		mp.Lsh(one, p)
		mp.Sub(mp, one)
		if mp.ProbablyPrime(0) {
			elapsed := time.Since(start).Seconds()
			if elapsed >= 0.01 {
				fmt.Printf("2 ^ %-4d - 1 took %6.2f secs\n", p, elapsed)
			} else {
				fmt.Printf("2 ^ %-4d - 1\n", p)
			}
			count++
		}
		for {
			if p > 2 {
				p += 2
			} else {
				p = 3
			}
			bp.SetUint64(uint64(p))
			if bp.ProbablyPrime(0) {
				break
			}
		}
	}
}


Java: import java.math.BigInteger;

public class MersennePrimes {
    private static final int MAX = 20;

    private static final BigInteger ONE = BigInteger.ONE;
    private static final BigInteger TWO = BigInteger.valueOf(2);

    private static boolean isPrime(int n) {
        if (n < 2) return false;
        if (n % 2 == 0) return n == 2;
        if (n % 3 == 0) return n == 3;
        int d = 5;
        while (d * d <= n) {
            if (n % d == 0) return false;
            d += 2;
            if (n % d == 0) return false;
            d += 4;
        }
        return true;
    }

    public static void main(String[] args) {
        int count = 0;
        int p = 2;
        while (true) {
            BigInteger m = TWO.shiftLeft(p - 1).subtract(ONE);
            if (m.isProbablePrime(10)) {
                System.out.printf("2 ^ %d - 1\n", p);
                if (++count == MAX) break;
            }
            
            do {
                p = (p > 2) ? p + 2 : 3;
            } while (!isPrime(p));
        }
    }
}
Task: Mersenne primes





Java: import java.math.BigInteger;

public class MersennePrimes {
    private static final int MAX = 20;

    private static final BigInteger ONE = BigInteger.ONE;
    private static final BigInteger TWO = BigInteger.valueOf(2);

    private static boolean isPrime(int n) {
        if (n < 2) return false;
        if (n % 2 == 0) return n == 2;
        if (n % 3 == 0) return n == 3;
        int d = 5;
        while (d * d <= n) {
            if (n % d == 0) return false;
            d += 2;
            if (n % d == 0) return false;
            d += 4;
        }
        return true;
    }

    public static void main(String[] args) {
        int count = 0;
        int p = 2;
        while (true) {
            BigInteger m = TWO.shiftLeft(p - 1).subtract(ONE);
            if (m.isProbablePrime(10)) {
                System.out.printf("2 ^ %d - 1\n", p);
                if (++count == MAX) break;
            }
            
            do {
                p = (p > 2) ? p + 2 : 3;
            } while (!isPrime(p));
        }
    }
}



Python: import random


def MillerRabinPrimalityTest(number):
    
    if number == 2:
        return True
    elif number == 1 or number % 2 == 0:
        return False
    
    
    
    
    oddPartOfNumber = number - 1
    
    
    timesTwoDividNumber = 0
    
    
    while oddPartOfNumber % 2 == 0:
        oddPartOfNumber = oddPartOfNumber / 2
        timesTwoDividNumber = timesTwoDividNumber + 1 
     
    
    for time in range(3):
        
        
        while True:
            
            randomNumber = random.randint(2, number)-1
            if randomNumber != 0 and randomNumber != 1:
                break
        
        
        randomNumberWithPower = pow(randomNumber, oddPartOfNumber, number)
        
        
        if (randomNumberWithPower != 1) and (randomNumberWithPower != number - 1):
            
            iterationNumber = 1
            
            
            while (iterationNumber <= timesTwoDividNumber - 1) and (randomNumberWithPower != number - 1):
                
                randomNumberWithPower = pow(randomNumberWithPower, 2, number)
                
                
                iterationNumber = iterationNumber + 1
            
            if (randomNumberWithPower != (number - 1)):
                return False
            
    
    return True


MAX = 20
p = 2
count = 0
while True:
    m = (2 << (p - 1)) - 1
    if MillerRabinPrimalityTest(m):
        print "2 ^ {} - 1".format(p)
        count = count + 1
        if count == MAX:
            break
    
    while True:
        p = p + 2 if (p > 2) else 3
        if MillerRabinPrimalityTest(p):
            break
print "done"
Task: Mersenne primes





Java: import java.math.BigInteger;

public class MersennePrimes {
    private static final int MAX = 20;

    private static final BigInteger ONE = BigInteger.ONE;
    private static final BigInteger TWO = BigInteger.valueOf(2);

    private static boolean isPrime(int n) {
        if (n < 2) return false;
        if (n % 2 == 0) return n == 2;
        if (n % 3 == 0) return n == 3;
        int d = 5;
        while (d * d <= n) {
            if (n % d == 0) return false;
            d += 2;
            if (n % d == 0) return false;
            d += 4;
        }
        return true;
    }

    public static void main(String[] args) {
        int count = 0;
        int p = 2;
        while (true) {
            BigInteger m = TWO.shiftLeft(p - 1).subtract(ONE);
            if (m.isProbablePrime(10)) {
                System.out.printf("2 ^ %d - 1\n", p);
                if (++count == MAX) break;
            }
            
            do {
                p = (p > 2) ? p + 2 : 3;
            } while (!isPrime(p));
        }
    }
}




VB: Imports System.Numerics

Module Module1

    Function Sqrt(x As BigInteger) As BigInteger
        If x < 0 Then
            Throw New ArgumentException("Negative argument.")
        End If
        If x < 2 Then
            Return x
        End If
        Dim y = x / 2
        While y > (x / y)
            y = ((x / y) + y) / 2
        End While
        Return y
    End Function

    Function IsPrime(bi As BigInteger) As Boolean
        If bi < 2 Then
            Return False
        End If
        If bi Mod 2 = 0 Then
            Return bi = 2
        End If
        If bi Mod 3 = 0 Then
            Return bi = 3
        End If
        If bi Mod 5 = 0 Then
            Return bi = 5
        End If
        If bi Mod 7 = 0 Then
            Return bi = 7
        End If
        If bi Mod 11 = 0 Then
            Return bi = 11
        End If
        If bi Mod 13 = 0 Then
            Return bi = 13
        End If
        If bi Mod 17 = 0 Then
            Return bi = 17
        End If
        If bi Mod 19 = 0 Then
            Return bi = 19
        End If

        Dim limit = Sqrt(bi)
        Dim test As BigInteger = 23
        While test < limit
            If bi Mod test = 0 Then
                Return False
            End If
            test += 2
            If bi Mod test = 0 Then
                Return False
            End If
            test += 4
        End While

        Return True
    End Function

    Sub Main()
        Const MAX = 9

        Dim pow = 2
        Dim count = 0

        While True
            If IsPrime(pow) Then
                Dim p = BigInteger.Pow(2, pow) - 1
                If IsPrime(p) Then
                    Console.WriteLine("2 ^ {0} - 1", pow)
                    count += 1
                    If count >= MAX Then
                        Exit While
                    End If
                End If
            End If
            pow += 1
        End While
    End Sub

End Module
Task: Mersenne primes




Go: package main

import (
	"fmt"
	"time"

	
	"math/big"
	
)

func main() {
	start := time.Now()
	one := big.NewInt(1)
	mp := big.NewInt(0)
	bp := big.NewInt(0)
	const max = 22
	for count, p := 0, uint(2); count < max; {
		mp.Lsh(one, p)
		mp.Sub(mp, one)
		if mp.ProbablyPrime(0) {
			elapsed := time.Since(start).Seconds()
			if elapsed >= 0.01 {
				fmt.Printf("2 ^ %-4d - 1 took %6.2f secs\n", p, elapsed)
			} else {
				fmt.Printf("2 ^ %-4d - 1\n", p)
			}
			count++
		}
		for {
			if p > 2 {
				p += 2
			} else {
				p = 3
			}
			bp.SetUint64(uint64(p))
			if bp.ProbablyPrime(0) {
				break
			}
		}
	}
}




Python: import random


def MillerRabinPrimalityTest(number):
    
    if number == 2:
        return True
    elif number == 1 or number % 2 == 0:
        return False
    
    
    
    
    oddPartOfNumber = number - 1
    
    
    timesTwoDividNumber = 0
    
    
    while oddPartOfNumber % 2 == 0:
        oddPartOfNumber = oddPartOfNumber / 2
        timesTwoDividNumber = timesTwoDividNumber + 1 
     
    
    for time in range(3):
        
        
        while True:
            
            randomNumber = random.randint(2, number)-1
            if randomNumber != 0 and randomNumber != 1:
                break
        
        
        randomNumberWithPower = pow(randomNumber, oddPartOfNumber, number)
        
        
        if (randomNumberWithPower != 1) and (randomNumberWithPower != number - 1):
            
            iterationNumber = 1
            
            
            while (iterationNumber <= timesTwoDividNumber - 1) and (randomNumberWithPower != number - 1):
                
                randomNumberWithPower = pow(randomNumberWithPower, 2, number)
                
                
                iterationNumber = iterationNumber + 1
            
            if (randomNumberWithPower != (number - 1)):
                return False
            
    
    return True


MAX = 20
p = 2
count = 0
while True:
    m = (2 << (p - 1)) - 1
    if MillerRabinPrimalityTest(m):
        print "2 ^ {} - 1".format(p)
        count = count + 1
        if count == MAX:
            break
    
    while True:
        p = p + 2 if (p > 2) else 3
        if MillerRabinPrimalityTest(p):
            break
print "done"
Task: Mersenne primes




Go: package main

import (
	"fmt"
	"time"

	
	"math/big"
	
)

func main() {
	start := time.Now()
	one := big.NewInt(1)
	mp := big.NewInt(0)
	bp := big.NewInt(0)
	const max = 22
	for count, p := 0, uint(2); count < max; {
		mp.Lsh(one, p)
		mp.Sub(mp, one)
		if mp.ProbablyPrime(0) {
			elapsed := time.Since(start).Seconds()
			if elapsed >= 0.01 {
				fmt.Printf("2 ^ %-4d - 1 took %6.2f secs\n", p, elapsed)
			} else {
				fmt.Printf("2 ^ %-4d - 1\n", p)
			}
			count++
		}
		for {
			if p > 2 {
				p += 2
			} else {
				p = 3
			}
			bp.SetUint64(uint64(p))
			if bp.ProbablyPrime(0) {
				break
			}
		}
	}
}





VB: Imports System.Numerics

Module Module1

    Function Sqrt(x As BigInteger) As BigInteger
        If x < 0 Then
            Throw New ArgumentException("Negative argument.")
        End If
        If x < 2 Then
            Return x
        End If
        Dim y = x / 2
        While y > (x / y)
            y = ((x / y) + y) / 2
        End While
        Return y
    End Function

    Function IsPrime(bi As BigInteger) As Boolean
        If bi < 2 Then
            Return False
        End If
        If bi Mod 2 = 0 Then
            Return bi = 2
        End If
        If bi Mod 3 = 0 Then
            Return bi = 3
        End If
        If bi Mod 5 = 0 Then
            Return bi = 5
        End If
        If bi Mod 7 = 0 Then
            Return bi = 7
        End If
        If bi Mod 11 = 0 Then
            Return bi = 11
        End If
        If bi Mod 13 = 0 Then
            Return bi = 13
        End If
        If bi Mod 17 = 0 Then
            Return bi = 17
        End If
        If bi Mod 19 = 0 Then
            Return bi = 19
        End If

        Dim limit = Sqrt(bi)
        Dim test As BigInteger = 23
        While test < limit
            If bi Mod test = 0 Then
                Return False
            End If
            test += 2
            If bi Mod test = 0 Then
                Return False
            End If
            test += 4
        End While

        Return True
    End Function

    Sub Main()
        Const MAX = 9

        Dim pow = 2
        Dim count = 0

        While True
            If IsPrime(pow) Then
                Dim p = BigInteger.Pow(2, pow) - 1
                If IsPrime(p) Then
                    Console.WriteLine("2 ^ {0} - 1", pow)
                    count += 1
                    If count >= MAX Then
                        Exit While
                    End If
                End If
            End If
            pow += 1
        End While
    End Sub

End Module