Krishnamurthy Number

What is Krishnamurthy Number?

A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself.

For example 145, the sum of the factorial of each digit:

1! + 4! + 5! = 1 + 24 + 120 = 145

Check if a number is a Krishnamurthy Number or not

package _KrishnamurthyNumber

class KrishnamurthyNumber {

    fun isKrishnamurthyNumber(n: Int): Boolean {
        var temp = n
        var sum = 0
        while (temp != 0) {
            sum += factorial(temp % 10)
            temp/=10
        }
        return sum == n
    }

    private fun factorial(number: Int): Int {
        var n = number
        var fact = 1
        while (n != 0) {
            fact *= n
            n--
        }
        return fact
    }
}
原文地址:https://www.cnblogs.com/johnnyzhao/p/12652058.html