How to resolve the algorithm Ascending primes step by step in the Java programming language
How to resolve the algorithm Ascending primes step by step in the Java programming language
Table of Contents
Problem Statement
Generate and show all primes with strictly ascending decimal digits. Aside: Try solving without peeking at existing solutions. I had a weird idea for generating a prime sieve faster, which needless to say didn't pan out. The solution may be p(r)etty trivial but generating them quickly is at least mildly interesting. Tip: filtering all 7,027,260 primes below 123,456,789 probably won't kill you, but there is at least one significantly better and much faster way, needing a mere 511 odd/prime tests.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ascending primes step by step in the Java programming language
This Java program generates and shows all prime numbers with strictly ascending decimal digits. Here's a detailed explanation of the code:
-
Problem Overview:
- The program finds prime numbers whose digits are arranged in ascending order (e.g., 12345, 23456).
- The program efficiently generates such numbers and checks their primality using a queue.
-
Queue for Ascending Numbers:
- An integer array
queue
is used as a queue to store ascending numbers. - The program starts with single-digit numbers (1 to 9) and enqueues them.
- As the program dequeues a number
n
, it enqueues all possible ascending numbers by appending digits greater than the last digit ofn
.
- An integer array
-
Primality Checking:
- Once the queue contains all ascending numbers, the program checks each number for primality using the
isPrime
method. - The
isPrime
method checks if a number is prime using basic primality rules.
- Once the queue contains all ascending numbers, the program checks each number for primality using the
-
Main Function:
- The
main
function starts the program and measures the execution time.
- The
-
run
Method:- The
run
method initializes the queue and generates ascending numbers. - It then uses a parallel stream to find prime numbers and prints the results.
- The
-
isPrime
Method:- The
isPrime
method returnstrue
if the given number is prime andfalse
otherwise. - It uses optimized primality checks for efficiency.
- The
-
Output:
- The program prints the list of prime numbers with strictly ascending decimal digits.
In summary, this program efficiently generates ascending numbers and checks their primality to find those that meet the desired criteria. It uses a queue to generate ascending numbers and leverages a parallel stream for fast primality checks.
Source code in the java programming language
/*
* Ascending primes
*
* Generate and show all primes with strictly ascending decimal digits.
*
*
* Solution
*
* We only consider positive numbers in the range 1 to 123456789. We would
* get 7027260 primes, because there are so many primes smaller than 123456789
* (see also Wolfram Alpha).On the other hand, there are only 511 distinct
* positive integers having their digits arranged in ascending order.
* Therefore, it is better to start with numbers that have properly arranged
* digits and then check if they are prime numbers.The method of generating
* a sequence of such numbers is not indifferent.We want this sequence to be
* monotonically increasing, because then additional sorting of results will
* be unnecessary. It turns out that by using a queue we can easily get the
* desired effect. Additionally, the algorithm then does not use recursion
* (although the program probably does not have to comply with the MISRA
* standard). The problem to be solved is the queue size, the a priori
* assumption that 1000 is good enough, but a bit magical.
*/
package example.rossetacode.ascendingprimes;
import java.util.Arrays;
public class Program implements Runnable {
public static void main(String[] args) {
long t1 = System.nanoTime();
new Program().run();
long t2 = System.nanoTime();
System.out.println(
"total time consumed = " + (t2 - t1) * 1E-6 + " milliseconds");
}
public void run() {
final int MAX_SIZE = 1000;
final int[] queue = new int[MAX_SIZE];
int begin = 0;
int end = 0;
for (int k = 1; k <= 9; k++) {
queue[end++] = k;
}
while (begin < end) {
int n = queue[begin++];
for (int k = n % 10 + 1; k <= 9; k++) {
queue[end++] = n * 10 + k;
}
}
// We can use a parallel stream (and then sort the results)
// to use multiple cores.
//
System.out.println(Arrays.stream(queue).filter(this::isPrime).boxed().toList());
}
private boolean isPrime(int n) {
if (n == 2) {
return true;
}
if (n == 1 || n % 2 == 0) {
return false;
}
int root = (int) Math.sqrt(n);
for (int k = 3; k <= root; k += 2) {
if (n % k == 0) {
return false;
}
}
return true;
}
}
You may also check:How to resolve the algorithm Comments step by step in the FUZE BASIC programming language
You may also check:How to resolve the algorithm Pick random element step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Population count step by step in the Ada programming language
You may also check:How to resolve the algorithm Wordiff step by step in the Nim programming language
You may also check:How to resolve the algorithm Knuth's algorithm S step by step in the Perl programming language