Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Solving 12 using a generator in a for loop for great justice

  • Loading branch information...
commit 7908b7924bab110cbcf812cb5d6d3b32e810b3b3 1 parent 1de95a8
@coleifer authored
Showing with 36 additions and 0 deletions.
  1. +36 −0 go/problem12.go
View
36 go/problem12.go
@@ -0,0 +1,36 @@
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+func triangle() func() int {
+ i, sum := 0, 0
+ return func() int {
+ i += 1
+ sum += i
+ return sum
+ }
+}
+
+func count_factors(n int) int {
+ sqrt := int(math.Ceil(math.Sqrt(float64(n))))
+ count := 0
+ for i := 1; i <= sqrt; i++ {
+ if n % i == 0 {
+ // increase by 2 since there's another
+ // factor on the opposite side of the sqrt
+ count += 2
+ }
+ }
+ return count
+}
+
+func main() {
+ var sum int
+ triangle_gen := triangle()
+ for sum = 1; count_factors(sum) < 500; sum = triangle_gen() {
+ }
+ fmt.Printf("%v\n", sum)
+}
Please sign in to comment.
Something went wrong with that request. Please try again.