-
-
Save avelino/88aaa34cd7806855ccc9 to your computer and use it in GitHub Desktop.
refactoring happy numbers in Go
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" | |
"math" | |
) | |
func calc(number int, total *int) int { | |
if number == 0 { | |
return *total | |
} | |
*total += int(math.Pow(float64(number%10), float64(2))) | |
return calc(number/10, total) | |
} | |
func is_happy_number(number int) bool { | |
var i int | |
for i = 100; i > 0; i-- { | |
total := 0 | |
number = calc(number, &total) | |
if number == 1 { | |
return true | |
} | |
} | |
return false | |
} | |
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