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
    }
}

 

posted @ 2020-04-07 10:56  johnny_zhao  阅读(110)  评论(0编辑  收藏  举报