How to resolve the algorithm Cullen and Woodall numbers step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cullen and Woodall numbers step by step in the Java programming language

Table of Contents

Problem Statement

A Cullen number is a number of the form n × 2n + 1 where n is a natural number. A Woodall number is very similar. It is a number of the form n × 2n - 1 where n is a natural number. So for each n the associated Cullen number and Woodall number differ by 2. Woodall numbers are sometimes referred to as Riesel numbers or Cullen numbers of the second kind.

Cullen primes are Cullen numbers that are prime. Similarly, Woodall primes are Woodall numbers that are prime. It is common to list the Cullen and Woodall primes by the value of n rather than the full evaluated expression. They tend to get very large very quickly. For example, the third Cullen prime, n == 4713, has 1423 digits when evaluated.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Cullen and Woodall numbers step by step in the Java programming language

The provided Java code is popularly known as the Cullen and Woodhall numbers and their corresponding primes, which are two sequences of numbers that share a unique relationship with prime numbers.

Here's a detailed explanation:

  1. Number Sequence Generation:

    • numberSequence() method: This method generates a specified number of Cullen or Woodhall numbers.
    • NumberType enum: It defines two types of number sequences: Cullen and Woodhall. Cullen numbers are calculated as n * (2^n) + 1, while Woodhall numbers are calculated as n * (2^n) - 1.
  2. Prime Sequence Generation:

    • primeSequence() method: This method generates the indexes of the first N Cullen or Woodhall primes.
  3. Calculation of the Numbers:

    • nextNumber() method: It calculates the next Cullen or Woodhall number based on number and power variables. It increments number by 1 and shifts power left by 1.
  4. Number Initialization:

    • numberInitialise() method: It initializes number and power to 0 and 1, respectively.
  5. Prime Initialization:

    • primeInitialise() method: It initializes count to 0, primeIndex to 1, and calls numberInitialise().
  6. Constants:

    • CERTAINTY: It's a constant set to 20, which specifies the certainty level for primality testing.

Usage: The main method demonstrates the usage of these methods:

  • It prints the first 20 Cullen numbers.
  • It prints the first 20 Woodhall numbers.
  • It prints the indexes of the first 5 Cullen primes.
  • It prints the indexes of the first 12 Woodhall primes.

Source code in the java programming language

import java.math.BigInteger;

public final  class CullenAndWoodhall {

	public static void main(String[] aArgs) {
		numberSequence(20, NumberType.Cullen);
		
		numberSequence(20, NumberType.Woodhall);
		
		primeSequence(5, NumberType.Cullen);
		
		primeSequence(12, NumberType.Woodhall);
	}

	private enum NumberType { Cullen, Woodhall }
	
	private static void numberSequence(int aCount, NumberType aNumberType) {
		System.out.println();
		System.out.println("The first " + aCount + " " + aNumberType + " numbers are:");
		numberInitialise();
		for ( int index = 1; index <= aCount; index++ ) {
			System.out.print(nextNumber(aNumberType) + " ");
		}	
		System.out.println();	
	}
	
	private static void primeSequence(int aCount, NumberType aNumberType) {
		System.out.println();
		System.out.println("The indexes of the first " + aCount + " " + aNumberType + " primes are:");
		primeInitialise();
		
		while ( count < aCount ) {			
			if ( nextNumber(aNumberType).isProbablePrime(CERTAINTY) ) {
				System.out.print(primeIndex + " ");
				count += 1;
			}
			
			primeIndex += 1; 
		}
		System.out.println();
	}
	
	private static BigInteger nextNumber(NumberType aNumberType) {
		number = number.add(BigInteger.ONE);
		power = power.shiftLeft(1);
		return switch ( aNumberType ) {
			case Cullen -> number.multiply(power).add(BigInteger.ONE);
			case Woodhall -> number.multiply(power).subtract(BigInteger.ONE);
		};
	}
	
	private static void numberInitialise() {
		number = BigInteger.ZERO;
		power = BigInteger.ONE;		
	}
	
	private static void primeInitialise() {	
		count = 0;
		primeIndex = 1;
		numberInitialise();
	}
	
	private static BigInteger number;
	private static BigInteger power;
	private static int count;
	private static int primeIndex;
	
	private static final int CERTAINTY = 20;
	
}


  

You may also check:How to resolve the algorithm Execute HQ9+ step by step in the zkl programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the Haskell programming language
You may also check:How to resolve the algorithm Safe addition step by step in the Ada programming language
You may also check:How to resolve the algorithm String append step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the Elena programming language