How to resolve the algorithm Unprimeable numbers step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Unprimeable numbers step by step in the Java programming language

Table of Contents

Problem Statement

As used here, all unprimeable numbers   (positive integers)   are always expressed in base ten.

───── Definition from OEIS ─────: Unprimeable numbers are composite numbers that always remain composite when a single decimal digit of the number is changed.

───── Definition from Wiktionary   (referenced from Adam Spencer's book) ─────: (arithmetic)   that cannot be turned into a prime number by changing just one of its digits to any other digit.   (sic)

Unprimeable numbers are also spelled:   unprimable. All one─ and two─digit numbers can be turned into primes by changing a single decimal digit.

190   isn't unprimeable,   because by changing the zero digit into a three yields   193,   which is a prime.

The number   200   is unprimeable,   since none of the numbers   201, 202, 203, ··· 209   are prime, and all the other numbers obtained by changing a single digit to produce   100, 300, 400, ··· 900,   or   210, 220, 230, ··· 290   which are all even.

It is valid to change   189   into   089   by changing the   1   (one)   into a   0   (zero),   which then the leading zero can be removed,   and then treated as if the   "new"   number is   89.

Show all output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Unprimeable numbers step by step in the Java programming language

1. Overview

  • This Java program identifies and analyzes "unprimeable numbers." An unprimeable number is a positive integer for which it is impossible to change a single digit to create a prime number.
  • It uses a sieve of Eratosthenes to generate prime numbers up to a specified maximum value (10 million in this case).

2. Main Method

  • The main method:
    • Calls the sieve method to generate prime numbers.
    • Prints the first 35 unprimeable numbers.
    • Finds and prints the 600th unprimeable number.
    • Finds and prints the least unprimeable number that ends in each digit from 0 to 9.

3. Helper Methods

  • genLowest: Generates an array containing the least unprimeable number that ends in each digit from 0 to 9.
  • nthUnprimeableNumber: Finds and returns the nth unprimeable number.
  • displayUnprimeableNumbers: Prints the first maxCount unprimeable numbers.
  • unPrimable: Checks whether a given integer test is unprimeable.
    • It iterates over each digit in test, replacing it with every possible digit (0-9) and checking if the resulting number is prime. If any such replacement yields a prime number, test is not unprimeable and the method returns false.
  • replace: Replaces a character (digit) at a specific position in a string.
  • sieve: Uses the Sieve of Eratosthenes algorithm to generate prime numbers up to a specified maximum value.

4. Algorithm

  • The program checks whether a given number is unprimeable by trying all possible single-digit replacements and seeing if any of them produce a prime number.
  • It uses a precomputed list of prime numbers generated by the sieve of Eratosthenes to efficiently check for primality.

Source code in the java programming language

public class UnprimeableNumbers {

    private static int MAX = 10_000_000;
    private static boolean[] primes = new boolean[MAX];

    public static void main(String[] args) {
        sieve();
        System.out.println("First 35 unprimeable numbers:");
        displayUnprimeableNumbers(35);
        int n = 600;
        System.out.printf("%nThe %dth unprimeable number = %,d%n%n", n, nthUnprimeableNumber(n));
        int[] lowest = genLowest();
        System.out.println("Least unprimeable number that ends in:");
        for ( int i = 0 ; i <= 9 ; i++ ) {
            System.out.printf(" %d is %,d%n", i, lowest[i]);
        }
    }
    
    private static int[] genLowest() {
        int[] lowest = new int[10];
        int count = 0;
        int test = 1;
        while ( count < 10 ) {
            test++;
            if ( unPrimable(test) && lowest[test % 10] == 0 ) {
                lowest[test % 10] = test;
                count++;
            }
        }
        return lowest;
    }

    private static int nthUnprimeableNumber(int maxCount) {
        int test = 1;
        int count = 0;
        int result = 0;
        while ( count < maxCount ) {
            test++;
            if ( unPrimable(test) ) {
                count++;
                result = test;
            }
        }
        return result;
    }

    private static void displayUnprimeableNumbers(int maxCount) {
        int test = 1;
        int count = 0;
        while ( count < maxCount ) {
            test++;
            if ( unPrimable(test) ) {
                count++;
                System.out.printf("%d ", test);
            }
        }
        System.out.println();
    }
    
    private static boolean unPrimable(int test) {
        if ( primes[test] ) {
            return false;
        }
        String s = test + "";
        for ( int i = 0 ; i < s.length() ; i++ ) {
            for ( int j = 0 ; j <= 9 ; j++ ) {
                if ( primes[Integer.parseInt(replace(s, i, j))] ) {
                    return false;
                }
            }
        }
        return true;
    }
    
    private static String replace(String str, int position, int value) {
        char[] sChar = str.toCharArray();
        sChar[position] = (char) value;
        return str.substring(0, position) + value + str.substring(position + 1);
    }

    private static final void sieve() {
        //  primes
        for ( int i = 2 ; i < MAX ; i++ ) {
            primes[i] = true;            
        }
        for ( int i = 2 ; i < MAX ; i++ ) {
            if ( primes[i] ) {
                for ( int j = 2*i ; j < MAX ; j += i ) {
                    primes[j] = false;
                }
            }
        }
    }

}


  

You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the Raku programming language
You may also check:How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the J programming language
You may also check:How to resolve the algorithm Five weekends step by step in the VBA programming language
You may also check:How to resolve the algorithm Musical scale step by step in the Forth programming language
You may also check:How to resolve the algorithm Nim game step by step in the REXX programming language