-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add files with new naming convention
- Loading branch information
Steve Jain
committed
Feb 5, 2013
1 parent
d5e0d7f
commit 675091c
Showing
6 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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]) |