How to find all the strong numbers in a given range with Go

1 Answer

0 votes
// Strong numbers are the numbers that the sum of factorial of its digits 
// is equal to the original number
    
// 145 is a strong number: 1 + 24 + 120 = 145

package main

import "fmt"

func factorial(n int) int {
    fact := 1
    for i := 2; i <= n; i++ {
        fact *= i
    }
    return fact
}

func main() {
    for n := 1; n <= 1000000; n++ {
        tmp := n
        sum := 0
        for tmp != 0 {
            reminder := tmp % 10
            sum += factorial(reminder)
            tmp /= 10
        }
        if sum == n {
            fmt.Println(n)
        }
    }
}




/*
run:

1
2
145
40585

*/

 



answered Jul 5, 2024 by avibootz

Related questions

1 answer 121 views
1 answer 177 views
1 answer 105 views
1 answer 2,180 views
1 answer 215 views
...