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 972f8c910df107ffa4ac6f5dc8ff77d65ace48c8
parent c550d42f95ae65c1020ca262e14beb9304221036
Author: therealFIGBERT <figbertwelner@gmail.com>
Date:   Sat,  8 Feb 2020 14:58:12 -0800

:recycle: Add __main__ check to all files

Diffstat:
Mproblem001.py | 5+++--
Mproblem002.py | 13+++++++------
Mproblem003.py | 5+++--
Mproblem004.py | 5+++--
Mproblem005.py | 17+++++++++--------
Mproblem006.py | 17+++++++++--------
Mproblem007.py | 11++++++-----
Mproblem008.py | 5+++--
Mproblem009.py | 9+++++----
Mproblem010.py | 5+++--
Mproblem011.py | 11++++++-----
Mproblem012.py | 11++++++-----
Mproblem013.py | 5+++--
13 files changed, 66 insertions(+), 53 deletions(-)

diff --git a/problem001.py b/problem001.py @@ -16,5 +16,6 @@ def sumOfMultiplesOfThreeOrFive(num): numStep -= 1 return sum(lstOfMults) -answer = sumOfMultiplesOfThreeOrFive(1000) -print("The sum of all the multiples of 3 or 5 below 1000 is %s" % answer) +if __name__ == "__main__": + answer = sumOfMultiplesOfThreeOrFive(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 @@ -17,9 +17,10 @@ def sumOfEvensInList(srcList): lst.append(i) return sum(lst) -fibUntil4Mil = fib(4000000) -answer = sumOfEvensInList(fibUntil4Mil) -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) -)) +if __name__ == "__main__": + fibUntil4Mil = fib(4000000) + answer = sumOfEvensInList(fibUntil4Mil) + 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) + )) diff --git a/problem003.py b/problem003.py @@ -23,5 +23,6 @@ def primeFactors(num): return primes -answer = max(primeFactors(600851475143)) -print("The largest prime factor of the number 600851475143 is %s" % answer) +if __name__ == "__main__": + answer = max(primeFactors(600851475143)) + print("The largest prime factor of the number 600851475143 is %s" % answer) diff --git a/problem004.py b/problem004.py @@ -4,5 +4,6 @@ # language = "Python" # dateCompleted = "21/01/2020" -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)) +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)) diff --git a/problem005.py b/problem005.py @@ -4,11 +4,12 @@ # language = "Python" # dateCompleted = "21/01/2020" -answer = 1 -for i in range(1, 21): - if answer % i > 0: - for k in range(1, 21): - 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) +if __name__ == "__main__": + answer = 1 + for i in range(1, 21): + if answer % i > 0: + for k in range(1, 21): + 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) diff --git a/problem006.py b/problem006.py @@ -4,11 +4,12 @@ # language = "Python" # dateCompleted = "24/01/2020" -sumOfTheSquares = 0 -squareOfTheSum = 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) +if __name__ == "__main__": + sumOfTheSquares = 0 + squareOfTheSum = 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) diff --git a/problem007.py b/problem007.py @@ -15,9 +15,10 @@ def sieveOfEratosthenes(limit): prime[0:2] = [False, False] return [p for p in range(limit + 1) if prime[p]] -searchRange = 1000 -returnValue = sieveOfEratosthenes(searchRange) -while len(returnValue) < 10001: - searchRange += 1000 +if __name__ == "__main__": + searchRange = 1000 returnValue = sieveOfEratosthenes(searchRange) -print("The 10,001st prime number is %s" % returnValue[10000]) + while len(returnValue) < 10001: + searchRange += 1000 + returnValue = sieveOfEratosthenes(searchRange) + print("The 10,001st prime number is %s" % returnValue[10000]) diff --git a/problem008.py b/problem008.py @@ -20,5 +20,6 @@ def findLargestProduct(num): end += 1 return largestProduct -answer = findLargestProduct(VAL) -print(f"The value of the greatest product of thirteen adjacent digits in the provided 1000-digit number is {answer}") +if __name__ == "__main__": + answer = findLargestProduct(VAL) + print(f"The value of the greatest product of thirteen adjacent digits in the provided 1000-digit number is {answer}") diff --git a/problem009.py b/problem009.py @@ -11,7 +11,8 @@ def triplet(a, b): return int(a*b*c) return False -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}") +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}") diff --git a/problem010.py b/problem010.py @@ -15,5 +15,6 @@ def sieveOfEratosthenes(limit): prime[0:2] = [False, False] return [p for p in range(limit + 1) if prime[p]] -primes = sieveOfEratosthenes(2000000) -print(f"The sum of all the primes below two million is {sum(primes)}") +if __name__ == "__main__": + primes = sieveOfEratosthenes(2000000) + print(f"The sum of all the primes below two million is {sum(primes)}") diff --git a/problem011.py b/problem011.py @@ -80,8 +80,9 @@ def largestDiagonal(lst): largest = downRight if downRight > largest else largest return largest -diagonal = largestDiagonal(VAL) -horizontal = largestHorizontal(VAL) -vertical = largestVertical(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}") +if __name__ == "__main__": + diagonal = largestDiagonal(VAL) + horizontal = largestHorizontal(VAL) + vertical = largestVertical(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}") diff --git a/problem012.py b/problem012.py @@ -16,8 +16,9 @@ def triangle(pos): val += i return moreThanFiveHundredFactors(val) -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}") +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}") diff --git a/problem013.py b/problem013.py @@ -110,5 +110,6 @@ STRVAL = """ for line in STRVAL.splitlines(): VAL.append(int(line)) -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}") +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}")