How to resolve the algorithm Long primes step by step in the Java programming language
How to resolve the algorithm Long primes step by step in the Java 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 Java programming language
The given Java code is designed to find and count a specific type of prime numbers called "long primes" within a specified range. A long prime is a prime number for which the period of its reciprocal is equal to the prime number minus one. In other words, when you repeatedly divide 1 by the prime number and take the remainder, the sequence of remainders will eventually repeat itself after a certain number of steps. For a long prime, this number of steps is equal to the prime number minus one.
Here's a detailed breakdown of the code:
-
sieve
Method (Sieve of Eratosthenes):- This method uses the Sieve of Eratosthenes algorithm to generate a list of prime numbers up to a specified limit.
- It creates a boolean array
c
, where each element corresponds to a number from 0 to the limit. Initially, all elements are set tofalse
. - The algorithm iterates through odd numbers starting from 3 and marks the multiples of each prime as
true
in the array. - The resulting list of primes is stored in a
List
calledprimes
.
-
findPeriod
Method:- This method calculates the period of the reciprocal of a given integer
n
. - It initializes
r
to 1 andperiod
to 0. - It iterates through values from 1 to
n-1
, repeatedly multiplyingr
by 10 and taking the remainder when dividing byn
. - The period is the number of iterations required for
r
to return to its initial value.
- This method calculates the period of the reciprocal of a given integer
-
main
Method:- Initializes an array
numbers
with various limits up to which long primes will be found. - Initializes an array
totals
to store the count of long primes for each limit innumbers
. - Calls the
sieve
method to generate the list of primes up to 64000. - Iterates through the primes and checks if the period of the reciprocal of each prime is equal to the prime minus one. If so, it is considered a long prime and added to the
longPrimes
list. - Counts the number of long primes within each limit in
numbers
and stores the counts in thetotals
array. - Prints the long primes up to the first limit and the counts of long primes for all limits.
- Initializes an array
In summary, this code efficiently generates long primes within a specified range using the Sieve of Eratosthenes algorithm and calculates their periods to identify those that meet the definition of long primes. It then counts and reports the number of long primes found up to different limits.
Source code in the java programming language
import java.util.LinkedList;
import java.util.List;
public class LongPrimes
{
private static void sieve(int limit, List<Integer> primes)
{
boolean[] c = new boolean[limit];
for (int i = 0; i < limit; i++)
c[i] = false;
// No need to process even numbers
int p = 3, n = 0;
int p2 = p * p;
while (p2 <= limit)
{
for (int i = p2; i <= limit; i += 2 * p)
c[i] = true;
do
p += 2;
while (c[p]);
p2 = p * p;
}
for (int i = 3; i <= limit; i += 2)
if (!c[i])
primes.add(i);
}
// Finds the period of the reciprocal of n
private static int findPeriod(int n)
{
int r = 1, period = 0;
for (int i = 1; i < n; i++)
r = (10 * r) % n;
int rr = r;
do
{
r = (10 * r) % n;
++period;
}
while (r != rr);
return period;
}
public static void main(String[] args)
{
int[] numbers = new int[]{500, 1000, 2000, 4000, 8000, 16000, 32000, 64000};
int[] totals = new int[numbers.length];
List<Integer> primes = new LinkedList<Integer>();
List<Integer> longPrimes = new LinkedList<Integer>();
sieve(64000, primes);
for (int prime : primes)
if (findPeriod(prime) == prime - 1)
longPrimes.add(prime);
int count = 0, index = 0;
for (int longPrime : longPrimes)
{
if (longPrime > numbers[index])
totals[index++] = count;
++count;
}
totals[numbers.length - 1] = count;
System.out.println("The long primes up to " + numbers[0] + " are:");
System.out.println(longPrimes.subList(0, totals[0]));
System.out.println();
System.out.println("The number of long primes up to:");
for (int i = 0; i <= 7; i++)
System.out.printf(" %5d is %d\n", numbers[i], totals[i]);
}
}
You may also check:How to resolve the algorithm MD5 step by step in the Suneido programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the Nim programming language
You may also check:How to resolve the algorithm User input/Text step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Empty program step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Objeck programming language