How to resolve the algorithm Smith numbers step by step in the Kotlin programming language
How to resolve the algorithm Smith numbers step by step in the Kotlin programming language
Table of Contents
Problem Statement
Smith numbers are numbers such that the sum of the decimal digits of the integers that make up that number is the same as the sum of the decimal digits of its prime factors excluding 1. By definition, all primes are excluded as they (naturally) satisfy this condition! Smith numbers are also known as joke numbers.
Using the number 166 Find the prime factors of 166 which are: 2 x 83 Then, take those two prime factors and sum all their decimal digits: 2 + 8 + 3 which is 13 Then, take the decimal digits of 166 and add their decimal digits: 1 + 6 + 6 which is 13 Therefore, the number 166 is a Smith number.
Write a program to find all Smith numbers below 10000.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Smith numbers step by step in the Kotlin programming language
The provided Kotlin code is designed to find and print all Smith numbers below 10,000. A Smith number is a composite number whose sum of digits is the same as the sum of digits in its prime factors.
Here's a breakdown of the code:
-
The program begins with three helper functions:
-
getPrimeFactors(n)
: This function takes an integern
and returns a mutable list containing the prime factors ofn
. It uses a while loop to repeatedly dividen
by prime numbers untiln
is equal to 1, and collects the prime factors along the way. -
sumDigits(n)
: This function takes an integern
and returns the sum of its digits. Ifn
is less than 10, it returnsn
directly. Otherwise, it repeatedly extracts the last digit ofn
and adds it to a running sum untiln
becomes 0. -
isSmith(n)
: This function checks whether a given integern
is a Smith number. It first ensures thatn
is a composite number (i.e.,n
has more than one prime factor). Then, it calculates the sum of digits inn
usingsumDigits(n)
and the sum of digits in its prime factors (usingsumBy
andsumDigits
). If these two sums are equal,n
is a Smith number.
-
-
The
main
function, which is the entry point of the program, does the following:-
It iterates through all integers from 2 to 9,999 (exclusive).
-
For each integer
i
, it checks if it's a Smith number using theisSmith
function. -
If
i
is a Smith number, its value is printed on the console, and a count of Smith numbers found is incremented.
-
-
After processing all integers, the program prints the total count of Smith numbers found below 10,000.
In summary, this Kotlin program efficiently finds and displays all Smith numbers below 10,000 using its helper functions for calculating prime factors and summing digits.
Source code in the kotlin programming language
// version 1.0.6
fun getPrimeFactors(n: Int): MutableList<Int> {
val factors = mutableListOf<Int>()
if (n < 2) return factors
var factor = 2
var nn = n
while (true) {
if (nn % factor == 0) {
factors.add(factor)
nn /= factor
if (nn == 1) return factors
}
else if (factor >= 3) factor += 2
else factor = 3
}
}
fun sumDigits(n: Int): Int = when {
n < 10 -> n
else -> {
var sum = 0
var nn = n
while (nn > 0) {
sum += (nn % 10)
nn /= 10
}
sum
}
}
fun isSmith(n: Int): Boolean {
if (n < 2) return false
val factors = getPrimeFactors(n)
if (factors.size == 1) return false
val primeSum = factors.sumBy { sumDigits(it) }
return sumDigits(n) == primeSum
}
fun main(args: Array<String>) {
println("The Smith numbers below 10000 are:\n")
var count = 0
for (i in 2 until 10000) {
if (isSmith(i)) {
print("%5d".format(i))
count++
}
}
println("\n\n$count numbers found")
}
You may also check:How to resolve the algorithm Logical operations step by step in the zkl programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Dart programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Aime programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Go programming language
You may also check:How to resolve the algorithm Tau number step by step in the PILOT programming language