How to resolve the algorithm Casting out nines step by step in the Kotlin programming language
How to resolve the algorithm Casting out nines step by step in the Kotlin programming language
Table of Contents
Problem Statement
Write a procedure (say
c o 9
( x )
{\displaystyle {\mathit {co9}}(x)}
) which implements Casting Out Nines as described by returning the checksum for
x
{\displaystyle x}
. Demonstrate the procedure using the examples given there, or others you may consider lucky. Note that this function does nothing more than calculate the least positive residue, modulo 9. Many of the solutions omit Part 1 for this reason. Many languages have a modulo operator, of which this is a trivial application. With that understanding, solutions to Part 1, if given, are encouraged to follow the naive pencil-and-paper or mental arithmetic of repeated digit addition understood to be "casting out nines", or some approach other than just reducing modulo 9 using a built-in operator. Solutions for part 2 and 3 are not required to make use of the function presented in part 1. Notwithstanding past Intel microcode errors, checking computer calculations like this would not be sensible. To find a computer use for your procedure: Demonstrate that your procedure can be used to generate or filter a range of numbers with the property
c o 9
( k )
c o 9
(
k
2
)
{\displaystyle {\mathit {co9}}(k)={\mathit {co9}}(k^{2})}
and show that this subset is a small proportion of the range and contains all the Kaprekar in the range. Considering this MathWorld page, produce a efficient algorithm based on the more mathematical treatment of Casting Out Nines, and realizing: Demonstrate your algorithm by generating or filtering a range of numbers with the property
k % (
B a s e
− 1 )
(
k
2
) % (
B a s e
− 1 )
{\displaystyle k%({\mathit {Base}}-1)==(k^{2})%({\mathit {Base}}-1)}
and show that this subset is a small proportion of the range and contains all the Kaprekar in the range.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Casting out nines step by step in the Kotlin programming language
The provided Kotlin code defines a function castOut
that generates a list of integers within a specified range that satisfy a particular condition. Here's a detailed explanation of the code:
-
castOut
Function:- It takes three integer parameters:
base
: This is the number whose multiples are filtered out.start
: The starting point of the range.end
: The ending point of the range.
- The function generates a list of integers within the range
[start, end]
that match a specific criterion.
- It takes three integer parameters:
-
Computation of
b
andran
:- The variable
b
is calculated asbase - 1
. ran
is a list of integers from 0 tob-1
(inclusive) that satisfy the following condition:it % b == (it * it) % b
. This condition means that the square of each number inran
is congruent to the number itself when divided byb
.
- The variable
-
Initialization:
x
is initialized tostart / b
.result
is initialized as an empty mutable list.
-
Loop:
- The code enters an infinite loop that continues until it breaks out explicitly.
- Inside the loop:
- For each
n
inran
, the variablek
is calculated asb * x + n
. - If
k
is less thanstart
, it is ignored and the loop continues to the next value ofn
. - If
k
is greater thanend
, it means the end of the range has been exceeded, so the function breaks out of the loop and returns theresult
list. - If
k
is within the range, it is added to theresult
list.
- For each
- After iterating through all values of
n
inran
,x
is incremented by 1, and the loop continues.
-
Return Value:
- When the loop exits, the function returns the
result
list, which contains all the integers within the range[start, end]
that satisfy the condition specified by theran
list.
- When the loop exits, the function returns the
-
Main Function:
- The main function calls the
castOut
function three times with different parameters and prints the results to the console.
- The main function calls the
In summary, the castOut
function filters out multiples of a given base (base
) from a specified range ([start, end]
) and generates a list of the remaining integers that satisfy a particular condition related to squaring modulo base
. It accomplishes this by iteratively checking each integer in the range and only including those that meet the condition.
Source code in the kotlin programming language
// version 1.1.3
fun castOut(base: Int, start: Int, end: Int): List<Int> {
val b = base - 1
val ran = (0 until b).filter { it % b == (it * it) % b }
var x = start / b
val result = mutableListOf<Int>()
while (true) {
for (n in ran) {
val k = b * x + n
if (k < start) continue
if (k > end) return result
result.add(k)
}
x++
}
}
fun main(args: Array<String>) {
println(castOut(16, 1, 255))
println()
println(castOut(10, 1, 99))
println()
println(castOut(17, 1, 288))
}
You may also check:How to resolve the algorithm Minimal steps down to 1 step by step in the Go programming language
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the Phix programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Pascal programming language
You may also check:How to resolve the algorithm Empty program step by step in the Nim programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Ada programming language