Created
October 20, 2020 08:30
-
-
Save fatred/24b7f59e03d078245caad43ff59eb0e9 to your computer and use it in GitHub Desktop.
Go solution to the even-number challenge in Go Essential Tranining
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" | |
) | |
// how many even ended numbers result from multiplying two four digit numbers | |
func main() { | |
// setup a new counter | |
even_numbers := 0 | |
// setup a loop with x as a seed | |
for x := 1000; x <=9999; x++ { | |
//iterate over that loop with y incrementing as well | |
for y := x; y <= 9999; y++ { | |
// work out the answer | |
result := x*y | |
result_str := fmt.Sprintf("%d", result) | |
// check if the answer has the same start and end digit | |
if result_str[0] == result_str[len(result_str)-1] { | |
// increment the counter | |
even_numbers++ | |
} | |
} | |
} | |
// at the end, print out the count... | |
fmt.Printf("We found %d even-ended numbers", even_numbers) | |
fmt.Println("") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment