Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 7908b7924bab110cbcf812cb5d6d3b32e810b3b3 1 parent 1de95a8
Charles Leifer authored

Showing 1 changed file with 36 additions and 0 deletions. Show diff stats Hide diff stats

  1. 36  go/problem12.go
36  go/problem12.go
... ...
@@ -0,0 +1,36 @@
  1
+package main
  2
+
  3
+import (
  4
+    "fmt"
  5
+    "math"
  6
+)
  7
+
  8
+func triangle() func() int {
  9
+    i, sum := 0, 0
  10
+    return func() int {
  11
+        i += 1
  12
+        sum += i
  13
+        return sum
  14
+    }
  15
+}
  16
+
  17
+func count_factors(n int) int {
  18
+    sqrt := int(math.Ceil(math.Sqrt(float64(n))))
  19
+    count := 0
  20
+    for i := 1; i <= sqrt; i++ {
  21
+        if n % i == 0 {
  22
+            // increase by 2 since there's another
  23
+            // factor on the opposite side of the sqrt
  24
+            count += 2
  25
+        }
  26
+    }
  27
+    return count
  28
+}
  29
+
  30
+func main() {
  31
+    var sum int
  32
+    triangle_gen := triangle()
  33
+    for sum = 1; count_factors(sum) < 500; sum = triangle_gen() {
  34
+    }
  35
+    fmt.Printf("%v\n", sum)
  36
+}

0 notes on commit 7908b79

Please sign in to comment.
Something went wrong with that request. Please try again.