MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / IsKrishnamurthyNumber

Function IsKrishnamurthyNumber

math/krishnamurthy.go:14–34  ·  view source on GitHub ↗

IsKrishnamurthyNumber returns if the provided number n is a Krishnamurthy number or not.

(n T)

Source from the content-addressed store, hash-verified

12
13// IsKrishnamurthyNumber returns if the provided number n is a Krishnamurthy number or not.
14func 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}

Callers 2

Calls

no outgoing calls

Tested by 2