How to resolve the algorithm Wieferich primes step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Wieferich primes step by step in the Go programming language

Table of Contents

Problem Statement

In number theory, a Wieferich prime is a prime number p such that p2 evenly divides 2(p − 1) − 1 .

It is conjectured that there are infinitely many Wieferich primes, but as of March 2021,only two have been identified.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Wieferich primes step by step in the Go programming language

This code determines the Wieferich primes less than 5,000.

Wieferich Primes Wieferich primes are prime numbers (p) for which (2^{p-1}\equiv 1\pmod{p^2}). Another way to express this is that (p) divides (2^{p-1}-1).

Code Overview The code consists of three main parts:

  1. Generating Primes:

    • The rcu.Primes(5000) function generates a slice of prime numbers less than 5,000.
  2. Checking Wieferich Condition:

    • For each prime (p) in the slice:
      • Compute (2^{p-1} - 1) using the big.Int package.
      • Check if (p) divides the result by using num.Rem(num, den).Cmp(zero) == 0. If it does, it means (p) is a Wieferich prime.
  3. Printing Results:

    • Iterate over the list of Wieferich primes and print each one using the rcu.Commatize function for readability.

Code Details

  • big.Int Package: The program uses the big.Int package to handle large integer calculations.
  • Commatize Function: The rcu.Commatize function is a helper function to add commas for readability to the prime numbers.
  • fmt Package: The fmt package is used for input and output.

Source code in the go programming language

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))
        }
    }
}


  

You may also check:How to resolve the algorithm Dragon curve step by step in the Metafont programming language
You may also check:How to resolve the algorithm Mutex step by step in the Haskell programming language
You may also check:How to resolve the algorithm Even or odd step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the VBA programming language
You may also check:How to resolve the algorithm UTF-8 encode and decode step by step in the Python programming language