Problems 1 - 6
This commit is contained in:
commit
2a762f8a9a
7
0001.py
Normal file
7
0001.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
result = 0
|
||||||
|
|
||||||
|
for num in range(1000):
|
||||||
|
if (num % 5 == 0 or num % 3 == 0):
|
||||||
|
result = result + num
|
||||||
|
|
||||||
|
print(result)
|
||||||
14
0002.py
Normal file
14
0002.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
previous = 1
|
||||||
|
current = 1
|
||||||
|
result = 0
|
||||||
|
|
||||||
|
while (current <= 4000000):
|
||||||
|
if (current % 2 == 0):
|
||||||
|
result = result + current
|
||||||
|
|
||||||
|
next = current + previous
|
||||||
|
previous = current
|
||||||
|
current = next
|
||||||
|
|
||||||
|
print(result)
|
||||||
57
0003.py
Normal file
57
0003.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
from functools import reduce
|
||||||
|
target = 600851475143
|
||||||
|
|
||||||
|
# list = []
|
||||||
|
|
||||||
|
# for num in range(1, int(target / 2)):
|
||||||
|
# print(num)
|
||||||
|
# if (target % num == 0):
|
||||||
|
# list.append(num)
|
||||||
|
|
||||||
|
# 2^x - 1 6k - 1
|
||||||
|
# print(list)
|
||||||
|
factors = []
|
||||||
|
def findLargestPrimeFactor(n):
|
||||||
|
primeFactor = 1
|
||||||
|
factors.append(primeFactor)
|
||||||
|
print('appended')
|
||||||
|
i = 2
|
||||||
|
|
||||||
|
while i <= n / i:
|
||||||
|
if n % i == 0:
|
||||||
|
factors.append(i)
|
||||||
|
primeFactor = i
|
||||||
|
n = int(n / i)
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
if primeFactor < n:
|
||||||
|
primeFactor = n
|
||||||
|
|
||||||
|
return primeFactor
|
||||||
|
|
||||||
|
def findPrimeFactors(n):
|
||||||
|
factors = []
|
||||||
|
|
||||||
|
i = 2
|
||||||
|
|
||||||
|
while i <= n / i:
|
||||||
|
if n % i == 0:
|
||||||
|
factors.append(i)
|
||||||
|
n = int(n / i)
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
factors.append(n)
|
||||||
|
|
||||||
|
return factors
|
||||||
|
|
||||||
|
|
||||||
|
target = 600851475143
|
||||||
|
|
||||||
|
# print(findLargestPrimeFactor(target))
|
||||||
|
factors = findPrimeFactors(target)
|
||||||
|
print(factors)
|
||||||
|
result = reduce((lambda x, y: x * y), factors)
|
||||||
|
print(result)
|
||||||
|
print(findPrimeFactors(48))
|
||||||
31
0004.py
Normal file
31
0004.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
def is_palindrome(n):
|
||||||
|
st = str(n)
|
||||||
|
|
||||||
|
for i in range(0, int(len(st) / 2)):
|
||||||
|
if (st[i] != st[len(st) - 1 - i]):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def palindrome():
|
||||||
|
largest = 0
|
||||||
|
i = 999
|
||||||
|
|
||||||
|
while(i > 0):
|
||||||
|
j = 999
|
||||||
|
|
||||||
|
while(j > 0):
|
||||||
|
result = i * j
|
||||||
|
|
||||||
|
if (largest >= result):
|
||||||
|
break;
|
||||||
|
elif (is_palindrome(result)):
|
||||||
|
largest = result
|
||||||
|
|
||||||
|
j -= 1
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
return largest
|
||||||
|
# print(is_palindrome(9119))
|
||||||
|
print(palindrome())
|
||||||
49
0005.py
Normal file
49
0005.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
from collections import Counter
|
||||||
|
from functools import reduce
|
||||||
|
|
||||||
|
def findPrimeFactors(n):
|
||||||
|
factors = []
|
||||||
|
|
||||||
|
i = 2
|
||||||
|
|
||||||
|
while i <= n / i:
|
||||||
|
if n % i == 0:
|
||||||
|
factors.append(i)
|
||||||
|
n = int(n / i)
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
factors.append(n)
|
||||||
|
|
||||||
|
return factors
|
||||||
|
|
||||||
|
def divisible(rng):
|
||||||
|
factors = None
|
||||||
|
|
||||||
|
for num in rng:
|
||||||
|
nums = findPrimeFactors(num)
|
||||||
|
count = Counter(nums)
|
||||||
|
|
||||||
|
if (factors == None):
|
||||||
|
factors = count
|
||||||
|
else:
|
||||||
|
for key, value in count.items():
|
||||||
|
if (not factors[key] or value > factors[key]):
|
||||||
|
factors[key] = value
|
||||||
|
|
||||||
|
return factors
|
||||||
|
# max_num = max(rng)
|
||||||
|
|
||||||
|
|
||||||
|
factors = divisible(range(1, 21))
|
||||||
|
|
||||||
|
# print(factors)
|
||||||
|
|
||||||
|
# reduce(def fnc(value, key): print(value), factors.items())
|
||||||
|
|
||||||
|
result = 1
|
||||||
|
for key, value in factors.items():
|
||||||
|
result *= key ** value
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
|
||||||
19
0006.py
Normal file
19
0006.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from functools import reduce
|
||||||
|
|
||||||
|
def square_sum(rng):
|
||||||
|
sum = reduce(lambda x, y: x + y, rng)
|
||||||
|
|
||||||
|
return sum ** 2
|
||||||
|
|
||||||
|
def sum_squares(rng):
|
||||||
|
squares = list(map(lambda x: x**2, rng))
|
||||||
|
|
||||||
|
return reduce(lambda x, y: x + y, squares)
|
||||||
|
|
||||||
|
lst = list(range(1, 101))
|
||||||
|
result1 = sum_squares(lst)
|
||||||
|
result2 = square_sum(lst)
|
||||||
|
|
||||||
|
print(result2 - result1)
|
||||||
|
|
||||||
|
# print(result)
|
||||||
Loading…
Reference in New Issue
Block a user