How to resolve the algorithm Long primes step by step in the 11l programming language
How to resolve the algorithm Long primes step by step in the 11l 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 11l programming language
Source code in the 11l programming language
F sieve(limit)
[Int] primes
V c = [0B] * (limit + 1)
V p = 3
L
V p2 = p * p
I p2 > limit
L.break
L(i) (p2 .< limit).step(2 * p)
c[i] = 1B
L
p += 2
I !c[p]
L.break
L(i) (3 .< limit).step(2)
I !(c[i])
primes.append(i)
R primes
F findPeriod(n)
V r = 1
L(i) 1 .< n
r = (10 * r) % n
V rr = r
V period = 0
L
r = (10 * r) % n
period++
I r == rr
L.break
R period
V primes = sieve(64000)
[Int] longPrimes
L(prime) primes
I findPeriod(prime) == prime - 1
longPrimes.append(prime)
V numbers = [500, 1000, 2000, 4000, 8000, 16000, 32000, 64000]
V count = 0
V index = 0
V totals = [0] * numbers.len
L(longPrime) longPrimes
I longPrime > numbers[index]
totals[index] = count
index++
count++
totals.last = count
print(‘The long primes up to 500 are:’)
print(String(longPrimes[0 .< totals[0]]).replace(‘,’, ‘’))
print("\nThe number of long primes up to:")
L(total) totals
print(‘ #5 is #.’.format(numbers[L.index], total))
You may also check:How to resolve the algorithm Rep-string step by step in the Raku programming language
You may also check:How to resolve the algorithm Currency step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Deceptive numbers step by step in the Lua programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Factor programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the PL/I programming language