WeixiangYan/CodeTransOcean huggingface dataset:
Task: Wieferich primes





Java: import java.util.*;

public class WieferichPrimes {
    public static void main(String[] args) {
        final int limit = 5000;
        System.out.printf("Wieferich primes less than %d:\n", limit);
        for (Integer p : wieferichPrimes(limit))
            System.out.println(p);
    }    

    private static boolean[] primeSieve(int limit) {
        boolean[] sieve = new boolean[limit];
        Arrays.fill(sieve, true);
        if (limit > 0)
            sieve[0] = false;
        if (limit > 1)
            sieve[1] = false;
        for (int i = 4; i < limit; i += 2)
            sieve[i] = false;
        for (int p = 3; ; p += 2) {
            int q = p * p;
            if (q >= limit)
                break;
            if (sieve[p]) {
                int inc = 2 * p;
                for (; q < limit; q += inc)
                    sieve[q] = false;
            }
        }
        return sieve;
    }

    private static long modpow(long base, long exp, long mod) {
        if (mod == 1)
            return 0;
        long result = 1;
        base %= mod;
        for (; exp > 0; exp >>= 1) {
            if ((exp & 1) == 1)
                result = (result * base) % mod;
            base = (base * base) % mod;
        }
        return result;
    }

    private static List<Integer> wieferichPrimes(int limit) {
        boolean[] sieve = primeSieve(limit);
        List<Integer> result = new ArrayList<>();
        for (int p = 2; p < limit; ++p) {
            if (sieve[p] && modpow(2, p - 1, p * p) == 1)
                result.add(p);
        }
        return result;
    }
}







Ada: with Ada.Text_IO;

procedure Wieferich_Primes is

   function Is_Prime (V : Positive) return Boolean is
      D : Positive := 5;
   begin
      if V < 2       then return False; end if;
      if V mod 2 = 0 then return V = 2; end if;
      if V mod 3 = 0 then return V = 3; end if;
      while D * D <= V loop
         if V mod D = 0 then
            return False;
         end if;
         D := D + 2;
      end loop;
      return True;
   end Is_Prime;

   function Is_Wieferich (N : Positive) return Boolean is
      Q : Natural := 1;
   begin
      if not Is_Prime (N) then
         return False;
      end if;
      for P in 2 .. N loop
         Q := (2 * Q) mod N**2;
      end loop;
      return Q = 1;
   end Is_Wieferich;

begin

   Ada.Text_IO.Put_Line ("Wieferich primes below 5000:");
   for N in 1 .. 4999 loop
      if Is_Wieferich (N) then
         Ada.Text_IO.Put_Line (N'Image);
      end if;
   end loop;

end Wieferich_Primes;
Task: Wieferich primes







Python: 

def isPrime(n):
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False        
    return True

def isWeiferich(p):
    if not isPrime(p):
        return False
    q = 1
    p2 = p ** 2
    while p > 1:
        q = (2 * q) % p2
        p -= 1
    if q == 1:
        return True
    else:
        return False


if __name__ == '__main__':
    print("Wieferich primes less than 5000: ")
    for i in range(2, 5001):
        if isWeiferich(i):
            print(i)





Ada: with Ada.Text_IO;

procedure Wieferich_Primes is

   function Is_Prime (V : Positive) return Boolean is
      D : Positive := 5;
   begin
      if V < 2       then return False; end if;
      if V mod 2 = 0 then return V = 2; end if;
      if V mod 3 = 0 then return V = 3; end if;
      while D * D <= V loop
         if V mod D = 0 then
            return False;
         end if;
         D := D + 2;
      end loop;
      return True;
   end Is_Prime;

   function Is_Wieferich (N : Positive) return Boolean is
      Q : Natural := 1;
   begin
      if not Is_Prime (N) then
         return False;
      end if;
      for P in 2 .. N loop
         Q := (2 * Q) mod N**2;
      end loop;
      return Q = 1;
   end Is_Wieferich;

begin

   Ada.Text_IO.Put_Line ("Wieferich primes below 5000:");
   for N in 1 .. 4999 loop
      if Is_Wieferich (N) then
         Ada.Text_IO.Put_Line (N'Image);
      end if;
   end loop;

end Wieferich_Primes;
Task: Wieferich primes

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

#define LIMIT 5000
static bool PRIMES[LIMIT];

static void prime_sieve() {
    uint64_t p;
    int i;

    PRIMES[0] = false;
    PRIMES[1] = false;
    for (i = 2; i < LIMIT; i++) {
        PRIMES[i] = true;
    }

    for (i = 4; i < LIMIT; i += 2) {
        PRIMES[i] = false;
    }

    for (p = 3;; p += 2) {
        uint64_t q = p * p;
        if (q >= LIMIT) {
            break;
        }
        if (PRIMES[p]) {
            uint64_t inc = 2 * p;
            for (; q < LIMIT; q += inc) {
                PRIMES[q] = false;
            }
        }
    }
}

uint64_t modpow(uint64_t base, uint64_t exp, uint64_t mod) {
    uint64_t result = 1;

    if (mod == 1) {
        return 0;
    }

    base %= mod;
    for (; exp > 0; exp >>= 1) {
        if ((exp & 1) == 1) {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
    }
    return result;
}

void wieferich_primes() {
    uint64_t p;

    for (p = 2; p < LIMIT; ++p) {
        if (PRIMES[p] && modpow(2, p - 1, p * p) == 1) {
            printf("%lld\n", p);
        }
    }
}

int main() {
    prime_sieve();

    printf("Wieferich primes less than %d:\n", LIMIT);
    wieferich_primes();

    return 0;
}
Task: Wieferich primes



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

namespace WieferichPrimes {
    class Program {
        static long ModPow(long @base, long exp, long mod) {
            if (mod == 1) {
                return 0;
            }

            long result = 1;
            @base %= mod;
            for (; exp > 0; exp >>= 1) {
                if ((exp & 1) == 1) {
                    result = (result * @base) % mod;
                }
                @base = (@base * @base) % mod;
            }
            return result;
        }

        static bool[] PrimeSieve(int limit) {
            bool[] sieve = Enumerable.Repeat(true, limit).ToArray();

            if (limit > 0) {
                sieve[0] = false;
            }
            if (limit > 1) {
                sieve[1] = false;
            }

            for (int i = 4; i < limit; i += 2) {
                sieve[i] = false;
            }

            for (int p = 3; ; p += 2) {
                int q = p * p;
                if (q >= limit) {
                    break;
                }
                if (sieve[p]) {
                    int inc = 2 * p;
                    for (; q < limit; q += inc) {
                        sieve[q] = false;
                    }
                }
            }

            return sieve;
        }

        static List<int> WiefreichPrimes(int limit) {
            bool[] sieve = PrimeSieve(limit);
            List<int> result = new List<int>();
            for (int p = 2; p < limit; p++) {
                if (sieve[p] && ModPow(2, p - 1, p * p) == 1) {
                    result.Add(p);
                }
            }
            return result;
        }

        static void Main() {
            const int limit = 5000;
            Console.WriteLine("Wieferich primes less that {0}:", limit);
            foreach (int p in WiefreichPrimes(limit)) {
                Console.WriteLine(p);
            }
        }
    }
}
Task: Wieferich primes


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

std::vector<bool> prime_sieve(uint64_t limit) {
    std::vector<bool> sieve(limit, true);
    if (limit > 0)
        sieve[0] = false;
    if (limit > 1)
        sieve[1] = false;
    for (uint64_t i = 4; i < limit; i += 2)
        sieve[i] = false;
    for (uint64_t p = 3; ; p += 2) {
        uint64_t q = p * p;
        if (q >= limit)
            break;
        if (sieve[p]) {
            uint64_t inc = 2 * p;
            for (; q < limit; q += inc)
                sieve[q] = false;
        }
    }
    return sieve;
}

uint64_t modpow(uint64_t base, uint64_t exp, uint64_t mod) {
    if (mod == 1)
        return 0;
    uint64_t result = 1;
    base %= mod;
    for (; exp > 0; exp >>= 1) {
        if ((exp & 1) == 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
    }
    return result;
}

std::vector<uint64_t> wieferich_primes(uint64_t limit) {
    std::vector<uint64_t> result;
    std::vector<bool> sieve(prime_sieve(limit));
    for (uint64_t p = 2; p < limit; ++p)
        if (sieve[p] && modpow(2, p - 1, p * p) == 1)
            result.push_back(p);
    return result;
}

int main() {
    const uint64_t limit = 5000;
    std::cout << "Wieferich primes less than " << limit << ":\n";
    for (uint64_t p : wieferich_primes(limit))
        std::cout << p << '\n';
}
Task: Wieferich primes





Java: import java.util.*;

public class WieferichPrimes {
    public static void main(String[] args) {
        final int limit = 5000;
        System.out.printf("Wieferich primes less than %d:\n", limit);
        for (Integer p : wieferichPrimes(limit))
            System.out.println(p);
    }    

    private static boolean[] primeSieve(int limit) {
        boolean[] sieve = new boolean[limit];
        Arrays.fill(sieve, true);
        if (limit > 0)
            sieve[0] = false;
        if (limit > 1)
            sieve[1] = false;
        for (int i = 4; i < limit; i += 2)
            sieve[i] = false;
        for (int p = 3; ; p += 2) {
            int q = p * p;
            if (q >= limit)
                break;
            if (sieve[p]) {
                int inc = 2 * p;
                for (; q < limit; q += inc)
                    sieve[q] = false;
            }
        }
        return sieve;
    }

    private static long modpow(long base, long exp, long mod) {
        if (mod == 1)
            return 0;
        long result = 1;
        base %= mod;
        for (; exp > 0; exp >>= 1) {
            if ((exp & 1) == 1)
                result = (result * base) % mod;
            base = (base * base) % mod;
        }
        return result;
    }

    private static List<Integer> wieferichPrimes(int limit) {
        boolean[] sieve = primeSieve(limit);
        List<Integer> result = new ArrayList<>();
        for (int p = 2; p < limit; ++p) {
            if (sieve[p] && modpow(2, p - 1, p * p) == 1)
                result.add(p);
        }
        return result;
    }
}
Task: Wieferich primes







Python: 

def isPrime(n):
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False        
    return True

def isWeiferich(p):
    if not isPrime(p):
        return False
    q = 1
    p2 = p ** 2
    while p > 1:
        q = (2 * q) % p2
        p -= 1
    if q == 1:
        return True
    else:
        return False


if __name__ == '__main__':
    print("Wieferich primes less than 5000: ")
    for i in range(2, 5001):
        if isWeiferich(i):
            print(i)
Task: Wieferich primes




Go: package main

import (
    "fmt"
    "math/big"
    "rcu"
)

func main() {
    primes := rcu.Primes(5000)
    zero := new(big.Int)
    one := big.NewInt(1)
    num := new(big.Int)
    fmt.Println("Wieferich primes < 5,000:")
    for _, p := range primes {
        num.Set(one)
        num.Lsh(num, uint(p-1))
        num.Sub(num, one)
        den := big.NewInt(int64(p * p))
        if num.Rem(num, den).Cmp(zero) == 0 {
            fmt.Println(rcu.Commatize(p))
        }
    }
}
Task: AKS test for primes







Python: def expand_x_1(n): 

    c =1
    for i in range(n//2+1):
        c = c*(n-i)//(i+1)
        yield c
        
def aks(p):
    if p==2:
        return True

    for i in expand_x_1(p):
        if i % p:

            return False
    return True





Ada: with Ada.Text_IO;

procedure Test_For_Primes is

   type Pascal_Triangle_Type is array (Natural range <>) of Long_Long_Integer;

   function Calculate_Pascal_Triangle (N : in Natural) return Pascal_Triangle_Type is
      Pascal_Triangle : Pascal_Triangle_Type (0 .. N);
   begin
      Pascal_Triangle (0) := 1;
      for I in Pascal_Triangle'First .. Pascal_Triangle'Last - 1 loop
         Pascal_Triangle (1 + I) := 1;
         for J in reverse 1 .. I loop
            Pascal_Triangle (J) := Pascal_Triangle (J - 1) - Pascal_Triangle (J);
         end loop;
         Pascal_Triangle (0) := -Pascal_Triangle (0);
      end loop;
      return Pascal_Triangle;
   end Calculate_Pascal_Triangle;

   function Is_Prime (N : Integer) return Boolean is
      I      : Integer;
      Result : Boolean := True;
      Pascal_Triangle : constant Pascal_Triangle_Type := Calculate_Pascal_Triangle (N);
   begin
      I := N / 2;
      while Result and I > 1 loop
         Result := Result and Pascal_Triangle (I) mod Long_Long_Integer (N) = 0;
         I := I - 1;
      end loop;
      return Result;
   end Is_Prime;

   function Image (N    : in Long_Long_Integer;
                   Sign : in Boolean := False) return String is
      Image : constant String := N'Image;
   begin
      if N < 0 then
         return Image;
      else
         if Sign then
            return "+" & Image (Image'First + 1 .. Image'Last);
         else
            return Image (Image'First + 1 .. Image'Last);
         end if;
      end if;
   end Image;

   procedure Show (Triangle : in Pascal_Triangle_Type) is
      use Ada.Text_IO;
   Begin
      for I in reverse Triangle'Range loop
         Put (Image (Triangle (I), Sign => True));
         Put ("x^");
         Put (Image (Long_Long_Integer (I)));
         Put (" ");
      end loop;
   end Show;

   procedure Show_Pascal_Triangles is
      use Ada.Text_IO;
   begin
      for N in 0 .. 9 loop
         declare
            Pascal_Triangle : constant Pascal_Triangle_Type := Calculate_Pascal_Triangle (N);
         begin
            Put ("(x-1)^" & Image (Long_Long_Integer (N)) & " = ");
            Show (Pascal_Triangle);
            New_Line;
         end;
      end loop;
   end Show_Pascal_Triangles;

   procedure Show_Primes is
      use Ada.Text_IO;
   begin
      for N in 2 .. 63 loop
         if Is_Prime (N) then
            Put (N'Image);
         end if;
      end loop;
      New_Line;
   end Show_Primes;

begin
   Show_Pascal_Triangles;
   Show_Primes;
end Test_For_Primes;
Task: AKS test for primes

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

long long c[100];

void coef(int n)
{
	int i, j;

	if (n < 0 || n > 63) abort(); 

	for (c[i=0] = 1; i < n; c[0] = -c[0], i++)
		for (c[1 + (j=i)] = 1; j > 0; j--)
			c[j] = c[j-1] - c[j];
}

int is_prime(int n)
{
	int i;

	coef(n);
	c[0] += 1, c[i=n] -= 1;
	while (i-- && !(c[i] % n));

	return i < 0;
}

void show(int n)
{
	do printf("%+lldx^%d", c[n], n); while (n--);
}

int main(void)
{
	int n;

	for (n = 0; n < 10; n++) {
		coef(n);
		printf("(x-1)^%d = ", n);
		show(n);
		putchar('\n');
	}

	printf("\nprimes (never mind the 1):");
	for (n = 1; n <= 63; n++)
		if (is_prime(n))
			printf(" %d", n);

	putchar('\n');
	return 0;
}
Task: AKS test for primes



C#: using System;
    public class AksTest
    {
        static long[] c = new long[100];

        static void Main(string[] args)
        {
        for (int n = 0; n < 10; n++) {
		coef(n);
		Console.Write("(x-1)^" + n + " = ");
		show(n);
		Console.WriteLine("");
	}	 
	   Console.Write("Primes:");
	  for (int n = 1; n <= 63; n++)
	     if (is_prime(n))
	       Console.Write(n + " ");
	 
	    Console.WriteLine('\n');
            Console.ReadLine();
        }

        static void coef(int n)
        {
            int i, j;

            if (n < 0 || n > 63) System.Environment.Exit(0);

            for (c[i = 0] = 1L; i < n; c[0] = -c[0], i++)
                for (c[1 + (j = i)] = 1L; j > 0; j--)
                    c[j] = c[j - 1] - c[j];
        }

        static bool is_prime(int n)
        {
            int i;

            coef(n);
            c[0] += 1;
            c[i = n] -= 1;

            while (i-- != 0 && (c[i] % n) == 0) ;

            return i < 0;
        }

        static void show(int n)
	    {
		    do {
                Console.Write("+" + c[n] + "x^" + n);
		    }while (n-- != 0);
	    }
    }
Task: AKS test for primes


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

const int pasTriMax = 61;

uint64_t pasTri[pasTriMax + 1];

void pascalTriangle(unsigned long n)

{
    unsigned long j, k;

    pasTri[0] = 1;
    j = 1;
    while (j <= n)
    {
        j++;
        k = j / 2;
        pasTri[k] = pasTri[k - 1];
        for ( ;k >= 1; k--)
            pasTri[k] += pasTri[k - 1];
    }
}

bool isPrime(unsigned long n)
{
    if (n > pasTriMax)
    {
        cout << n << " is out of range" << endl;
        exit(1);
    }
    
    pascalTriangle(n);
    bool res = true;
    int i = n / 2;
    while (res && (i > 1))
    {
        res = res && (pasTri[i] % n == 0);
        i--;
    }
    return res;
}

void expandPoly(unsigned long n)
{
    const char vz[] = {'+', '-'};    
    
    if (n > pasTriMax)
    {
        cout << n << " is out of range" << endl;
        exit(1);
    }
    
    switch (n)
    {
        case 0:
            cout << "(x-1)^0 = 1" << endl;
            break;
        case 1:
            cout << "(x-1)^1 = x-1" << endl;
            break;
        default:
            pascalTriangle(n);
            cout << "(x-1)^" << n << " = ";
            cout << "x^" << n;
            bool bVz = true;
            int nDiv2 = n / 2;
            for (unsigned long j = n - 1; j > nDiv2; j--, bVz = !bVz)
                cout << vz[bVz] << pasTri[n - j] << "*x^" << j;
            for (unsigned long j = nDiv2; j > 1; j--, bVz = !bVz)
                cout << vz[bVz] << pasTri[j] << "*x^" << j;
            cout << vz[bVz] << pasTri[1] << "*x";
            bVz = !bVz;
            cout << vz[bVz] << pasTri[0] << endl;
            break;
    }
}

int main()
{
    for (unsigned long n = 0; n <= 9; n++)
        expandPoly(n);      
    for (unsigned long n = 2; n <= pasTriMax; n++)
        if (isPrime(n))
            cout << setw(3) << n;
    cout << endl;        
}
Task: AKS test for primes





Java: public class AksTest {
    private static final long[] c = new long[64];

    public static void main(String[] args) {
        for (int n = 0; n < 10; n++) {
            coeff(n);
            show(n);
        }

        System.out.print("Primes:");
        for (int n = 1; n < c.length; n++)
            if (isPrime(n))
                System.out.printf(" %d", n);

        System.out.println();
    }

    static void coeff(int n) {
        c[0] = 1;
        for (int i = 0; i < n; c[0] = -c[0], i++) {
            c[1 + i] = 1;
            for (int j = i; j > 0; j--)
                c[j] = c[j - 1] - c[j];
        }
    }

    static boolean isPrime(int n) {
        coeff(n);
        c[0]++;
        c[n]--;

        int i = n;
        while (i-- != 0 && c[i] % n == 0)
            continue;
        return i < 0;
    }

    static void show(int n) {
        System.out.print("(x-1)^" + n + " =");
        for (int i = n; i >= 0; i--) {
            System.out.print(" + " + c[i] + "x^" + i);
        }
        System.out.println();
    }
}
Task: AKS test for primes







Python: def expand_x_1(n): 

    c =1
    for i in range(n//2+1):
        c = c*(n-i)//(i+1)
        yield c
        
def aks(p):
    if p==2:
        return True

    for i in expand_x_1(p):
        if i % p:

            return False
    return True
Task: AKS test for primes








VB: 



Const MAX = 99
Dim c(MAX + 1) As Variant
Private Sub coef(n As Integer)

    c(n) = CDec(1) 
    For i = n - 1 To 2 Step -1
        c(i) = c(i) + c(i - 1)
    Next i
End Sub
Private Function is_prime(fn As Variant) As Boolean
    fn = CDec(fn)
    Call coef(fn + 1)   
    For i = 2 To fn - 1 
        If c(i) - fn * Int(c(i) / fn) <> 0 Then 
            is_prime = False: Exit Function
        End If
    Next i
    is_prime = True
End Function
Private Sub show(n As Integer)

    Dim ci As Variant
    For i = n To 1 Step -1
        ci = c(i)
        If ci = 1 Then
            If (n - i) Mod 2 = 0 Then
                If i = 1 Then
                    If n = 1 Then
                        ci = "1"
                    Else
                        ci = "+1"
                    End If
                Else
                    ci = ""
                End If
            Else
                ci = "-1"
            End If
        Else
            If (n - i) Mod 2 = 0 Then
                ci = "+" & ci
            Else
                ci = "-" & ci
            End If
        End If
        If i = 1 Then 
            Debug.Print ci
        Else
            If i = 2 Then 
                Debug.Print ci & "x";
            Else
                Debug.Print ci & "x^" & i - 1;
            End If
        End If
    Next i
End Sub
Public Sub AKS_test_for_primes()
    Dim n As Integer
    For n = 1 To 10 
        coef n
        Debug.Print "(x-1)^" & n - 1 & " = ";
        show n
    Next n
    Debug.Print "primes (<="; MAX; "):"
    coef 2 
    For n = 2 To MAX
        If is_prime(n) Then
            Debug.Print n;
        End If
    Next n
End Sub
Task: AKS test for primes




Go: package main

import "fmt"

func bc(p int) []int64 {
    c := make([]int64, p+1)
    r := int64(1)
    for i, half := 0, p/2; i <= half; i++ {
        c[i] = r
        c[p-i] = r
        r = r * int64(p-i) / int64(i+1)
    }
    for i := p - 1; i >= 0; i -= 2 {
        c[i] = -c[i]
    }
    return c
}

func main() {
    for p := 0; p <= 7; p++ {
        fmt.Printf("%d:  %s\n", p, pp(bc(p)))
    }
    for p := 2; p < 50; p++ {
        if aks(p) {
            fmt.Print(p, " ")
        }
    }
    fmt.Println()
}

var e = []rune("²³⁴⁵⁶⁷")

func pp(c []int64) (s string) {
    if len(c) == 1 {
        return fmt.Sprint(c[0])
    }
    p := len(c) - 1
    if c[p] != 1 {
        s = fmt.Sprint(c[p])
    }
    for i := p; i > 0; i-- {
        s += "x"
        if i != 1 {
            s += string(e[i-2])
        }
        if d := c[i-1]; d < 0 {
            s += fmt.Sprintf(" - %d", -d)
        } else {
            s += fmt.Sprintf(" + %d", d)
        }
    }
    return
}

func aks(p int) bool {
    c := bc(p)
    c[p]--
    c[0]++
    for _, d := range c {
        if d%int64(p) != 0 {
            return false
        }
    }
    return true
}