Skip to content

Commit

Permalink
add files with new naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Jain committed Feb 5, 2013
1 parent d5e0d7f commit 675091c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
Binary file added .test.py.swp
Binary file not shown.
Binary file added .three.py.swp
Binary file not shown.
11 changes: 11 additions & 0 deletions pe1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def sumn(n):
num = 0
while num < n:
if ((num%3)==0) or ((num%5)==0):
print num
yield num
num += 1

threefivesum = sum(sumn(1000))

print threefivesum
15 changes: 15 additions & 0 deletions pe2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fibs = [1, 1, 2]
sum = 0

while fibs.last < 4_000_000 do

if fibs.last.even?
sum += fibs.last
end

fibs << (fibs[-2] + fibs[-1])

fibs.shift
end

puts sum
31 changes: 31 additions & 0 deletions pe3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# average time: 59s for 600851475143
# #terrible #wasteful #inefficient #naive

def get_lpf(num):
ufl = long(num/2 + 1) # upper factor limit
lpf = test_factors(num, long(num/2 + 1) # largest prime factor
print("Largest prime factor: {}".format(lpf))

def test_factors(num, ufl):
i = 1
while i > ufl:
if(num % i == 0):
factor = long(num / i)
prime = long(check_prime(factor))
if prime is True:
return factor
i += 1
else:
return 1

def check_prime(num):
i = 2
while i < (num/2 + 1):
if(num % i == 0):
return False
i += 1
else:
return True

# reason for while loops: integers got too big for range() and xrange()
# also tried recursion but exceeded recursion limit
15 changes: 15 additions & 0 deletions pe3e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# average time: 51.5ns for 600851475143
# basic sieves implementation results in 9 orders of magnitude improvement

num = 600851475143 # input("Number: ")

d = 2
factors = []

while num > 1:
while(num % d == 0):
factors.append(d)
num = num / d
d += 1

print(factors[-1])

0 comments on commit 675091c

Please sign in to comment.