How to resolve the algorithm Wagstaff primes step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Wagstaff primes step by step in the Java programming language

Table of Contents

Problem Statement

A Wagstaff prime is a prime number of the form (2^p + 1)/3 where the exponent p is an odd prime. (2^5 + 1)/3 = 11 is a Wagstaff prime because both 5 and 11 are primes. Find and show here the first 10 Wagstaff primes and their corresponding exponents p. Find and show here the exponents p corresponding to the next 14 Wagstaff primes (not the primes themselves) and any more that you have the patience for. When testing for primality, you may use a method which determines that a large number is probably prime with reasonable certainty. It can be shown (see talk page) that (2^p + 1)/3 is always integral if p is odd. So there's no need to check for that prior to checking for primality.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Wagstaff primes step by step in the Java programming language

The provided Java code generates and prints prime numbers in the range 3 to 5808. It uses the BigInteger class to handle large integer values. A short explanation of the code:

  • Generating primes:

    • The program starts by initializing a BigInteger d to 3 (a prime number) and lmt to 25 (the maximum number of digits to print).
    • It enters a loop that runs from i = 3 to 5808.
    • Inside the loop, it calculates a by shifting 1 by i bits to the left, adding 1, and dividing by d. This formula results in the next prime number after the current i.
    • If a is a probable prime (as determined by the isProbablePrime method with a certainty of 1), it's considered a prime.
  • Printing primes:

    • The program prints the prime count c and the value of i (bit length) of the prime.
    • It then converts a to a String, calculates its length sl, and checks if sl is less than lmt.
    • If sl is less than lmt, it prints the entire prime. Otherwise, it prints only the first 11 and last 11 digits, along with the sl to indicate the total number of digits.
  • Next probable prime:

    • After printing a prime, the program updates i to the next probable prime using the nextProbablePrime method of BigInteger. This method returns the next larger prime after i.

The program terminates after generating primes in the specified range.

Source code in the java programming language

import java.math.BigInteger; 

public class Main {
  public static void main(String[] args) {
    BigInteger d = new BigInteger("3"), a;
    int lmt = 25, sl, c = 0;
    for (int i = 3; i < 5808; ) {
      a = BigInteger.ONE.shiftLeft(i).add(BigInteger.ONE).divide(d);
      if (a.isProbablePrime(1)) {
        System.out.printf("%2d %4d ", ++c, i);
        String s = a.toString(); sl = s.length();
        if (sl < lmt) System.out.println(a);
        else System.out.println(s.substring(0, 11) + ".." + s.substring(sl - 11, sl) + " " + sl + " digits");
      }
      i = BigInteger.valueOf(i).nextProbablePrime().intValue();
    }
  }
}


  

You may also check:How to resolve the algorithm Continued fraction step by step in the C# programming language
You may also check:How to resolve the algorithm Additive primes step by step in the PL/I programming language
You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Read entire file step by step in the C programming language
You may also check:How to resolve the algorithm Vector products step by step in the Fantom programming language