Updated all problems to use fstrings and results()

This commit is contained in:
2020-10-30 16:20:39 -04:00
parent ad458278d4
commit 46216a2807
33 changed files with 280 additions and 452 deletions

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem1.py
#Matthew Ellison
# Created: 01-26-19
#Modified: 10-29-20
#Modified: 10-30-20
#What is the sum of all the multiples of 3 or 5 that are less than 1000
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -60,13 +60,14 @@ class Problem1(Problem):
def reset(self):
super().reset()
self.fullSum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return "The sum of all numbers < " + str(self.__topNum + 1) + " is " + str(self.fullSum)
#Gets
return f"The sum of all numbers < {self.__topNum + 1} is {self.fullSum}"
#Returns the requested sum
def getSum(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -80,15 +81,6 @@ class Problem1(Problem):
return int((numTerms / 2) * (multiple + (numTerms * multiple)))
#If you are running this file, automatically start the correct function
if(__name__ == "__main__"):
problem = Problem1()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The sum of all numbers < 1000 is 233168
It took an average of 2.293 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#Project Euler/Python/Problem10.py
#Matthew Ellison
# Created: 01-30-19
#Modified: 07-18-20
#Modified: 10-30-20
#Find the sum of all the primes below two million
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from Algorithms import getPrimes
@@ -56,17 +55,21 @@ class Problem10(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of all the prime numbers less than " + str(self.__numberGreaterThanPrimes + 1) + " is " + str(self.sum)
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.sum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The sum of all the prime numbers less than {self.__numberGreaterThanPrimes + 1} is {self.sum}"
#Returns the sum that was requested
def getSum(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -75,15 +78,6 @@ class Problem10(Problem):
return self.sum
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem10()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The sum of all the prime numbers less than 2000000 is 142913828922
It took an average of 7.187 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem11.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 07-18-20
#Modified: 10-30-20
#What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
"""
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
@@ -45,7 +45,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from Algorithms import prod
@@ -153,17 +152,21 @@ class Problem11(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The greatest product of 3 numbers in a line is " + str(prod(self.greatestProduct))
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.greatestProduct.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The greatest product of 3 numbers in a line is {prod(self.greatestProduct)}"
#Returns the numbers that were being searched
def getNumbers(self) -> list:
#If the problem hasn't been solved throw an exception
@@ -177,14 +180,6 @@ class Problem11(Problem):
raise Unsolved("You must solve the problem before you can get the product")
return prod(self.greatestProduct)
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem11()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The greatest product of 3 numbers in a line is 70600674

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem12.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 07-18-20
#Modified: 10-30-20
#What is the value of the first triangle number to have over five hundred divisors?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from Algorithms import getDivisors
@@ -63,14 +62,9 @@ class Problem12(Problem):
#Stop the timer
self.timer.stop()
#Save the results
if(self.sum <= 0):
self.result = "There was an error. Could not find a triangular number with " + str(self.__goalDivisors) + " divisors before overflow"
else:
self.result = "The first triangular number with more than " + str(self.__goalDivisors) + " divisors is " + str(self.sum)
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -79,6 +73,12 @@ class Problem12(Problem):
self.divisors.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The first triangular number with more than {self.__goalDivisors} divisors is {self.sum}"
#Returns the triangular number
def getTriangularNumber(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -104,14 +104,6 @@ class Problem12(Problem):
raise Unsolved("You must solve the problem before you can get the number of divisors")
return len(self.divisors)
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem12()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The first triangular number with more than 500 divisors is 76576500

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem13.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 07-17-20
#Modified: 10-30-20
#Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
"""
37107287533902102798797998220837590246510135740250
@@ -125,7 +125,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -254,17 +253,22 @@ class Problem13(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of all " + str(len(self.__numbers)) + " numbers is " + str(self.sum) + "\nThe first 10 digits are: " + str(self.sum)[0:10]
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
sum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The sum of all {len(self.__numbers)} numbers is {self.sum}\n" \
f"The first 10 digits are: {str(self.sum)[0:10]}"
#Returns the list of 50-digit numbers
def getNumbers(self) -> list:
#If the problem hasn't been solved throw an exception
@@ -278,14 +282,6 @@ class Problem13(Problem):
raise Unsolved("You must solve the problem before you can get the sum of the numbers")
return self.sum
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem13()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem14.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 07-18-20
#Modified: 10-30-20
"""
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
@@ -28,7 +28,6 @@ Which starting number, under one million, produces the longest chain?
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -64,11 +63,9 @@ class Problem14(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The number " + str(self.maxNum) + " produced a chain of " + str(self.maxLength) + " steps"
#Throw a flag to show the problem is solved
self.solved = True
#This function follows the rules of the sequence and returns its length
def checkSeries(self, num: int) -> int:
length = 1 #Start at 1 because you need to count the starting number
@@ -82,6 +79,7 @@ class Problem14(Problem):
length += 1
return length
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -89,6 +87,12 @@ class Problem14(Problem):
self.maxNum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The number {self.maxNum} produced a chain of {self.maxLength} steps"
#Returns the length of the requested chain
def getLength(self):
#If the problem hasn't been solved throw an exception
@@ -102,14 +106,6 @@ class Problem14(Problem):
raise Unsolved("You must solve the problem before you can get the number that started the series")
return self.maxNum
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem14()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The number 837799 produced a chain of 525 steps

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem15.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 07-18-20
#Modified: 10-30-20
#How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -49,16 +48,14 @@ class Problem15(Problem):
self.timer.start()
#Start the recursion at the right location and catch what is returned
numberMoves = self.movement(0, 0)
self.numOfRoutes = self.movement(0, 0)
#Stop the timer
self.timer.stop()
#Print the results
self.result = "The number of paths from 1 corner of a " + str(self.__gridWidth) + " x " + str(self.__gridHeight) + " grid to the opposite corner is " + str(numberMoves)
#Throw a flag to show the problem is solved
self.solved = True
#This function acts as a handler for moving the position on the grid and counting the distance
#It moves right first, then down
def movement(self, currentX: int, currentY: int) -> int:
@@ -76,12 +73,19 @@ class Problem15(Problem):
numberMoves += self.movement(currentX, currentY + 1)
return numberMoves
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.numOfRoutes = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The number of paths from 1 corner of a {self.__gridWidth} x {self.__gridHeight} grid to the opposite corner is {self.numOfRoutes}"
#Returns the number of routes found
def getNumberOfRoutes(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -89,14 +93,6 @@ class Problem15(Problem):
raise Unsolved("You must solve the problem before you can get the number of routes")
return self.numOfRoutes
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem15()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The number of paths from 1 corner of a 20 x 20 grid to the opposite corner is 137846528820

View File

@@ -1,11 +1,11 @@
#Project Euler/Python/Problem16.py
#Matthew Ellison
# Created: 02-03-19
#Modified: 03-28-19
#Modified: 10-30-20
#What is the sum of the digits of the number 2^1000?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
Copyright (C) 2019 Matthew Ellison
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -66,6 +65,7 @@ class Problem16(Problem):
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -73,6 +73,13 @@ class Problem16(Problem):
self.sumOfElements = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"{self.__numToPower}^{self.__power} = {self.num}\n" \
f"The sum of the elements is {self.sumOfElements}"
#Returns the number that was calculated
def getNumber(self):
#If the problem hasn't been solved throw an exception
@@ -86,15 +93,6 @@ class Problem16(Problem):
raise Unsolved("You must solve the problem before you can get the sum of the digits of the number")
return self.sumOfElements
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem16()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem17.py
#Matthew Ellison
# Created: 02-04-19
#Modified: 07-18-20
#Modified: 10-30-20
#If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import math
@@ -59,9 +58,6 @@ class Problem17(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of all the letters in all the numbers " + str(self.__startNum) + '-' + str(self.__stopNum) + " is " + str(self.letterCount)
#Throw a flag to show the problem is solved
self.solved = True
@@ -168,6 +164,7 @@ class Problem17(Problem):
#Return the string
return numberString
#Get the number of alphabetic characters in the string passed in
def getNumberChars(self, number: str) -> int:
sumOfLetters = 0
@@ -179,12 +176,19 @@ class Problem17(Problem):
#Return the number
return sumOfLetters
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.letterCount = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The sum of all the letters in all the numbers {self.__startNum}-{self.__stopNum} is {self.letterCount}"
#Returns the number of letters asked for
def getLetterCount(self):
#If the problem hasn't been solved throw an exception
@@ -193,15 +197,6 @@ class Problem17(Problem):
return self.letterCount
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem17()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The sum of all the letters in all the numbers 1-1000 is 21124
It took an average of 3.095 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem18.py
#Matthew Ellison
# Created: 03-12-19
#Modified: 07-20-20
#Modified: 10-30-20
#Find the maximum total from top to bottom
"""
75
@@ -40,7 +40,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from collections import namedtuple
@@ -128,9 +127,6 @@ class Problem18(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The value of the longest path is " + str(self.actualTotal)
#Throw a flag to show the problem is solved
self.solved = True
@@ -139,6 +135,7 @@ class Problem18(Problem):
for rowCnt in range(0, self.__numRows):
for colCnt in range(0, len(self.__listNum[rowCnt])):
self.__listNum[rowCnt][colCnt] = 100 - self.__listNum[rowCnt][colCnt]
#This function removes every element in listNum that is equal to loc
def removeIf(self, listNum: list, loc: tuple):
location = 0
@@ -147,14 +144,21 @@ class Problem18(Problem):
del listNum[location]
else:
location += 1
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.foundPoints.clear()
self.possiblePoints.clear()
actualTotal = 0
self.actualTotal = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The value of the longest path is {self.actualTotal}"
#Returns the pyramid that was traversed as a string
def getPyramid(self) -> str:
if(not self.solved):
@@ -180,14 +184,6 @@ class Problem18(Problem):
return self.actualTotal
if __name__ == "__main__":
problem = Problem18()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The value of the longest path is 1074
It took an average of 422.974 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem19.py
#Matthew Ellison
# Created: 03-13-19
#Modified: 07-18-20
#Modified: 10-30-20
#How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
"""
You are given the following information, but you may prefer to do some research for yourself.
@@ -34,7 +34,6 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -84,11 +83,9 @@ class Problem19(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "There are " + str(self.totalSundays) + " Sundays that landed on the first of the month from " + str(self.__startYear) + " to " + str(self.__endYear)
#Throw a flag to show the problem is solved
self.solved = True
#Return the day of the week that the date you pass into it is on
def getDay(self, month: int, day: int, year: int) -> DAYS:
#Make sure the numebrs are within propper bounds
@@ -155,6 +152,7 @@ class Problem19(Problem):
return DAYS.SATURDAY
else:
return DAYS.ERROR
#Returns true if the year passed to it is a leap year
def isLeapYear(self, year: int) -> bool:
if(year < 1):
@@ -168,12 +166,19 @@ class Problem19(Problem):
elif((year % 4) == 0):
return True
return False
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.totalSundays = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"There are {self.totalSundays} Sundays that landed on the first of the month from {self.__startYear} to {self.__endYear}"
#Returns the total sundays that were asked for
def getTotalSundays(self):
#If the problem hasn't been solved throw an exception
@@ -182,16 +187,6 @@ class Problem19(Problem):
return self.totalSundays
#Run automatically if the script was called stand alone
if __name__ == "__main__":
problem = Problem19()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
There are 171 Sundays that landed on the first of the month from 1901 to 2000
It took an average of 386.694 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem2.py
#Matthew Ellison
# Created: 01-26-19
#Modified: 10-29-20
#Modified: 10-30-20
#The sum of the even Fibonacci numbers less than 4,000,000
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -65,13 +65,14 @@ class Problem2(Problem):
def reset(self):
super().reset()
self.fullSum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return "The sum of all even Fibonacci numbers less than " + str(self.__topNumber + 1) + " is " + str(self.fullSum)
#Gets
return f"The sum of all even Fibonacci numbers less than {self.__topNumber + 1} is {self.fullSum}"
#Returns the requested sum
def getSum(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -80,15 +81,6 @@ class Problem2(Problem):
return self.fullSum
#If you are running this file, automatically start the correct function
if __name__ == '__main__':
problem = Problem2() #Call the function that answers the question
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The sum of all even Fibonacci numbers less than 4000000 is 4613732
It took an average of 10.286 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem20.py
#Matthew Ellison
# Created: 03-14-19
#Modified: 07-19-20
#Modified: 10-30-20
#What is the sum of the digits of 100!
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -61,9 +60,6 @@ class Problem20(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "100! = " + numString + "\nThe sum of the digits is: " + str(self.sum)
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
@@ -73,6 +69,12 @@ class Problem20(Problem):
self.sum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"100! = {self.num}\nThe sum of the digits is: {self.sum}"
#Returns the number 100!
def getNumber(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -86,14 +88,6 @@ class Problem20(Problem):
raise Unsolved("You must solve the problem before you can get the sum")
return self.sum
#This starts the correct function if called directly
if __name__ == "__main__":
problem = Problem20()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem21.py
#Matthew Ellison
# Created: 03-18-19
#Modified: 07-19-20
#Modified: 10-30-20
#Evaluate the sum of all the amicable numbers under 10000
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
@@ -71,14 +70,9 @@ class Problem21(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result += "All amicable numbers less than 10000 are\n"
for num in self.amicable:
self.result += str(num) + '\n'
self.result += "The sum of all of these amicable numbers is " + str(sum(self.amicable))
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -86,6 +80,18 @@ class Problem21(Problem):
self.amicable.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
result = "All amicable numbers less than 10000 are\n"
for num in self.amicable:
result += f"{num}\n"
result += f"The sum of all of these amicable numbers is {sum(self.amicable)}"
return result
#Returns a vector with all of the amicable number calculated
def getAmicable(self) -> list:
#If the problem hasn't been solved throw an exception
@@ -99,14 +105,6 @@ class Problem21(Problem):
raise Unsolved("You must solve the problem before you can get the sum of the amicable numbers")
return sum(self.amicable)
#Run the correct function if this script is called stand along
if __name__ == "__main__":
problem = Problem21()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
All amicable numbers less than 10000 are

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem22.py
#Matthew Ellison
# Created: 03-20-19
#Modified: 07-19-20
#Modified: 10-30-20
#What is the total of all the name scores in the file?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,9 +23,7 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
class Problem22(Problem):
@@ -416,7 +414,6 @@ class Problem22(Problem):
#Start the timer
self.timer.start()
#Sort all the names
self.__names.sort()
#Step through every name adding up the values of the characters
@@ -431,15 +428,12 @@ class Problem22(Problem):
for cnt in range(0, len(self.sums)):
self.prod.append(self.sums[cnt] * (cnt + 1))
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The answer to the question is " + str(sum(self.prod))
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -447,6 +441,12 @@ class Problem22(Problem):
self.prod.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The answer to the question is {sum(self.prod)}"
#Returns the vecot of the names being scored
def getNames(self) -> list:
return self.__names
@@ -457,14 +457,6 @@ class Problem22(Problem):
raise Unsolved("You must solve the problem before can you see the sum")
return sum(self.prod)
#This ensures the correct function is called if this is called as a stand along script
if __name__ == "__main__":
problem = Problem22()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The answer to the question is 871198282

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem23.py
#Matthew Ellison
# Created: 03-22-19
#Modified: 07-19-20
#Modified: 10-30-20
#Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
#All of my imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
@@ -50,7 +49,6 @@ class Problem23(Problem):
#Start the timer
self.timer.start()
#Get the sum of the divisors of all numbers < __maxNum
for cnt in range(1, self.__maxNum):
div = Algorithms.getDivisors(cnt)
@@ -70,13 +68,9 @@ class Problem23(Problem):
if(not self.isSum(abund, cnt)):
self.sum += cnt
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The answer is " + str(self.sum)
#Throw a flag to show the problem is solved
self.solved = True
#Reserve the size of the array to speed up insertion
@@ -106,6 +100,12 @@ class Problem23(Problem):
self.sum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The answer is {self.sum}"
#Returns the sum of the numbers asked for
def getSum(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -114,14 +114,6 @@ class Problem23(Problem):
return self.sum
if __name__ == "__main__":
problem = Problem23()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The answer is 4179871
It took an average of 24.184 minutes to run this problem through 10 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem24.py
#Matthew Ellison
# Created: 03-24-19
#Modified: 07-19-20
#Modified: 10-30-20
#What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
@@ -50,22 +49,26 @@ class Problem24(Problem):
self.timer.start()
#Get all permutations of the string
permutations = Algorithms.getPermutations(self.__nums)
self.permutations = Algorithms.getPermutations(self.__nums)
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The 1 millionth permutation is " + str(permutations[self.__neededPerm - 1])
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.permutations.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The 1 millionth permutation is {self.permutations[self.__neededPerm - 1]}"
#Returns a list with all of the permutations
def getPermutationsList(self) -> list:
#If the problem hasn't been solved throw an exception
@@ -81,15 +84,6 @@ class Problem24(Problem):
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem24()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The 1 millionth permutation is 2783915460
It took an average of 7.147 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem25.py
#Matthew Ellison
# Created: 03-25-19
#Modified: 07-19-20
#Modified: 10-30-20
#What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
@@ -57,11 +56,9 @@ class Problem25(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The first Fibonacci number with " + str(self.__numDigits) + " digits is " + str(self.number) + "\nIts index is " + str(self.index)
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -69,6 +66,13 @@ class Problem25(Problem):
self.index = 2
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The first Fibonacci number with {self.__numDigits} digits is {self.number}\n" \
f"Its index is {self.index}"
#Returns the Fibonacci number asked for
def getNumber(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -83,15 +87,6 @@ class Problem25(Problem):
return self.index
#This runs the appropriate functions if the script is called by itself
if __name__ == "__main__":
problem = Problem25()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816
Its index is 4782

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem26.py
#Matthew Ellison
# Created: 07-29-19
#Modified: 07-19-20
#Modified: 10-30-20
#Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,9 +23,7 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
class Problem26(Problem):
@@ -84,11 +82,9 @@ class Problem26(Problem):
#Stop the timer
self.timer.stop()
#Print the results
self.result = "The longest cycle is " + str(self.longestCycle) + " digits long" + "\nIt is started with the number " + str(self.longestNumber)
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -96,6 +92,13 @@ class Problem26(Problem):
self.longestNumber = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The longest cycle is {self.longestCycle} digits long\n" \
f"It is started with the number {self.longestNumber}"
#Returns the length of the longest cycle
def getLongestCycle(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -110,15 +113,6 @@ class Problem26(Problem):
return self.longestNumber
#This calls the appropriate functions if the script is called stand along
if __name__ == "__main__":
problem = Problem26()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The longest cycle is 982 digits long
It is started with the number 983

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem27.py
#Matthew Ellison
# Created: 09-15-19
#Modified: 07-19-20
#Modified: 10-30-20
#Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
@@ -78,6 +77,7 @@ class Problem27(Problem):
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -87,18 +87,28 @@ class Problem27(Problem):
self.primes.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The greatest number of primes found is {self.topN}\n" \
f"It was found with A = {self.topA}, B = {self.topB}\n" \
f"The product of A and B is {self.topA * self.topB}"
#Returns the top A that was generated
def getTopA(self) -> int:
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before can you see the top A")
return self.topA
#Returns the top B that was generated
def getTopB(self) -> int:
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before can you see the top B")
return self.topA
#Returns the top N that was generated
def getTopN(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -107,15 +117,6 @@ class Problem27(Problem):
return self.topA
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem27()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The greatest number of primes found is 70
It was found with A = -61, B = 971

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem28.py
#Matthew Ellison
# Created: 09-22-19
#Modified: 07-19-20
#Modified: 10-30-20
#What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -54,11 +53,9 @@ class Problem28(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of the diagonals in the given grid is " + str(self.sumOfDiagonals)
#Throw a flag to show the problem is solved
self.solved = True
#Sets up the grid
def setupGrid(self) -> list:
#Setup the grid to be the right size and fill it with 0's
@@ -103,6 +100,7 @@ class Problem28(Problem):
finalLocation = True
break
return self.grid
#Finds the sum of the diagonals in the grid
def findSum(self) -> int:
sumOfDiagonals = 0
@@ -120,12 +118,19 @@ class Problem28(Problem):
leftSide += 1
rightSide -= 1
return sumOfDiagonals
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.sumOfDiagonals = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The sum of the diagonals in the given grid is {self.sumOfDiagonals}"
#Returns the grid
def getGrid(self) -> list:
return self.grid
@@ -137,15 +142,6 @@ class Problem28(Problem):
return self.sumOfDiagonals
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem28()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The sum of the diagonals in the given grid is 669171001
It took an average of 212.747 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem29.py
#Matthew Ellison
# Created: 10-10-19
#Modified: 07-19-20
#Modified: 10-30-20
#How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -63,17 +62,21 @@ class Problem29(Problem):
#Stop the timer
self.timer.stop()
#Print the results
self.result = "The number of unique values generated by a^b for " + str(self.__bottomA) + " <= a < = " + str(self.__topA) + " and " + str(self.__bottomB) + " <= b <= " + str(self.__topB) + " is " + str(len(self.unique))
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.unique.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The number of unique values generated by a^b for {self.__bottomA} <= a < = {self.__topA} and {self.__bottomB} <= b <= {self.__topB} is {len(self.unique)}"
#Returns the lowest possible value for a
def getBottomA(self):
#If the problem hasn't been solved throw an exception
@@ -106,15 +109,6 @@ class Problem29(Problem):
return self.unique
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem29()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The number of unique values generated by a^b for 2 <= a < = 100 and 2 <= b <= 100 is 9183
It took an average of 304.306 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem3.py
#Matthew Ellison
# Created: 01-27-19
#Modified: 10-29-20
#Modified: 10-30-20
#The largest prime factor of 600851475143
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -27,7 +27,6 @@ from Algorithms import getFactors
from Unsolved import Unsolved
class Problem3(Problem):
#Variables
__goalNumber = 600851475143
@@ -62,13 +61,14 @@ class Problem3(Problem):
def reset(self):
super().reset()
self.factors.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return "The largest prime factor of " + str(self.__goalNumber) + " is " + str(self.factors[(len(self.factors) - 1)])
#Gets
return f"The largest prime factor of {self.__goalNumber} is {self.factors[(len(self.factors) - 1)]}"
#Returns the list of factors of the number
def getFactors(self) -> list:
#If the problem hasn't been solved throw an exceptions
@@ -86,15 +86,6 @@ class Problem3(Problem):
return self.__goalNumber
#If you are running this file, automatically start the correct function
if __name__ == '__main__':
problem = Problem3()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The largest prime factor of 600851475143 is 6857
It took an average of 2.084 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem30.py
#Matthew Ellison
# Created: 10-28-19
#Modified: 07-19-20
#Modified: 10-30-20
#Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -65,11 +64,9 @@ class Problem30(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of all the numbers that can be written as the sum of the fifth powers of their digits is " + str(sum(self.sumOfFifthNumbers))
#Throw a flag to show the problem is solved
self.solved = True
#Returns a list with the individual digits of the number passed to it
def getDigits(self, num: int) -> list:
listOfDigits = [] #This list holds the individual digits of num
@@ -80,11 +77,19 @@ class Problem30(Problem):
listOfDigits.append(int(digits[cnt]))
#Return the list of digits
return listOfDigits
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.sumOfFifthNumbers.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The sum of all the numbers that can be written as the sum of the fifth powers of their digits is {sum(self.sumOfFifthNumbers)}"
#Returns the top number to be checked
def getTopNum(self) -> int:
#If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem31.py
#Matthew Ellison
# Created: 06-19-20
#Modified: 07-19-20
#Modified: 10-30-20
#How many different ways can £2 be made using any number of coins?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -60,17 +59,21 @@ class Problem31(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "There are " + str(self.permutations) + " ways to make 2 pounds with the given denominations of coins"
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.permutations = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"There are {self.permutations} ways to make 2 pounds with the given denominations of coins"
#Returns the number of correct permutations of the coins
def getPermutations(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -79,16 +82,6 @@ class Problem31(Problem):
return self.permutations
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem31()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
There are 73682 ways to make 2 pounds with the given denominations of coins
It took an average of 5.519 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/ProjectEulerPython/Problems/Problem32.py
#Matthew Ellison
# Created: 07-28-20
#Modified: 07-28-20
#Modified: 10-30-20
#Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,9 +23,7 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from collections import namedtuple
class Problem32(Problem):
@@ -87,9 +85,6 @@ class Problem32(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "There are " + str(len(self.listOfProducts)) + " unique 1-9 pandigitals\nThe sum of the products of these pandigitals is " + str(self.sumOfPandigitals)
#Throw a flag to show the problem is solved
self.solved = True
@@ -115,6 +110,12 @@ class Problem32(Problem):
self.sumOfPandigitals = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"There are {self.listOfProducts} unique 1-9 pandigitals\nThe sum of the products of these pandigitals is {self.sumOfPandigitals}"
#Returns the sum of the pandigitals
def getSumOfPandigitals(self):
#If the problem hasn't been solved throw an exception
@@ -122,15 +123,6 @@ class Problem32(Problem):
raise Unsolved("You must solve the problem before can you see the sum of the pandigitals")
return self.sumOfPandigitals
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem32()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
There are 7 unique 1-9 pandigitals

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem4.py
#Matthew Ellison
# Created: 01-28-19
#Modified: 10-29-20
#Modified: 10-30-20
#Find the largest palindrome made from the product of two 3-digit numbers
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -66,17 +66,19 @@ class Problem4(Problem):
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.palindromes.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return "The largest palindrome made from the product of two 3-digit numbers is " + str(self.palindromes[len(self.palindromes) - 1])
#Gets
return f"The largest palindrome made from the product of two 3-digit numbers is {self.palindromes[len(self.palindromes) - 1]}"
#Returns the list of all palindromes
def getPalindromes(self) -> list:
#If the problem hasn't been solved throw an exceptions
@@ -91,15 +93,6 @@ class Problem4(Problem):
return self.palindromes[len(self.palindromes) - 1]
#If you are running this file, automatically start the correct function
if __name__ == '__main__':
problem = Problem4()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The largest palindrome made from the product of two 3-digit numbers is 906609
It took an average of 149.187 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEulter/Python/Project5.py
#Matthew Ellison
# Created: 01-28-19
#Modified: 10-29-20
#Modified: 10-30-20
#What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -73,17 +73,19 @@ class Problem5(Problem):
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.smallestNum = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return "The smallest positive number that is evenly divisible by all numbers 1-20 is " + str(self.smallestNum)
#Gets
return f"The smallest positive number that is evenly divisible by all numbers 1-20 is {self.smallestNum}"
#Returns the requested number
def getNumber(self) -> int:
#If the problem hasn't been solved throw an exceptions
@@ -92,15 +94,6 @@ class Problem5(Problem):
return self.smallestNum
#If it are running this file, automatically start the correct function
if __name__ == '__main__':
problem = Problem5()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The smallest positive number that is evenly divisible by all numbers 1-20 is 232792560
It took an average of 54.833 seconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem6.py
#Matthew Ellison
# Created: 01-28-19
#Modified: 07-18-20
#Modified: 10-29-20
#Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -50,7 +49,7 @@ class Problem6(Problem):
self.timer.start()
#Run through all numbers from 1-100 and add them to the approriate sums
for num in range(1, 101):
for num in range(self.__startNum, self.__endNum + 1):
self.sumOfSquares += (num * num) #Get the sum of the squares of the first 100 natural numbers
self.squareOfSum += num #Get the sum of the first 100 natural numbers so you can square it later
#Square the normal sum
@@ -59,9 +58,6 @@ class Problem6(Problem):
#Stop the timer
self.timer.stop()
#Save the result
self.result = "The difference between the sum of the squares and the square of the sum of the numbers 1-100 is " + str(abs(self.sumOfSquares - self.squareOfSum))
#Throw a flag to show the problem is solved
self.solved = True
@@ -72,6 +68,12 @@ class Problem6(Problem):
self.sumOfSquares = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The difference between the sum of the squares and the square of the sum of the numbers 1-100 is {abs(self.sumOfSquares - self.squareOfSum)}"
#Returns the sum of all the squares
def getSumOfSquares(self) -> int:
#If the problem hasn't been solved throw an exceptions
@@ -91,14 +93,6 @@ class Problem6(Problem):
raise Unsolved("You must solve the problem before you can get the difference between the sum of squares and square of sum")
return abs(self.sumOfSquares - self.squareOfSum)
#If you are running this file, automatically start the correct function
if __name__ == '__main__':
problem = Problem6()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The difference between the sum of the squares and the square of the sum of the numbers 1-100 is 25164150

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem67.py
#Matthew Ellison
# Created: 03-26-19
#Modified: 07-19-20
#Modified: 10-30-20
#Find the maximum total from top to bottom
"""
59
@@ -125,7 +125,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from collections import namedtuple
@@ -298,9 +297,6 @@ class Problem67(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The value of the longest path is " + str(self.actualTotal)
#Throw a flag to show the problem is solved
self.solved = True
@@ -309,6 +305,7 @@ class Problem67(Problem):
for rowCnt in range(0, self.__numRows):
for colCnt in range(0, len(self.__listNum[rowCnt])):
self.__listNum[rowCnt][colCnt] = 100 - self.__listNum[rowCnt][colCnt]
#This function removes every element in listNum that is equal to loc
def removeIf(self, listNum: list, loc: tuple):
location = 0
@@ -317,6 +314,7 @@ class Problem67(Problem):
del listNum[location]
else:
location += 1
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -325,6 +323,12 @@ class Problem67(Problem):
self.actualTotal = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The value of the longest path is {self.actualTotal}"
#Returns the pyramid that was traversed as a string
def getPyramid(self) -> str:
if(not self.solved):
@@ -350,14 +354,6 @@ class Problem67(Problem):
return self.actualTotal
if __name__ == "__main__":
problem = Problem67()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The value of the longest path is 7273
It took an average of 8.244 seconds to run this problem through 100 iterations

View File

@@ -1,8 +1,8 @@
#Project Eulter/Python/Problem7.py
#Matthew Ellison
# Created: 01-29-19
#Modified: 07-18-20
#What is the 10001th prime number?
#Modified: 10-30-20
#What is the 10001st prime number?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
Copyright (C) 2020 Matthew Ellison
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from Algorithms import getNumPrimes
@@ -35,7 +34,7 @@ class Problem7(Problem):
#Functions
#Constructor
def __init__(self):
super().__init__("What is the 10001th prime number?")
super().__init__("What is the 10001st prime number?")
self.primes = []
#Operational functions
@@ -54,16 +53,21 @@ class Problem7(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The " + str(self.__numPrimes) + "th prime number is " + str(self.primes[self.__numPrimes - 1])
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.primes.clear()
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The {self.__numPrimes}st prime number is {self.primes[self.__numPrimes - 1]}"
#Returns the requested prime number
def getPrime(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -71,17 +75,8 @@ class Problem7(Problem):
raise Unsolved("You must solve the problem before you can get the requested prime")
return self.primes[len(self.primes) - 1]
#If you are running this file, automatically start the correct function
if __name__ == '__main__':
problem = Problem7()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The 10001th prime number is 104743
The 10001st prime number is 104743
It took an average of 109.101 milliseconds to run this problem through 100 iterations
"""

View File

@@ -1,7 +1,7 @@
#Project Euler/Python/Problem8.py
#Matthew Ellison
# Created: 01-29-19
#Modified: 07-18-20
#Modified: 10-30-20
#Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
"""
73167176531330624919225119674426574742355349194934
@@ -45,7 +45,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -85,18 +84,23 @@ class Problem8(Problem):
#Stop the timer
self.timer.stop
#Save the results
self.result = "The largest product of 13 adjacent digits in the number is " + str(self.maxProduct) + "\nThe numbers are: " + self.maxNums
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
maxNums = ""
maxProduct = 0
self.maxNums = ""
self.maxProduct = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The largest product of 13 adjacent digits in the number is {self.maxProduct}\n" \
f"The numbers are: {self.maxNums}"
#Returns the string of number that produces the largest product
def getLargestNums(self) -> str:
#If the problem hasn't been solved throw an exception
@@ -110,14 +114,6 @@ class Problem8(Problem):
raise Unsolved("You must solve the problem before you can get the requested product")
return self.maxProduct
#If you are running this file, automatically start the correct function
if __name__ == '__main__':
problem = Problem8()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The largest product of 13 adjacent digits in the number is 23514624000

View File

@@ -1,7 +1,7 @@
#Project Euler/Python/Problem9.py
#Matthew Ellison
# Created: 01-29-19
#Modified: 07-18-20
#Modified: 10-30-20
#There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import math
@@ -70,14 +69,9 @@ class Problem9(Problem):
#Stop the timer
self.timer.stop()
#Save the results
if(self.found):
self.result = "The Pythagorean triplet where a + b + c = 1000 is " + str(self.a) + " " + str(self.b) + " " + str(int(self.c)) + "\nThe product of those numbers is " + str(int(self.a * self.b * self.c))
else:
self.result = "Could not find the triplet where a + b + c = 1000"
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
@@ -87,6 +81,13 @@ class Problem9(Problem):
self.found = False
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The Pythagorean triplet where a + b + c = 1000 is {self.a} {self.b} {int(self.c)}\n" \
f"The product of those numbers is {int(self.a * self.b * self.c)}"
#Returns the length of the first side
def getSideA(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -112,14 +113,6 @@ class Problem9(Problem):
raise Unsolved("You must solve the problem before you can get the length first side")
return int(self.a * self.b * self.c)
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem9()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The Pythagorean triplet where a + b + c = 1000 is 200 375 425