How to resolve the algorithm Cuban primes step by step in the Go programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Cuban primes step by step in the Go programming language
Table of Contents
Problem Statement
The name cuban has nothing to do with Cuba (the country), but has to do with the fact that cubes (3rd powers) play a role in its definition.
Cuban primes were named in 1923 by Allan Joseph Champneys Cunningham.
Note that cuban prime isn't capitalized (as it doesn't refer to the nation of Cuba).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cuban primes step by step in the Go programming language
Overview: This Go program identifies and displays the first 200 prime numbers that differ by one trillion (10^12).
Key Concepts:
- Cuban Primes: A Cuban prime is a prime number that differs by exactly one trillion from another prime number.
- Sieve of Eratosthenes: This program uses a modified version of the Sieve of Eratosthenes to find prime numbers.
- Big Integers: The program uses the
big
package to represent and perform operations on large integers, such as one trillion.
Code Breakdown:
Functions:
commatize(n uint64) string
: This function converts a large integer to a string with commas inserted every three digits for readability.
Main Function:
- Initialization: Declare variables for the current cube (cube1), the next cube (cube2), the difference between them (diff), and an array to store the first 200 Cuban primes (cubans).
- Loop to Find Cuban Primes: The program enters an infinite loop (
for { ... }
) that iterates through potential Cuban primes.- It calculates the next cube (cube2) and computes the difference between the current and next cube (diff).
- It uses the
ProbablyPrime(0)
function to check if diff is prime with 100% accuracy for integers less than2^64
. - If diff is prime and count is less than 200, it stores the comma-separated string representation of diff in the cubans array.
- If count reaches 100000, it stores the 100,000th Cuban prime (cube100k) and exits the loop.
- Display Results:
- The program prints the first 200 Cuban primes, grouped into 10 per line, using the commatize function for readability.
- It also prints the 100,000th Cuban prime with commas inserted.
Source code in the go programming language
package main
import (
"fmt"
"math/big"
)
func commatize(n uint64) string {
s := fmt.Sprintf("%d", n)
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
return s
}
func main() {
var z big.Int
var cube1, cube2, cube100k, diff uint64
cubans := make([]string, 200)
cube1 = 1
count := 0
for i := 1; ; i++ {
j := i + 1
cube2 = uint64(j * j * j)
diff = cube2 - cube1
z.SetUint64(diff)
if z.ProbablyPrime(0) { // 100% accurate for z < 2 ^ 64
if count < 200 {
cubans[count] = commatize(diff)
}
count++
if count == 100000 {
cube100k = diff
break
}
}
cube1 = cube2
}
fmt.Println("The first 200 cuban primes are:-")
for i := 0; i < 20; i++ {
j := i * 10
fmt.Printf("%9s\n", cubans[j : j+10]) // 10 per line say
}
fmt.Println("\nThe 100,000th cuban prime is", commatize(cube100k))
}
You may also check:How to resolve the algorithm Search in paragraph's text step by step in the Julia programming language
You may also check:How to resolve the algorithm Solve a Hopido puzzle step by step in the Wren programming language
You may also check:How to resolve the algorithm Generic swap step by step in the Racket programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the APL programming language
You may also check:How to resolve the algorithm Loops/For step by step in the DWScript programming language