Last active
August 29, 2015 14:20
-
-
Save dannluciano/f2bb68396f84b55f5419 to your computer and use it in GitHub Desktop.
HappyNumbers
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" | |
) | |
func is_happy_number(number int) bool { | |
if number < 10 { | |
return (number == 1 || number == 7) | |
} else { | |
sum := 0 | |
for ; number != 0; number /= 10 { | |
digit := number % 10 | |
sum += digit * digit | |
} | |
return is_happy_number(sum) | |
} | |
} | |
func main() { | |
for n := 1; n <= 1000; n += 1 { | |
if is_happy_number(n) { | |
fmt.Printf("%d, ", n) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment