How to resolve the algorithm Fortunate numbers step by step in the Java programming language
How to resolve the algorithm Fortunate numbers step by step in the Java programming language
Table of Contents
Problem Statement
A Fortunate number is the smallest integer m > 1 such that for a given positive integer n, primorial(n) + m is a prime number, where primorial(n) is the product of the first n prime numbers. For example the first fortunate number is 3 because primorial(1) is 2 and 2 + 3 = 5 which is prime whereas 2 + 2 = 4 is composite.
After sorting and removal of any duplicates, compute and show on this page the first 8 Fortunate numbers or, if your language supports big integers, the first 50 Fortunate numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fortunate numbers step by step in the Java programming language
The Java program is designed to identify and display the first 50 fortunate numbers. Fortunate numbers are integers that, when added to the primorial of the preceding prime number, result in a probable prime. The program generates and stores the fortunate numbers in a NavigableSet
.
-
Prime Sieve (
primeSieve
** method):**- Utilizes a
BitSet
to represent a sieve of prime numbers up to a specified number (aNumber
). - Begins with all bits set to
true
, indicating potential primes. - Iterates through potential primes from 2 to the square root of
aNumber
. - For each prime found, it marks its multiples as
false
in the sieve, eliminating them as potential primes. - Returns the resulting
BitSet
with prime numbers marked astrue
.
- Utilizes a
-
Fortunate Number Generation:
- The
main
method initializes aNavigableSet
(fortunates
) for storing fortunate numbers. - The program initializes a
BigInteger
variableprimorial
to 1, representing the product of all prime numbers so far. - In a loop, it iterates through prime numbers:
- Calculates a new
primorial
by multiplying it with the current prime. - Finds the next odd integer (
candidate)
by adding 2 and keeps incrementing it until adding it toprimorial
results in a probable prime. - Adds the candidate to the
fortunates
set.
- Calculates a new
- The
-
Output:
- In the main method, the program prints the first 50 distinct fortunate numbers to the console, formatted with a width of 4 characters and a newline every 10th number.
-
Additional Constants and Methods:
- CERTAINTY_LEVEL: Used as a parameter for
BigInteger.isProbablePrime
to define the confidence level in finding probable primes.
- CERTAINTY_LEVEL: Used as a parameter for
Source code in the java programming language
import java.math.BigInteger;
import java.util.BitSet;
import java.util.NavigableSet;
import java.util.TreeSet;
public final class FortunateNumbers {
public static void main(String[] aArgs) {
BitSet primes = primeSieve(400);
NavigableSet<Integer> fortunates = new TreeSet<Integer>();
BigInteger primorial = BigInteger.ONE;
for ( int prime = 2; prime >= 0; prime = primes.nextSetBit(prime + 1) ) {
primorial = primorial.multiply(BigInteger.valueOf(prime));
int candidate = 3;
while ( ! primorial.add(BigInteger.valueOf(candidate)).isProbablePrime(CERTAINTY_LEVEL) ) {
candidate += 2;
}
fortunates.add(candidate);
}
System.out.println("The first 50 distinct fortunate numbers are:");
for ( int i = 0; i < 50; i++ ) {
System.out.print(String.format("%4d%s", fortunates.pollFirst(), ( i % 10 == 9 ? "\n" : "" )));
}
}
private static BitSet primeSieve(int aNumber) {
BitSet sieve = new BitSet(aNumber + 1);
sieve.set(2, aNumber + 1);
for ( int i = 2; i <= Math.sqrt(aNumber); i = sieve.nextSetBit(i + 1) ) {
for ( int j = i * i; j <= aNumber; j = j + i ) {
sieve.clear(j);
}
}
return sieve;
}
private static final int CERTAINTY_LEVEL = 10;
}
You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the SAS programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Sidef programming language
You may also check:How to resolve the algorithm Strip block comments step by step in the Haskell programming language
You may also check:How to resolve the algorithm Call a function in a shared library step by step in the Maple programming language