How to resolve the algorithm Long primes step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Long primes step by step in the Julia programming language

Table of Contents

Problem Statement

A   long prime   (as defined here)   is a prime number whose reciprocal   (in decimal)   has a   period length   of one less than the prime number.

Long primes   are also known as:

Another definition:   primes   p   such that the decimal expansion of   1/p   has period   p-1,   which is the greatest period possible for any integer.

7   is the first long prime,   the reciprocal of seven is   1/7,   which is equal to the repeating decimal fraction   0.142857142857··· The length of the   repeating   part of the decimal fraction is six,   (the underlined part)   which is one less than the (decimal) prime number   7. Thus   7   is a long prime.

There are other (more) general definitions of a   long prime   which include wording/verbiage for bases other than ten.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Long primes step by step in the Julia programming language

The provided Julia code is designed to find and work with "long primes," a specific type of prime numbers that exhibit a particular property. A step-by-step explanation of the code is given below:

  1. The code begins by importing the Primes module, which provides functionality for working with prime numbers in Julia.

  2. divisors(n) function:

    • This function takes a positive integer n as input.
    • It calculates all the divisors of n and returns them as an array.
  3. islongprime(p) function:

    • This function takes a prime number p as input and checks if it is a "long prime."
    • It first finds all the divisors of p-1.
    • Then, for each divisor i, it checks if 10^i % p == 1. If this condition holds for any divisor i, the function returns true, indicating that p is a long prime.
    • Otherwise, it returns false.
  4. The code then iterates over all prime numbers less than or equal to 500 and checks if each prime is a long prime using the islongprime function.

  5. If a prime p is found to be a long prime, it is printed to the console.

  6. Finally, the code calculates and prints the number of long primes for several larger limits, such as 1000, 2000, 4000, and so on, up to 64000.

In summary, this code identifies and counts "long primes" for different limits, where a long prime is a prime number whose previous number (p-1) has a divisor i such that 10^i % p == 1.

Source code in the julia programming language

using Primes

function divisors(n)
    f = [one(n)]
    for (p,e) in factor(n)
        f = reduce(vcat, [f*p^j for j in 1:e], init=f)
    end
    return length(f) == 1 ? [one(n), n] : sort!(f)
end
 
function islongprime(p)
    for i in divisors(p-1)
        if powermod(10, i, p) == 1
            return i + 1 == p
        end
    end
    false
end
 
println("Long primes ≤ 500: ")
for i in 2:500
    if islongprime(i)
        i == 229 ? println(i) : print(i, "  ")
    end
end
print("\n\n")
 
for i in [500, 1000, 2000, 4000, 8000, 16000, 32000, 64000]
    println("Number of long primes ≤ $i: $(sum(map(x->islongprime(x), 1:i)))")
end


  

You may also check:How to resolve the algorithm Monty Hall problem step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm File modification time step by step in the Erlang programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the Sidef programming language
You may also check:How to resolve the algorithm String prepend step by step in the Oforth programming language
You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the Kotlin programming language