Permalink
Browse files

Solved prob 2 using itertools and a user-defined module called eulert…

…ools
  • Loading branch information...
1 parent 7b654bd commit 853b0974cc24c69c58d9b96cdae6dd96cf73a33e @annelagang committed May 12, 2012
Showing with 219 additions and 0 deletions.
  1. +26 −0 PE02.py
  2. +193 −0 eulertools.py
View
26 PE02.py
@@ -0,0 +1,26 @@
+'''Problem 2
+Each new term in the Fibonacci sequence is generated by adding the previous two terms.
+By starting with 1 and 2, the first 10 terms will be:
+
+1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
+
+By considering the terms in the Fibonacci sequence whose values do not exceed
+four million, find the sum of the even-valued terms.'''
+#Answer: 4613732
+
+
+import time, itertools
+from eulertools import fib_gen
+
+
+def solve():
+ even_nums = (x for x in fib_gen() if x % 2 == 0)
+ print sum(list(itertools.takewhile(lambda x: x < 4000000, even_nums)))
+
+
+if __name__ == '__main__':
+ start = time.clock()
+ solve()
+ print 'Time elapsed:', (time.clock() - start)
+
+
View
@@ -0,0 +1,193 @@
+def is_even(n):
+ return n % 2 == 0
+
+
+def is_palindrome(s):
+ if s == s[::-1]:
+ return True
+ return False
+
+
+def is_prime(n):
+ # n**0.5 == math.sqrt(n)
+ n = abs(n)
+ for x in range(2, n**0.5 + 1):
+ if n % x == 0:
+ return False
+ return True
+
+
+def nth(gen, n):
+ '''Returns nth number from a given generator.'''
+ for i in xrange(n - 1):
+ gen.next()
+ return gen.next()
+
+
+#source: http://www.siafoo.net/snippet/145
+def is_triangle(n):
+ x = (math.sqrt(8*n + 1) - 1) / 2
+ if x - int(x) > 0: # if x is not an integer
+ return False
+ return int(x)
+
+
+def prime_gen():
+ yield 2
+ yield 3
+ i = 5
+ while True:
+ if is_prime(i):
+ yield i
+ i += 2
+
+
+def fib_gen():
+ a = b = 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def triangle_num_gen(n = 1):
+ while True:
+ yield n * (n + 1) / 2
+ n += 1
+
+
+#using Binet's formula
+#source: http://en.literateprograms.org/Fibonacci_numbers_(Python)#Recursion_with_memoization
+def fib(n):
+ return int(round((phi**n - (1-phi)**n) / 5**0.5))
+
+
+#source: http://stackoverflow.com/questions/3939660/sieve-of-eratosthenes-finding-primes-python
+#author: Piet Delport
+def primes_sieve(limit):
+ a = [True] * limit # Initialize the primality list
+ a[0] = a[1] = False
+
+ for (i, isprime) in enumerate(a):
+ if isprime:
+ yield i
+ for n in xrange(i, limit, i): # Mark factors non-prime
+ a[n] = False
+
+
+#source: http://blog.dhananjaynene.com/2009/01/2009-is-not-a-prime-number-a-python-program-to-compute-factors/
+def prime_factors(n):
+ #yield 1
+ i = 2
+ limit = n**0.5
+ while i <= limit:
+ if n % i == 0:
+ yield i
+ n = n / i
+ limit = n**0.5
+ else:
+ i += 1
+ if n > 1:
+ yield n
+
+
+from collections import Counter
+def gcm(num1, num2):
+ gcm = list((Counter([x for x in prime_factors(num1)])
+ & Counter([x for x in prime_factors(num2)])).elements())
+
+ return reduce(lambda prod1, prod2: prod1 * prod2, gcm)
+
+
+def factor_count_dict(prime_factors):
+ '''Returns a dictionary mapping of the prime factors of a number and
+it's frequency.'''
+ dict_factor_count = {}
+ for prime in prime_factors:
+ dict_factor_count[prime] = dict_factor_count.get(prime,0) + 1
+
+ return dict_factor_count
+
+
+#formula: (a+1)*(b+1)*...(k+1)
+#source: http://www.manhattangmat.com/forums/is-there-a-formula-to-calculate-the-number-of-factors-t2500.html
+#author: David Pollack
+def factor_count(dict_factor_list):
+ dict = factor_count_dict(dict_factor_list)
+
+ total_count = 1
+
+ for count in dict.values():
+ total_count *= count + 1
+
+ return total_count
+
+
+#formula: (n / 10) + (n % 10) * 100000
+#source: http://projecteuler.net/thread=35;page=2
+#author: jorgbrown
+def rotate_digits(n):
+ num = n
+ count = len(str(num))
+ power = count - 1
+
+ rotated = [num]
+ #start at 1 to avoid rotating back to the given num
+ ctr = 1
+ while ctr < count:
+ num = (num / 10) + (num % 10) * (10 ** power)
+ rotated.append(num)
+ ctr += 1
+
+ #return distinct (esp for 11)
+ return set(rotated)
+
+
+def break_into_digits(n):
+ '''Returns the digits of a base 10 number.'''
+ count = len(str(n))
+ power = count - 1
+
+ digits = []
+ ctr = 0
+ while ctr < count:
+ num = n / (10 ** power)
+ if ctr != 0:
+ num = num % 10
+
+ digits.append(num)
+ power -= 1
+ ctr += 1
+
+ return digits
+
+
+#source: http://stackoverflow.com/questions/1408823/int-and-string-parsing
+#author: Charles Ma
+#modified to be a generator
+def extract_digits_reversed(num):
+ while num > 0:
+ yield num % 10
+ num /= 10
+
+
+def collatz_gen(num):
+ n = num
+ yield n
+ while n != 1:
+ if is_even(n):
+ n = n / 2
+ else:
+ n = 3 * n + 1
+
+ yield n
+
+
+def is_leap_year(year):
+ if year % 400 == 0:
+ return True
+ elif year % 100 == 0:
+ return False
+ elif year % 4 == 0:
+ return True
+ else:
+ return False

0 comments on commit 853b097

Please sign in to comment.