IsKrishnamurthyNumber returns if the provided number n is a Krishnamurthy number or not.
(n T)
| 12 | |
| 13 | // IsKrishnamurthyNumber returns if the provided number n is a Krishnamurthy number or not. |
| 14 | func IsKrishnamurthyNumber[T constraints.Integer](n T) bool { |
| 15 | if n <= 0 { |
| 16 | return false |
| 17 | } |
| 18 | |
| 19 | // Preprocessing: Using a slice to store the digit Factorials |
| 20 | digitFact := make([]T, 10) |
| 21 | digitFact[0] = 1 // 0! = 1 |
| 22 | |
| 23 | for i := 1; i < 10; i++ { |
| 24 | digitFact[i] = digitFact[i-1] * T(i) |
| 25 | } |
| 26 | |
| 27 | // Subtract the digit Facotorial from the number |
| 28 | nTemp := n |
| 29 | for n > 0 { |
| 30 | nTemp -= digitFact[n%10] |
| 31 | n /= 10 |
| 32 | } |
| 33 | return nTemp == 0 |
| 34 | } |
no outgoing calls