Improved project structure.
This commit is contained in:
0
python/__init__.py
Normal file
0
python/__init__.py
Normal file
40
python/e023.py
Normal file
40
python/e023.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import math
|
||||
|
||||
|
||||
def get_proper_divisors(n):
|
||||
proper_divisors = set([1])
|
||||
for i in range(2, int(math.sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
proper_divisors.add(i)
|
||||
proper_divisors.add(n / i)
|
||||
return proper_divisors
|
||||
|
||||
|
||||
def is_abundant(n):
|
||||
return sum(get_proper_divisors(n)) > n
|
||||
|
||||
|
||||
def get_abundant_numbers_smaller(n):
|
||||
ret = []
|
||||
for i in range(1, n):
|
||||
if is_abundant(i):
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
|
||||
def is_sum_of_two_abundant(n, abundant_numbers):
|
||||
abundant_numbers_set = set(abundant_numbers)
|
||||
for a1 in abundant_numbers:
|
||||
if a1 > n:
|
||||
return False
|
||||
elif (n - a1) in abundant_numbers_set:
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
abundant_numbers = get_abundant_numbers_smaller(30000)
|
||||
cannot_be_written_as_sum_of_abundant = []
|
||||
for i in range(28129):
|
||||
if not is_sum_of_two_abundant(i, abundant_numbers):
|
||||
cannot_be_written_as_sum_of_abundant.append(i)
|
||||
print(sum(cannot_be_written_as_sum_of_abundant))
|
||||
2
python/e024.py
Normal file
2
python/e024.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from itertools import permutations
|
||||
print("".join(list(permutations("0123456789"))[1000000-1]))
|
||||
109
python/e025.py
Normal file
109
python/e025.py
Normal file
@@ -0,0 +1,109 @@
|
||||
from copy import deepcopy
|
||||
from itertools import islice
|
||||
|
||||
|
||||
def primes(n):
|
||||
""" Nice way to calculate primes. Should be fast. """
|
||||
l = range(2, n + 1)
|
||||
_l = []
|
||||
while True:
|
||||
p = l[0]
|
||||
if p * p > n:
|
||||
return _l + l
|
||||
l = [i for i in l if i % p != 0]
|
||||
_l.append(p)
|
||||
|
||||
|
||||
def calculate_decimal_places(numerator, denominator):
|
||||
numerator = (numerator - (numerator / denominator) * denominator) * 10
|
||||
digits = []
|
||||
while True:
|
||||
digit = numerator / denominator
|
||||
numerator = (numerator - digit * denominator) * 10
|
||||
digits.append(digit)
|
||||
if digits[-3:] == [0, 0, 0]:
|
||||
raise StopIteration
|
||||
yield digit
|
||||
|
||||
|
||||
def has_cycle(decimal_places, n):
|
||||
d = decimal_places
|
||||
return list(islice(d, 0, n)) == list(islice(d, 0, n)) and \
|
||||
list(islice(d, 0, n)) == list(islice(d, 0, n))
|
||||
|
||||
|
||||
def f_025_1():
|
||||
""" Second try. I realized that only primes must be
|
||||
checked. Therefore, my brute force approach worked. """
|
||||
l = []
|
||||
for d in primes(1000):
|
||||
for i in range(5, 10000):
|
||||
decimal_places = calculate_decimal_places(1, d)
|
||||
if has_cycle(decimal_places, i):
|
||||
l.append((i, d))
|
||||
break
|
||||
print(max(l))
|
||||
|
||||
|
||||
def calculate_cycle(numerator, denominator):
|
||||
numerator = (numerator - (numerator / denominator) * denominator) * 10
|
||||
remainders = set([])
|
||||
while True:
|
||||
digit = numerator / denominator
|
||||
remainder = (numerator - digit * denominator)
|
||||
if remainder in remainders:
|
||||
raise StopIteration
|
||||
remainders.add(remainder)
|
||||
numerator = remainder * 10
|
||||
yield digit
|
||||
|
||||
|
||||
def f_025_2():
|
||||
""" Understood trick with remembering remainder... """
|
||||
s = [(len(list(calculate_cycle(1, d))), d)
|
||||
for d in range(1, 1001)]
|
||||
print(max(s))
|
||||
|
||||
|
||||
def f_025_3():
|
||||
""" Only testing primes... """
|
||||
s = [(len(list(calculate_cycle(1, d))), d)
|
||||
for d in primes(10000)]
|
||||
print(max(s))
|
||||
|
||||
|
||||
f_025_3()
|
||||
|
||||
|
||||
#print([(find_cycle_count(calculate_decimal_places(1, d)), d)
|
||||
# for d in range(1, 100)])
|
||||
|
||||
#print(find_cycle_count(calculate_decimal_places(22, 7)))
|
||||
|
||||
#l = []
|
||||
#for d in range(1, 1000):
|
||||
# for i in range(5, 1000):
|
||||
# decimal_places = calculate_decimal_places(1, d)
|
||||
# if has_cycle_one_off(decimal_places, i):
|
||||
# l.append((i, d))
|
||||
# break
|
||||
# decimal_places = calculate_decimal_places(1, d)
|
||||
# if has_cycle(decimal_places, i):
|
||||
# l.append((i, d))
|
||||
# break
|
||||
|
||||
#def find_cycle_count(decimal_places):
|
||||
# cycles = []
|
||||
# for digit in decimal_places:
|
||||
# new_cycles = []
|
||||
# for cycle in cycles:
|
||||
# digits, length = cycle
|
||||
# if digits[0] == digit:
|
||||
# if len(digits[1:]) == 0:
|
||||
# return length
|
||||
# new_cycles.append((digits[1:], length))
|
||||
# new_cycles.append((digits + [digit], length + 1))
|
||||
# new_cycles.append(([digit], 1))
|
||||
# cycles = new_cycles
|
||||
|
||||
|
||||
38
python/e026.py
Normal file
38
python/e026.py
Normal file
@@ -0,0 +1,38 @@
|
||||
def primes(n):
|
||||
""" Nice way to calculate primes. Should be fast. """
|
||||
l = range(2, n + 1)
|
||||
_l = []
|
||||
while True:
|
||||
p = l[0]
|
||||
if p * p > n:
|
||||
return _l + l
|
||||
l = [i for i in l if i % p != 0]
|
||||
_l.append(p)
|
||||
|
||||
|
||||
def produce_prime(a, b, n, primes):
|
||||
x = n*n + a*n + b
|
||||
return x in primes
|
||||
|
||||
|
||||
|
||||
def f_027():
|
||||
""" n^2 + a*n + b
|
||||
1) b must be prime
|
||||
"""
|
||||
p6 = set(primes(1000000))
|
||||
p3 = primes(1000)
|
||||
options = [(a, b)
|
||||
for a in range(1, 1000, 2)
|
||||
for b in p3]
|
||||
|
||||
print(len(options))
|
||||
for n in range(100):
|
||||
options = [(a, b)
|
||||
for a, b in options
|
||||
if produce_prime(a, b, n, p6)]
|
||||
print(options)
|
||||
print(len(options))
|
||||
|
||||
|
||||
f_027()
|
||||
Reference in New Issue
Block a user