Skip to content

Commit

Permalink
git-svn-id: svn+ssh://svn.liveoakinteractive.com/var/lib/svnroot/eule…
Browse files Browse the repository at this point in the history
…r/trunk@80 735d8fd3-3d48-0410-ad09-b9318371deed
  • Loading branch information
jason committed Apr 7, 2008
1 parent f8a2613 commit 494c9d0
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions jason/MyMath.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import operator
import math
import copy

def primes(n):
"""Returns the primes up to n."""
Expand Down Expand Up @@ -43,7 +44,7 @@ def factors(num):

def primeFactors(num):
"""Determine the prime factors of a number."""
factors = {}
factors = {}
i = 2
while i <= num**0.5:
if num % i == 0:
Expand All @@ -54,7 +55,31 @@ def primeFactors(num):
else:
i += 2
factors[num] = factors.get(num, 0) + 1


return factors

def fastFactors(num):
"""Determine the factors of a number."""
factors = {}
i = 2
while i <= num:
if num % i == 0:
factors_copy = copy.copy(factors)
for factor in factors_copy:
factors[factor * i] = True

factors[i] = True
num /= i
elif i == 2:
i = 3
else:
i += 2

factors_copy = copy.copy(factors)
for factor in factors_copy:
factors[factor * i] = True
factors[1] = True

return factors

def divisors(num):
Expand All @@ -75,4 +100,7 @@ def abundants(num):
if num >= 12:
return [i for i in xrange(12, num + 1) if isAbundant(i)]
else:
return []
return []

print factors(1525)
print fastFactors(1525).keys()

0 comments on commit 494c9d0

Please sign in to comment.