Last active
July 1, 2018 06:17
-
-
Save matinkaboli/1168ca6a4f29a33b03f1e6273b2eca33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
sum := 0 | |
for i := 3; i < 2540161; i++ { | |
if facEqual(i) { | |
sum += i | |
} | |
} | |
fmt.Println(sum) | |
} | |
func factorial(num int) int { | |
if num <= 1 { | |
return 1 | |
} | |
return num * factorial(num-1) | |
} | |
func facEqual(num int) bool { | |
str := strconv.Itoa(num) | |
arr := strings.Split(str, "") | |
sum := 0 | |
for _, v := range arr { | |
i, err := strconv.Atoi(v) | |
if err != nil { | |
os.Exit(1) | |
} | |
sum += factorial(i) | |
} | |
if num == sum { | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment