How to resolve the algorithm Ormiston triples step by step in the Java programming language
How to resolve the algorithm Ormiston triples step by step in the Java programming language
Table of Contents
Problem Statement
An Ormiston triple is three consecutive prime numbers which are anagrams, i.e. contain the same decimal digits but in a different order. The three consecutive primes (11117123, 11117213, 11117321) are an Ormiston triple.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ormiston triples step by step in the Java programming language
This code calculates and prints the Ormiston triples, which are sets of three prime numbers that have the same digits when written in decimal notation and differ by 18. It also prints the count of such triples for different ranges of prime numbers. Here's a detailed explanation of the code:
-
Main Method:
- Defines the limit for the range of prime numbers to consider, which is 10,000,000,000 in this case.
- Initializes a segmented prime iterator to generate prime numbers.
- Initializes a list to store the Ormiston triples found.
- Sets the initial lower limit to one-tenth of the total limit, and initializes a count for the Ormiston triples.
- Initializes lists to store the count of Ormiston triples for different ranges.
- Initializes variables p1, p2, and p3 to keep track of consecutive prime numbers.
-
Prime Generation:
- The SegmentedPrimeIterator class is used to generate prime numbers efficiently. It uses a combination of the segmented sieve of Eratosthenes and a small sieve to generate large prime numbers.
-
Ormiston Triple Finding:
- The main loop iterates through prime numbers generated by the segmented prime iterator.
- It checks if the differences between consecutive prime numbers are both multiples of 18.
- It checks if the first two prime numbers have the same set of digits when written in decimal notation using the haveSameDigits() method.
- It checks if the second and third prime numbers have the same set of digits when written in decimal notation.
- If the above conditions are met, it increments the count of Ormiston triples and adds the first prime number to the list of Ormiston triples.
- It updates the lower limit and stores the count of Ormiston triples in the count list when the first prime number reaches a multiple of the current lower limit.
-
Printing Results:
- After the main loop finishes, it prints the first 25 Ormiston triples.
- It prints the count of Ormiston triples for different ranges of prime numbers, with each range being ten times larger than the previous one.
-
Supporting Methods:
- haveSameDigits() method: Compares the set of digits in two numbers and returns true if they have the same digits.
- digits() method: Converts a number into a sorted list of its digits.
In summary, this code finds Ormiston triples efficiently using a combination of segmented sieve and digit comparison techniques, and it provides insights into the distribution of such triples within a given range of prime numbers.
Source code in the java programming language
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public final class OrmistonTriples {
public static void main(String[] aArgs) {
final long limit = 10_000_000_000L;
SegmentedPrimeIterator primes = new SegmentedPrimeIterator(limit);
List<Long> ormistons = new ArrayList<Long>();
long lowerLimit = limit / 10;
int count = 0;
List<Integer> counts = new ArrayList<Integer>();
long p1 = 0, p2 = 0, p3 = 0;
while ( p3 < limit ) {
p1 = p2; p2 = p3; p3 = primes.next();
if ( ( p2 - p1 ) % 18 != 0 || ( p3 - p2 ) % 18 != 0 ) {
continue;
}
if ( ! haveSameDigits(p1, p2) ) {
continue;
}
if ( haveSameDigits(p2, p3) ) {
if ( count < 25 ) {
ormistons.add(p1);
}
if ( p1 >= lowerLimit ) {
counts.add(count);
lowerLimit *= 10;
}
count += 1;
}
}
counts.add(count);
System.out.println("The smallest member of the first 25 Ormiston triples:");
for ( int i = 0; i < ormistons.size(); i++ ) {
System.out.print(String.format("%10d%s", ormistons.get(i), ( i % 5 == 4 ? "\n" : "" )));
}
System.out.println();
lowerLimit = limit / 10;
for ( int i = 0; i < counts.size(); i++ ) {
System.out.println(counts.get(i) + " Ormiston triples less than " + lowerLimit);
lowerLimit *= 10;
}
}
private static boolean haveSameDigits(long aOne, long aTwo) {
return digits(aOne).equals(digits(aTwo));
}
private static List<Integer> digits(long aNumber) {
List<Integer> result = new ArrayList<Integer>();
while ( aNumber > 0 ) {
result.add((int) ( aNumber % 10 ));
aNumber /= 10;
}
Collections.sort(result);
return result;
}
}
final class SegmentedPrimeIterator {
public SegmentedPrimeIterator(long aLimit) {
squareRoot = (int) Math.sqrt(aLimit);
high = squareRoot;
smallSieve(squareRoot);
}
public long next() {
if ( index == primes.size() ) {
index = 0;
segmentedSieve();
}
return primes.get(index++);
}
private void segmentedSieve() {
low += squareRoot;
high += squareRoot;
boolean[] markedPrime = new boolean[squareRoot];
Arrays.fill(markedPrime, true);
for ( int i = 0; i < smallPrimes.size(); i++ ) {
long lowLimit = ( low / smallPrimes.get(i) ) * smallPrimes.get(i);
if ( lowLimit < low ) {
lowLimit += smallPrimes.get(i);
}
for ( long j = lowLimit; j < high; j += smallPrimes.get(i) ) {
markedPrime[(int) ( j - low )] = false;
}
}
primes.clear();
for ( long i = low; i < high; i++ ) {
if ( markedPrime[(int) ( i - low )] ) {
primes.add(i);
}
}
}
private void smallSieve(int aSquareRoot) {
boolean[] markedPrime = new boolean[aSquareRoot + 1];
Arrays.fill(markedPrime, true);
for ( int p = 2; p * p <= aSquareRoot; p++ ) {
if ( markedPrime[p] ) {
for ( int i = p * p; i <= aSquareRoot; i += p ) {
markedPrime[i] = false;
}
}
}
for ( int p = 2; p <= aSquareRoot; p++ ) {
if ( markedPrime[p] ) {
primes.add((long) p);
}
}
smallPrimes.addAll(primes);
}
private static int index, squareRoot;
private static long low, high;
private static List<Long> primes = new ArrayList<Long>();
private static List<Long> smallPrimes = new ArrayList<Long>();
}
You may also check:How to resolve the algorithm Multi-dimensional array step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Peano curve step by step in the Ruby programming language
You may also check:How to resolve the algorithm Hello world/Web server step by step in the Ruby programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the K programming language