mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 23:04:42 +01:00
rename directory
This commit is contained in:
35
02_even_fibonacci_numbers/main.go
Normal file
35
02_even_fibonacci_numbers/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
end := 4000000
|
||||
nums := GetFibonacciTill(end)
|
||||
fmt.Println("the fibonacci below 4000000 is", nums)
|
||||
sum := 0
|
||||
for _, n := range nums {
|
||||
if IsEvenValue(n) {
|
||||
sum += n
|
||||
}
|
||||
}
|
||||
fmt.Println("event-value sum of numbers in 4000000 is", sum)
|
||||
}
|
||||
|
||||
func GetFibonacciTill(n int) []int {
|
||||
var ret []int
|
||||
next := 1
|
||||
last := 1
|
||||
for next <= n {
|
||||
add := next
|
||||
ret = append(ret, add)
|
||||
next += last
|
||||
last = add
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func IsEvenValue(n int) bool {
|
||||
return n%2 == 0
|
||||
}
|
||||
Reference in New Issue
Block a user