How to resolve the algorithm Unprimeable numbers step by step in the Kotlin programming language
How to resolve the algorithm Unprimeable numbers step by step in the Kotlin 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 Kotlin programming language
This code implements the "unprimeable" number sequence. An unprimeable number is an integer whose digits cannot be rearranged to form a prime number. For example, 123 is not unprimeable because its digits can be rearranged to form 132, which is prime. On the other hand, 147 is unprimeable because there is no way to rearrange its digits to form a prime number.
The code first generates a list of all primes less than 10^7 using the sieve of Eratosthenes. Then, it iterates through all integers from 1 to 10^7 and checks if each integer is unprimeable. If an integer is unprimeable, the code prints it out.
The code also provides a function to find the nth unprimeable number. This function takes an integer n as input and returns the nth unprimeable number.
Finally, the code provides a function to find the lowest unprimeable number that ends in a given digit. This function takes an integer digit as input and returns the lowest unprimeable number that ends in that digit.
Here is an example of how the code can be used:
val result = nthUnprimeableNumber(35)
println(result)
This code will print the 35th unprimeable number, which is 14237.
Source code in the kotlin programming language
private const val MAX = 10000000
private val primes = BooleanArray(MAX)
fun main() {
sieve()
println("First 35 unprimeable numbers:")
displayUnprimeableNumbers(35)
val n = 600
println()
println("The ${n}th unprimeable number = ${nthUnprimeableNumber(n)}")
println()
val lowest = genLowest()
println("Least unprimeable number that ends in:")
for (i in 0..9) {
println(" $i is ${lowest[i]}")
}
}
private fun genLowest(): IntArray {
val lowest = IntArray(10)
var count = 0
var test = 1
while (count < 10) {
test++
if (unPrimable(test) && lowest[test % 10] == 0) {
lowest[test % 10] = test
count++
}
}
return lowest
}
private fun nthUnprimeableNumber(maxCount: Int): Int {
var test = 1
var count = 0
var result = 0
while (count < maxCount) {
test++
if (unPrimable(test)) {
count++
result = test
}
}
return result
}
private fun displayUnprimeableNumbers(maxCount: Int) {
var test = 1
var count = 0
while (count < maxCount) {
test++
if (unPrimable(test)) {
count++
print("$test ")
}
}
println()
}
private fun unPrimable(test: Int): Boolean {
if (primes[test]) {
return false
}
val s = test.toString() + ""
for (i in s.indices) {
for (j in 0..9) {
if (primes[replace(s, i, j).toInt()]) {
return false
}
}
}
return true
}
private fun replace(str: String, position: Int, value: Int): String {
val sChar = str.toCharArray()
sChar[position] = value.toChar()
return str.substring(0, position) + value + str.substring(position + 1)
}
private fun sieve() {
// primes
for (i in 2 until MAX) {
primes[i] = true
}
for (i in 2 until MAX) {
if (primes[i]) {
var j = 2 * i
while (j < MAX) {
primes[j] = false
j += i
}
}
}
}
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Java programming language
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the x86 Assembly programming language
You may also check:How to resolve the algorithm Water collected between towers step by step in the Python programming language