Created
January 11, 2013 12:59
-
-
Save reedobrien/4510457 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 nextPrime() func() int { | |
last := 3 | |
return func() int { | |
for isprime(last) == false { | |
last = last + 2 | |
} | |
prime := last | |
last = last + 2 | |
return prime | |
} | |
} | |
func isprime(x int) bool { | |
var i float64 | |
for i = 3; i <= math.Sqrt(float64(x)); i += 2 { | |
if math.Mod(float64(x), float64(i)) == 0 { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
var total int64 = 2 | |
pg := nextPrime() | |
num := pg() | |
for num < 2000000 { | |
total = total + int64(num) | |
num = pg() | |
} | |
fmt.Println(total) | |
} |
This file contains hidden or 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
def primgen(maximum=2000000): | |
yield 2 | |
yield 3 | |
yield 5 | |
yield 7 | |
x = 9 | |
while x < maximum: | |
if isprime(x): | |
yield x | |
x += 2 | |
def isprime(n): | |
if n == 2: | |
return 1 | |
if n % 2 == 0: | |
return 0 | |
max = n ** 0.5 + 1 | |
i = 3 | |
while i <= max: | |
if n % i == 0: | |
return False | |
i += 2 | |
return True | |
if __name__ == "__main__": | |
primes = [x for x in primgen()] | |
print len(primes), sum(primes) |
This file contains hidden or 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 ( | |
"errors" | |
"fmt" | |
) | |
func makeTriple(m, n int) (a, b, c int, err error) { | |
if m > n { | |
err := errors.New("m must be less than n") | |
return 0, 0, 0, err | |
} | |
a = n*n - m*m | |
b = 2 * n * m | |
c = n*n + m*m | |
return a, b, c, nil | |
} | |
func main() { | |
done := false | |
m, n := 1, 2 | |
for done == false { | |
a, b, c, err := makeTriple(m, n) | |
if err != nil { | |
panic(err) | |
} | |
if a+b+c == 1000 { | |
fmt.Println(a * b * c) | |
done = true | |
} else { | |
n = n + 1 | |
} | |
if a+b+c > 1000 { | |
m = m + 1 | |
n = m + 2 | |
} | |
} | |
} |
This file contains hidden or 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
def make_triple(m, n): | |
assert(m < n) | |
a = n * n - m * m | |
b = 2 * n * m | |
c = n * n + m * m | |
return a, b, c | |
def main(): | |
done = False | |
m, n = 1, 2 | |
while not done: | |
a, b, c = make_triple(m, n) | |
if a + b + c == 1000: | |
print(a * b * c) | |
done = True | |
else: | |
n = n + 1 | |
if a + b + c > 1000: | |
m = m + 1 | |
n = m + 2 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment