How to resolve the algorithm Gapful numbers step by step in the Kotlin programming language
How to resolve the algorithm Gapful numbers step by step in the Kotlin programming language
Table of Contents
Problem Statement
Numbers (positive integers expressed in base ten) that are (evenly) divisible by the number formed by the first and last digit are known as gapful numbers.
Evenly divisible means divisible with no remainder.
All one─ and two─digit numbers have this property and are trivially excluded. Only numbers ≥ 100 will be considered for this Rosetta Code task.
187 is a gapful number because it is evenly divisible by the number 17 which is formed by the first and last decimal digits of 187.
About 7.46% of positive integers are gapful.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Gapful numbers step by step in the Kotlin programming language
This Kotlin code generates and prints the first n
gapful numbers starting from a given number. A gapful number is a number that has a difference of at least one between its first and last digits and is divisible by the number formed by its first and last digit.
-
Function
commatize
: This function takes a long integer as input and converts it to a string with commas as thousands separators. -
Main Function:
- It defines two lists,
starts
andcounts
, which specify the starting numbers and the number of gapful numbers to find for each starting number, respectively. - It iterates through the indices of the
starts
list. For each indexi
:- It initializes variables
count
,j
(the current number being checked), andpow
(the power of 10 used to determine the first and last digits). - It prints the first
counts[i]
gapful numbers starting fromstarts[i]
. It continues checking numbers until the desired count is reached. - It calculates the number formed by the first and last digits (
fl
) ofj
and checks ifj
is divisible byfl
. If it is, it printsj
and incrementscount
. - It increments
j
and updatespow
if necessary to check the next set of first and last digits.
- It initializes variables
- After processing all the starting numbers, it prints a newline.
- It defines two lists,
Source code in the kotlin programming language
private fun commatize(n: Long): String {
val sb = StringBuilder(n.toString())
val le = sb.length
var i = le - 3
while (i >= 1) {
sb.insert(i, ',')
i -= 3
}
return sb.toString()
}
fun main() {
val starts = listOf(1e2.toLong(), 1e6.toLong(), 1e7.toLong(), 1e9.toLong(), 7123.toLong())
val counts = listOf(30, 15, 15, 10, 25)
for (i in starts.indices) {
var count = 0
var j = starts[i]
var pow: Long = 100
while (j >= pow * 10) {
pow *= 10
}
System.out.printf(
"First %d gapful numbers starting at %s:\n",
counts[i],
commatize(starts[i])
)
while (count < counts[i]) {
val fl = j / pow * 10 + j % 10
if (j % fl == 0L) {
System.out.printf("%d ", j)
count++
}
j++
if (j >= 10 * pow) {
pow *= 10
}
}
println('\n')
}
}
You may also check:How to resolve the algorithm Tokenize a string with escaping step by step in the BQN programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the COBOL programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the CLU programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Play recorded sounds step by step in the Go programming language