How to resolve the algorithm Iterated digits squaring step by step in the Kotlin programming language
Published on 22 June 2024 08:30 PM
How to resolve the algorithm Iterated digits squaring step by step in the Kotlin programming language
Table of Contents
Problem Statement
If you add the square of the digits of a Natural number (an integer bigger than zero), you always end with either 1 or 89: An example in Python:
Or, for much less credit - (showing that your algorithm and/or language is slow): This problem derives from the Project Euler problem 92. For a quick algorithm for this task see the talk page
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Iterated digits squaring step by step in the Kotlin programming language
Explanation:
Function endsWith89
:
- Takes an integer
n
as input. - Initializes variables
digit
,sum
, andnn
to store the digit extracted fromn
, the sum of squared digits, and a copy ofn
. - Enters an infinite loop (represented by
while (true)
) to iteratively calculate the sum of squared digits:- It extracts each digit from
nn
and adds its square tosum
. - It divides
nn
by 10 to remove the last digit.
- It extracts each digit from
- If
sum
becomes 89, it returnstrue
, indicating thatn
ends with 89. - If
sum
becomes 1, it returnsfalse
, indicating thatn
does not end with 89. - It updates
nn
to besum
and resetssum
to 0.
Main Function:
- Initializes an array
sums
of size 8 * 81 + 1, where8 * 81
represents numbers up to 100 million. - Initializes
sums[0]
andsums[1]
to 1 and 0, respectively, as the sums of squared digits for 0 and 1. - Utilizes nested loops to calculate the sums for every number from 1 to 8 * 81:
- The outer loop iterates through numbers from 1 to 8, representing the starting point for a possible chain of squared digit summing.
- The middle loop iterates through numbers from the current starting point down to 1.
- The inner loop iterates through digits from 1 to 9 and calculates the sum of squared digits for each possible digit combination.
- Initializes
count89
to 0, which will track the count of numbers from 1 to 100 million that end with 89. - Iterates through numbers from 1 to 8 * 81 and checks if they end with 89 using the
endsWith89
function. If true, it incrementscount89
by the number of ways to reach that number by summing squared digits. - Finally, prints the count of numbers from 1 to 100 million that end with 89.
Source code in the kotlin programming language
// version 1.0.6
fun endsWith89(n: Int): Boolean {
var digit: Int
var sum = 0
var nn = n
while (true) {
while (nn > 0) {
digit = nn % 10
sum += digit * digit
nn /= 10
}
if (sum == 89) return true
if (sum == 1) return false
nn = sum
sum = 0
}
}
fun main(args: Array<String>) {
val sums = IntArray(8 * 81 + 1)
sums[0] = 1
sums[1] = 0
var s: Int
for (n in 1 .. 8)
for (i in n * 81 downTo 1)
for (j in 1 .. 9) {
s = j * j
if (s > i) break
sums[i] += sums[i - s]
}
var count89 = 0
for (i in 1 .. 8 * 81)
if (endsWith89(i)) count89 += sums[i]
println("There are $count89 numbers from 1 to 100 million ending with 89")
}
You may also check:How to resolve the algorithm Ranking methods step by step in the AWK programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the C# programming language
You may also check:How to resolve the algorithm Sub-unit squares step by step in the J programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the VBA programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Arturo programming language