-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
krishnamurthy.go
34 lines (29 loc) · 1017 Bytes
/
krishnamurthy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// filename : krishnamurthy.go
// description: A program which contains the function that returns true if a given number is Krishnamurthy number or not.
// details: A number is a Krishnamurthy number if the sum of all the factorials of the digits is equal to the number.
// Ex: 1! = 1, 145 = 1! + 4! + 5!
// time complexity: O(log n)
// space complexity: O(1)
// author(s): [GooMonk](https://github.com/GooMonk)
// see krishnamurthy_test.go
package math
import "github.com/TheAlgorithms/Go/constraints"
// IsKrishnamurthyNumber returns if the provided number n is a Krishnamurthy number or not.
func IsKrishnamurthyNumber[T constraints.Integer](n T) bool {
if n <= 0 {
return false
}
// Preprocessing: Using a slice to store the digit Factorials
digitFact := make([]T, 10)
digitFact[0] = 1 // 0! = 1
for i := 1; i < 10; i++ {
digitFact[i] = digitFact[i-1] * T(i)
}
// Subtract the digit Facotorial from the number
nTemp := n
for n > 0 {
nTemp -= digitFact[n%10]
n /= 10
}
return nTemp == 0
}