project-euler-100

[RADIOACTIVE] solutions to the first 100 challenges of project euler
git clone git://git.figbert.com/project-euler-100.git
Log | Files | Refs | README

commit c09af77451af2d453798017abf47358e7d2d5ec7
parent 972f8c910df107ffa4ac6f5dc8ff77d65ace48c8
Author: therealFIGBERT <figbertwelner@gmail.com>
Date:   Sat,  8 Feb 2020 21:23:05 -0800

:art: Enforce PEP8 style guidelines for variable names and line lengths

Diffstat:
Mproblem001.py | 20++++++++++----------
Mproblem002.py | 29+++++++++++++++--------------
Mproblem003.py | 16+++++++---------
Mproblem004.py | 10++++++++--
Mproblem005.py | 5++++-
Mproblem006.py | 17++++++++++-------
Mproblem007.py | 26+++++++++++++-------------
Mproblem009.py | 5++++-
Mproblem010.py | 16++++++++--------
Mproblem011.py | 54+++++++++++++++++++++++++++++-------------------------
Mproblem012.py | 15+++++++++++----
Mproblem013.py | 5++++-
Mproblem014.py | 6++++--
13 files changed, 127 insertions(+), 97 deletions(-)

diff --git a/problem001.py b/problem001.py @@ -4,18 +4,18 @@ # language = "Python" # dateCompleted = "20/01/2020" -def multOfThreeOrFive(num): +def mult_of_three_or_five(num): return num % 3 == 0 or num % 5 == 0 -def sumOfMultiplesOfThreeOrFive(num): - lstOfMults = [] - numStep = num - 1 - while numStep > 0: - if multOfThreeOrFive(numStep): - lstOfMults.append(numStep) - numStep -= 1 - return sum(lstOfMults) +def sum_of_multiples_of_three_or_five(num): + list_of_multiples = [] + num_step = num - 1 + while num_step > 0: + if mult_of_three_or_five(num_step): + list_of_multiples.append(num_step) + num_step -= 1 + return sum(list_of_multiples) if __name__ == "__main__": - answer = sumOfMultiplesOfThreeOrFive(1000) + answer = sum_of_multiples_of_three_or_five(1000) print("The sum of all the multiples of 3 or 5 below 1000 is %s" % answer) diff --git a/problem002.py b/problem002.py @@ -4,23 +4,24 @@ # language = "Python" # dateCompleted = "20/01/2020" -def fib(limit): - lst = [1,2] - while lst[-1] <= limit: - lst.append(lst[-2] + lst[-1]) - return lst +def fibonacci(limit): + list_ = [1,2] + while list_[-1] <= limit: + list_.append(list_[-2] + list_[-1]) + return list_ -def sumOfEvensInList(srcList): - lst = [] - for i in srcList: +def sum_of_evens_in_list(src_list): + list_ = [] + for i in src_list: if i % 2 == 0: - lst.append(i) - return sum(lst) + list_.append(i) + return sum(list_) if __name__ == "__main__": - fibUntil4Mil = fib(4000000) - answer = sumOfEvensInList(fibUntil4Mil) + fibonacci_until_4_mil = fibonacci(4000000) + answer = sum_of_evens_in_list(fibonacci_until_4_mil) print(( - "By considering the terms in the Fibonacci sequence whose values do not " - "exceed four million, the sum of the even-valued terms is {}".format(answer) + "By considering the terms in the Fibonacci sequence whose values do " + "not exceed four million, the sum of the even-valued terms is " + "{}".format(answer) )) diff --git a/problem003.py b/problem003.py @@ -5,24 +5,22 @@ # dateCompleted = "21/01/2020" import math -def primeFactors(num): +def prime_factors(num): primes = [] - factorLimit = int(math.sqrt(num)) + 1 - + factor_limit = int(math.sqrt(num)) + 1 while num % 2 == 0: primes.append(2) num = num / 2 - - for i in range(3, factorLimit, 2): + for i in range(3, factor_limit, 2): while num % i == 0: primes.append(i) num = num / i - if num > 2: primes.append(num) - return primes if __name__ == "__main__": - answer = max(primeFactors(600851475143)) - print("The largest prime factor of the number 600851475143 is %s" % answer) + answer = max(prime_factors(600851475143)) + print( + "The largest prime factor of the number 600851475143 is %s" % answer + ) diff --git a/problem004.py b/problem004.py @@ -5,5 +5,11 @@ # dateCompleted = "21/01/2020" if __name__ == "__main__": - allPossibleCombos = [a*b for a in range(999, 99, -1) for b in range(999, 99, -1) if int(str(a*b)[::-1]) == a*b] - print("The largest palindrome made from the product of two 3-digit numbers is %s" % max(allPossibleCombos)) + all_possible_combos = [ + a*b for a in range(999, 99, -1) for b in range(999, 99, -1) + if int(str(a*b)[::-1]) == a*b + ] + print(( + "The largest palindrome made from the product of two 3-digit " + "numbers is %s" % max(all_possible_combos) + )) diff --git a/problem005.py b/problem005.py @@ -12,4 +12,7 @@ if __name__ == "__main__": if (answer * k) % i == 0: answer *= k break - print("The smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is %s" % answer) + print(( + "The smallest positive number that is evenly divisible by all " + "of the numbers from 1 to 20 is %s" % answer + )) diff --git a/problem006.py b/problem006.py @@ -5,11 +5,14 @@ # dateCompleted = "24/01/2020" if __name__ == "__main__": - sumOfTheSquares = 0 - squareOfTheSum = 0 + sum_of_the_squares = 0 + square_of_the_sum = 0 for i in range(1, 101): - sumOfTheSquares += i**2 - squareOfTheSum += i - squareOfTheSum = squareOfTheSum**2 - answer = squareOfTheSum - sumOfTheSquares - print("The difference between the sum of the squares of the first one hundred natural numbers and the square of the sum is %s" % answer) + sum_of_the_squares += i**2 + square_of_the_sum += i + square_of_the_sum = square_of_the_sum**2 + answer = square_of_the_sum - sum_of_the_squares + print(( + "The difference between the sum of the squares of the first one " + "hundred natural numbers and the square of the sum is %s" % answer + )) diff --git a/problem007.py b/problem007.py @@ -4,21 +4,21 @@ # language = "Python" # dateCompleted = "24/01/2020" -def sieveOfEratosthenes(limit): +def sieve_of_eratosthenes(limit): prime = [True for _ in range(limit+1)] - p = 2 - while (p * p <= limit): - if (prime[p] == True): - for i in range(p * 2, limit + 1, p): + prime_pos = 2 + while (prime_pos**2 <= limit): + if (prime[prime_pos] == True): + for i in range(prime_pos * 2, limit + 1, prime_pos): prime[i] = False - p += 1 + prime_pos += 1 prime[0:2] = [False, False] - return [p for p in range(limit + 1) if prime[p]] + return [prime_pos for prime_pos in range(limit + 1) if prime[prime_pos]] if __name__ == "__main__": - searchRange = 1000 - returnValue = sieveOfEratosthenes(searchRange) - while len(returnValue) < 10001: - searchRange += 1000 - returnValue = sieveOfEratosthenes(searchRange) - print("The 10,001st prime number is %s" % returnValue[10000]) + search_range = 1000 + return_value = sieve_of_eratosthenes(search_range) + while len(return_value) < 10001: + search_range += 1000 + return_value = sieve_of_eratosthenes(search_range) + print("The 10,001st prime number is %s" % return_value[10000]) diff --git a/problem009.py b/problem009.py @@ -15,4 +15,7 @@ if __name__ == "__main__": for (i, k) in [(a, b) for a in range(500) for b in range(500) if a < b]: answer = triplet(i, k) if answer: - print(f"The product of abc, where:\n\ta < b < c,\n\ta2 + b2 = c2,\n\ta + b + c = 1000\nis {answer}") + print(( + "The product of abc, where:\n\ta < b < c,\n\ta2 + b2 = c2,\n" + f"\ta + b + c = 1000\nis {answer}" + )) diff --git a/problem010.py b/problem010.py @@ -4,17 +4,17 @@ # language = "Python" # dateCompleted = "28/01/2020" -def sieveOfEratosthenes(limit): +def sieve_of_eratosthenes(limit): prime = [True for _ in range(limit+1)] - p = 2 - while (p * p <= limit): - if (prime[p] == True): - for i in range(p * 2, limit + 1, p): + prime_pos = 2 + while (prime_pos**2 <= limit): + if (prime[prime_pos] == True): + for i in range(prime_pos * 2, limit + 1, prime_pos): prime[i] = False - p += 1 + prime_pos += 1 prime[0:2] = [False, False] - return [p for p in range(limit + 1) if prime[p]] + return [prime_pos for prime_pos in range(limit + 1) if prime[prime_pos]] if __name__ == "__main__": - primes = sieveOfEratosthenes(2000000) + primes = sieve_of_eratosthenes(2000000) print(f"The sum of all the primes below two million is {sum(primes)}") diff --git a/problem011.py b/problem011.py @@ -30,59 +30,63 @@ STRVAL = """ for line in STRVAL.splitlines(): VAL.append([int(number) for number in line.split()]) -def largestVertical(lst): +def largest_vertical(lst): largest = 0 for i, _ in enumerate(lst): if i + 4 <= len(lst): for k, _ in enumerate(lst[i]): - vertSum = 1 + vertical_sum = 1 for j in range(4): - vertSum *= lst[i+j][k] - largest = vertSum if vertSum > largest else largest + vertical_sum *= lst[i+j][k] + largest = vertical_sum if vertical_sum > largest else largest return largest -def largestHorizontal(lst): +def largest_horizontal(lst): largest = 0 for i, _ in enumerate(lst): for k, _ in enumerate(lst[i]): if k + 4 <= len(lst[i]): - horizSum = 1 + horizontal_sum = 1 for j in range(4): - horizSum *= lst[i][k+j] - largest = horizSum if horizSum > largest else largest + horizontal_sum *= lst[i][k+j] + if horizontal_sum > largest: + largest = horizontal_sum return largest -def largestDiagonal(lst): +def largest_diagonal(lst): largest = 0 for i, _ in enumerate(lst): for k, _ in enumerate(lst): if i - 3 >= 0: if k - 3 <= 0: - upLeft = 1 + up_left = 1 for j in range(4): - upLeft *= lst[i-j][i-k] - largest = upLeft if upLeft > largest else largest + up_left *= lst[i-j][i-k] + largest = up_left if up_left > largest else largest if k + 4 <= len(lst[i]): - upRight = 1 + up_right = 1 for j in range(4): - upRight *= lst[i-j][k+j] - largest = upRight if upRight > largest else largest + up_right *= lst[i-j][k+j] + largest = up_right if up_right > largest else largest if i + 4 <= len(lst[i]): if k - 3 <= 0: - downLeft = 1 + down_left = 1 for j in range(4): - downLeft *= lst[i+j][k-j] - largest = downLeft if downLeft > largest else largest + down_left *= lst[i+j][k-j] + largest = down_left if down_left > largest else largest if k + 4 <= len(lst[i]): - downRight = 1 + down_right = 1 for j in range(4): - downRight *= lst[i+j][k+j] - largest = downRight if downRight > largest else largest + down_right *= lst[i+j][k+j] + largest = down_right if down_right > largest else largest return largest if __name__ == "__main__": - diagonal = largestDiagonal(VAL) - horizontal = largestHorizontal(VAL) - vertical = largestVertical(VAL) + diagonal = largest_diagonal(VAL) + horizontal = largest_horizontal(VAL) + vertical = largest_vertical(VAL) answer = max([diagonal, horizontal, vertical]) - print(f"The greatest product of four adjacent numbers in the same direction in the 20×20 grid is {answer}") + print(( + "The greatest product of four adjacent numbers in the same " + f"direction in the 20×20 grid is {answer}" + )) diff --git a/problem012.py b/problem012.py @@ -6,19 +6,26 @@ from functools import reduce from math import sqrt -def moreThanFiveHundredFactors(num): - factors = len(list(reduce(list.__add__, ([i, num//i] for i in range(1, int(sqrt(num)) + 1) if num % i == 0)))) +def more_than_five_hundred_factors(num): + factors = len(list(reduce( + list.__add__, ( + [i, num//i] for i in range(1, int(sqrt(num)) + 1) if num % i == 0 + ) + ))) return num if factors > 500 else False def triangle(pos): val = 0 for i in range(1, pos + 1): val += i - return moreThanFiveHundredFactors(val) + return more_than_five_hundred_factors(val) if __name__ == "__main__": answer = 2 while not triangle(answer): answer += 1 answer = triangle(answer) - print(f"The value of the first triangle number to have over five hundred divisors is {answer}") + print(( + "The value of the first triangle number to have over " + f"five hundred divisors is {answer}" + )) diff --git a/problem013.py b/problem013.py @@ -112,4 +112,7 @@ for line in STRVAL.splitlines(): if __name__ == "__main__": answer = str(sum(VAL))[0:10] - print(f"The first ten digits of the sum of the provided one-hundred 50-digit numbers are {answer}") + print(( + "The first ten digits of the sum of the provided " + f"one-hundred 50-digit numbers are {answer}" + )) diff --git a/problem014.py b/problem014.py @@ -12,7 +12,6 @@ def collatz(num): else: num = (3*num) + 1 sequence += 1 - return sequence if __name__ == "__main__": @@ -23,4 +22,7 @@ if __name__ == "__main__": answer[1] = potential[0] answer[2] = potential[1] answer[0] = answer[0] - 1 - print(f"The starting number, under one million, that produces the longest chain from the given iterative sequence is {answer[1]}") + print(( + "The starting number, under one million, that produces the " + f"longest chain from the given iterative sequence is {answer[1]}" + ))