How to resolve the algorithm Long primes step by step in the Sidef programming language
How to resolve the algorithm Long primes step by step in the Sidef 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 Sidef programming language
Source code in the sidef programming language
func is_long_prime(p) {
for d in (divisors(p-1)) {
if (powmod(10, d, p) == 1) {
return (d+1 == p)
}
}
return false
}
say "Long primes ≤ 500:"
say primes(500).grep(is_long_prime).join(' ')
for n in ([500, 1000, 2000, 4000, 8000, 16000, 32000, 64000]) {
say ("Number of long primes ≤ #{n}: ", primes(n).count_by(is_long_prime))
}
func is_long_prime(p) {
znorder(10, p) == p-1
}
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Character codes step by step in the REXX programming language
You may also check:How to resolve the algorithm String comparison step by step in the R programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the Rust programming language