How to resolve the algorithm Smallest number k such that k+2^m is composite for all m less than k step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smallest number k such that k+2^m is composite for all m less than k step by step in the Java programming language

Table of Contents

Problem Statement

Generate the sequence of numbers a(k), where each k is the smallest positive integer such that k + 2m is composite for every positive integer m less than k.

Suppose k == 7; test m == 1 through m == 6. If any are prime, the test fails. Is 7 + 21 (9) prime? False Is 7 + 22 (11) prime? True So 7 is not an element of this sequence. It is only necessary to test odd natural numbers k. An even number, plus any positive integer power of 2 is always composite.

Find and display, here on this page, the first 5 elements of this sequence.

OEIS:A033919 - Odd k for which k+2^m is composite for all m < k

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smallest number k such that k+2^m is composite for all m less than k step by step in the Java programming language

The Java program presented is intended to discover and display the initial five values of k that fulfill the condition of being a k-number, as defined by the OEIS sequence A033919. These particular numbers possess the property that when you raise the following number to the power k and subtract one, the result is a probable prime number.

To start, some essential variables are declared. The objective is to uncover the first five numbers matching this criterion, so the variable count is initialized to zero and incremented each time a qualifying number is found. The program begins its search with k=3 and iterates through odd numbers in ascending order until it identifies the requisite number of qualifying numbers.

The program employs a function called isA033919 to validate whether a given k is indeed a k-number. This function iterates through all integers m from 1 to k-1. For each m, it evaluates the expression k + (1 << m) and checks if the result is a probable prime number using the isProbablePrime method with a certainty of 20 (meaning that the probability of incorrectly identifying a composite number as prime is less than 1 in 2^20 or approximately 1 in 1,048,576).

If any of these intermediate values yield a probable prime number, the function concludes that k does not meet the criteria and returns false. However, if none of these intermediate values are probable primes, the function infers that k is a k-number and returns true.

In summary, the provided Java code offers a means of computing and displaying the first five k-numbers, as specified by the OEIS sequence A033919.

Source code in the java programming language

import java.math.BigInteger;

public final class SmallestNumberK {

	public static void main(String[] aArgs) {
		int count = 0;
		int k = 3;
		while ( count < 5 ) {
		    if ( isA033919(k) ) {
		    	System.out.print(k + " ");
		    	count += 1;
		    }		    
		    k += 2;
		}
		System.out.println();
	}
	
	private static boolean isA033919(int aK) {
		final BigInteger bigK = BigInteger.valueOf(aK);
		for ( int m = 1; m < aK; m++ ) {
		    if ( bigK.add(BigInteger.ONE.shiftLeft(m)).isProbablePrime(20) ) {
		      return false;
		    }
		}
		return true;
	}

}


  

You may also check:How to resolve the algorithm N-queens problem step by step in the Pure programming language
You may also check:How to resolve the algorithm Compile-time calculation step by step in the Objeck programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/Construct from rational number step by step in the BASIC programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Swift programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Racket programming language