Skip to content

halilozel1903/KotlinSmithNumber

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kotlin Smith Number 🎬 2️⃣ 📗

Kotlin Smith Number

A Smith number is a composite number whose sum of digits in its prime factorization is equal to the sum of digits in the number itself.

For example, 85 is a Smith number because: 85 = 5 x 17 (prime factorization) sum of digits in 85 = 8 + 5 = 13 sum of digits in 5 + 1 + 7 = 13

Here is a program in Kotlin that finds all the Smith numbers within a given range:

fun main() {
    val range = 1..100
    println("Smith numbers between ${range.first} and ${range.last}:")
    range.filter { isSmithNumber(it) }.forEach(::println)
}

fun isSmithNumber(num: Int): Boolean {
    return if (num < 4) false
    else sumOfDigits(num) == primeFactors(num).sumOf { sumOfDigits(it) }
}

fun primeFactors(n: Int): List<Int> {
    var number = n
    val factors = mutableListOf<Int>()
    var i = 2
    while (i <= number) {
        while (number % i == 0) {
            factors.add(i)
            number /= i
        }
        i++
    }
    return factors
}

fun sumOfDigits(n: Int): Int {
    return n.toString().sumOf { it - '0' }
}

The main function sets a range for which Smith numbers are to be found, and loops through each number in the range. If a number is found to be a Smith number, it is printed to the console.

The isSmithNumber function checks whether a given number is a Smith number. It first checks if the number is less than 4, in which case it cannot be a composite number and therefore cannot be a Smith number. It then calculates the sum of digits in the number itself and in its prime factors, and returns true if the two sums are equal.

The primeFactors function returns a list of prime factors of a given number. It loops through each number from 2 to the square root of the given number, checking if each number is a factor. If a number is a factor, it is added to the list of factors and the given number is divided by it. If the given number is still greater than 1 after the loop, it is added to the list of factors as well.

The sumOfDigits function returns the sum of digits in a given number. It loops through each digit in the number, adding it to a running sum.

Note that this program assumes that the given range starts from 1 and goes up to 100, but this can be easily modified by changing the values of start and end in the main function.

When we run the program, it will output all the smith numbers up to the limit we set:

Smith numbers between 1 and 100:
4
5
7
11
13
17
19
22
23
27
29
31
37
41
43
47
53
58
59
61
67
71
73
79
83
85
89
94
97

Donation 💸

If this project help 💁 you, Can you give me a cup of coffee? ☕

"Buy Me A Coffee"

License ℹ️

MIT License

Copyright (c) 2023 Halil OZEL

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.