mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated to use new library layout
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#ProjectEuler/ProjectEulerPython/Problems/Problem.py
|
||||
#Matthew Ellison
|
||||
# Created: 07-11-20
|
||||
#Modified: 07-11-20
|
||||
#Modified: 07-23-21
|
||||
#This is a base class for problems to use as a template
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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
|
||||
@@ -28,9 +28,13 @@ from Unsolved import Unsolved
|
||||
|
||||
class Problem(metaclass=abc.ABCMeta):
|
||||
#Functions
|
||||
#Make sure the problem has been solved and throw an exception if not
|
||||
def solvedCheck(self, str: str) -> None:
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can see the " + str)
|
||||
#Constructor
|
||||
@abc.abstractmethod
|
||||
def __init__(self, description: str):
|
||||
def __init__(self, description: str) -> None:
|
||||
#Instance variables
|
||||
self.timer = Stopwatch()
|
||||
self.description = description
|
||||
@@ -39,27 +43,24 @@ class Problem(metaclass=abc.ABCMeta):
|
||||
#Returns the description of the problem
|
||||
def getDescription(self) -> str:
|
||||
return self.description
|
||||
#Returns the time taken to run the problem as a string
|
||||
def getTime(self) -> str:
|
||||
self.solvedCheck("time it took to run the algorithm")
|
||||
return self.timer.getString()
|
||||
#Returns the timer as a stopwatch
|
||||
def getTimer(self) -> Stopwatch:
|
||||
self.solvedCheck("timer")
|
||||
return self.timer
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self) -> None:
|
||||
self.timer.reset()
|
||||
self.solved = False
|
||||
self.result = ""
|
||||
#Solve the problem
|
||||
@abc.abstractmethod
|
||||
def solve(self) -> None:
|
||||
pass
|
||||
#Returns the result of solving the problem
|
||||
@abc.abstractmethod
|
||||
def getResult(self) -> str:
|
||||
pass
|
||||
#Returns the time taken to run the problem as a string
|
||||
def getTime(self) -> str:
|
||||
#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 time")
|
||||
return self.timer.getString()
|
||||
#Returns the timer as a stopwatch
|
||||
def getTimer(self) -> Stopwatch:
|
||||
#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 timer")
|
||||
return self.timer
|
||||
#Solve the problem
|
||||
@abc.abstractmethod
|
||||
def solve(self):
|
||||
pass
|
||||
def reset(self):
|
||||
self.timer.reset()
|
||||
self.solved = False
|
||||
self.result = ""
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem1.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-26-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-23-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
import math
|
||||
|
||||
|
||||
@@ -33,13 +32,13 @@ class Problem1(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the sum of all the multiples of 3 or 5 that are less than 1000")
|
||||
self.fullSum = 0 #The sum of all the numbers
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,8 +46,10 @@ class Problem1(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap
|
||||
self.fullSum = self.sumOfProgression(3) + self.sumOfProgression(5) - self.sumOfProgression(3 * 5)
|
||||
self.fullSum = self.__sumOfProgression(3) + self.__sumOfProgression(5) - self.__sumOfProgression(3 * 5)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
@@ -57,25 +58,21 @@ class Problem1(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
self.solvedCheck("result")
|
||||
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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the sum")
|
||||
self.solvedCheck("sum")
|
||||
return self.fullSum
|
||||
#Gets the sum of the progression of the multiple
|
||||
def sumOfProgression(self, multiple: int) -> int:
|
||||
def __sumOfProgression(self, multiple: int) -> int:
|
||||
numTerms = math.floor(self.__topNum / multiple) #Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap
|
||||
#The sum of progression formula is (n / 2)(a + l). n = number of terms, a = multiple, l = last term
|
||||
return int((numTerms / 2) * (multiple + (numTerms * multiple)))
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#Project Euler/Python/Problem10.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-30-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
from Algorithms import getPrimes
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem10(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem10(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all the primes below two million")
|
||||
self.sum = 0 #The sum of all of the prime numbers
|
||||
|
||||
#Operational functions
|
||||
#Solve the function
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,11 +46,13 @@ class Problem10(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get all of the primes < 2000000
|
||||
primes = getPrimes(self.__numberGreaterThanPrimes)
|
||||
primes = NumberAlgorithms.getPrimes(self.__numberGreaterThanPrimes)
|
||||
#Get the sum of the list
|
||||
self.sum = sum(primes)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -59,22 +60,18 @@ class Problem10(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the sum")
|
||||
self.solvedCheck("sum")
|
||||
return self.sum
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem11.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-31-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
@@ -27,7 +27,7 @@
|
||||
"""
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -45,8 +45,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
from Algorithms import prod
|
||||
from ArrayAlgorithms import prod
|
||||
|
||||
|
||||
class Problem11(Problem):
|
||||
@@ -74,13 +73,13 @@ class Problem11(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?")
|
||||
self.greatestProduct = []
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -92,6 +91,7 @@ class Problem11(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Loop through every location in the grid
|
||||
for row in range(0, len(self.__grid)):
|
||||
for col in range (0, len(self.__grid[row])):
|
||||
@@ -149,6 +149,7 @@ class Problem11(Problem):
|
||||
if(prod(currentNumbers) > prod(self.greatestProduct)):
|
||||
self.greatestProduct = currentNumbers.copy()
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -156,28 +157,22 @@ class Problem11(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the numbers being searched")
|
||||
self.solvedCheck("numbers")
|
||||
return self.greatestProduct
|
||||
#Returns the product that was requested
|
||||
def getProduct(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the product")
|
||||
self.solvedCheck("product of the numbers")
|
||||
return prod(self.greatestProduct)
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem12.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-31-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
from Algorithms import getDivisors
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem12(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem12(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the value of the first triangle number to have over five hundred divisors?")
|
||||
self.sum = 1
|
||||
self.counter = 2
|
||||
self.divisors = []
|
||||
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,11 +46,12 @@ class Problem12(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start at the first triangular number and loop around, moving to the next, until you find one with the appropriate number of divisors
|
||||
foundNumber = False #A flag for when the triangular number has over __goalDivisors divisors
|
||||
while((not foundNumber) and (self.sum > 0)): #Make sure you haven't caused an overflow and made trianularNumber negative
|
||||
#See how many divisors this triangular number has
|
||||
self.divisors = getDivisors(self.sum)
|
||||
self.divisors = NumberAlgorithms.getDivisors(self.sum)
|
||||
#If it did have enough raise a flag to stop the loop
|
||||
if(len(self.divisors) > self.__goalDivisors):
|
||||
foundNumber = True
|
||||
@@ -59,6 +59,7 @@ class Problem12(Problem):
|
||||
self.sum += self.counter #Add the next number to continue the triangular sequence
|
||||
self.counter += 1 #Advance to the next number in the triangular sequence
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -66,7 +67,7 @@ class Problem12(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.sum = 1
|
||||
self.counter = 2
|
||||
@@ -74,34 +75,24 @@ class Problem12(Problem):
|
||||
|
||||
#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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the triangular number")
|
||||
self.solvedCheck("triangular number")
|
||||
return self.sum
|
||||
#Gets the final number that was added to the triangular number
|
||||
def getLastNumberAdded(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the last number added to the triangular number")
|
||||
self.solvedCheck("last number added to get the triangular number")
|
||||
return self.counter - 1
|
||||
#Returns the list of divisors of the requested number
|
||||
def getDivisorsOfTriangularNumber(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the divisors of the triangular number")
|
||||
self.solvedCheck("divisors of the triangular number")
|
||||
return self.divisors
|
||||
#Returns the number of divisors of the requested number
|
||||
def getNumberOfDivisors(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the number of divisors")
|
||||
self.solvedCheck("number of divisors of the triangular number")
|
||||
return len(self.divisors)
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem13.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-31-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
|
||||
"""
|
||||
37107287533902102798797998220837590246510135740250
|
||||
@@ -107,7 +107,7 @@
|
||||
"""
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -125,7 +125,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem13(Problem):
|
||||
@@ -233,13 +232,13 @@ class Problem13(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Work out the first ten digits of the sum of the one-hundred 50-digit numbers")
|
||||
self.sum = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -247,9 +246,11 @@ class Problem13(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the sum of all of the numbers in the list
|
||||
self.sum = sum(self.__numbers)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -257,29 +258,23 @@ class Problem13(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the numbers")
|
||||
self.solvedCheck("numbers")
|
||||
return self.__numbers
|
||||
#Returns the sum of the 50-digit numbers
|
||||
def getSum(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the sum of the numbers")
|
||||
self.solvedCheck("sum")
|
||||
return self.sum
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem14.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-31-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
"""
|
||||
The following iterative sequence is defined for the set of positive integers:
|
||||
n → n/2 (n is even)
|
||||
@@ -10,7 +10,7 @@ Which starting number, under one million, produces the longest chain?
|
||||
"""
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -28,7 +28,6 @@ Which starting number, under one million, produces the longest chain?
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem14(Problem):
|
||||
@@ -37,14 +36,14 @@ class Problem14(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Which starting number, under one million, produces the longest chain using the itterative sequence?")
|
||||
self.maxLength = 0
|
||||
self.maxNum = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -52,6 +51,7 @@ class Problem14(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Loop through all number <= topNum and check them against the series
|
||||
for currentNum in range(1, self.__topNum + 1):
|
||||
currentLength = self.checkSeries(currentNum)
|
||||
@@ -60,6 +60,7 @@ class Problem14(Problem):
|
||||
self.maxLength = currentLength
|
||||
self.maxNum = currentNum
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -81,29 +82,23 @@ class Problem14(Problem):
|
||||
return length
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.maxLength = 0
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length of the requested chain")
|
||||
def getLength(self) -> int:
|
||||
self.solvedCheck("length of the longest chain")
|
||||
return self.maxLength
|
||||
#Returns the starting number of the requested chain
|
||||
def getStartingNumber(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 get the number that started the series")
|
||||
def getStartingNumber(self) -> int:
|
||||
self.solvedCheck("starting number of the longest chain")
|
||||
return self.maxNum
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem15.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-31-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem15(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem15(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("How many routes from the top left corner to the bottom right corner are there through a 20x20 grid if you can only move right and down?")
|
||||
self.numOfRoutes = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,9 +46,11 @@ class Problem15(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start the recursion at the right location and catch what is returned
|
||||
self.numOfRoutes = self.movement(0, 0)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -75,22 +76,18 @@ class Problem15(Problem):
|
||||
return numberMoves
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the number of routes")
|
||||
self.solvedCheck("number of routes")
|
||||
return self.numOfRoutes
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#Project Euler/Python/Problem16.py
|
||||
#Matthew Ellison
|
||||
# Created: 02-03-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem16(Problem):
|
||||
@@ -33,14 +32,14 @@ class Problem16(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the sum of the digits of the number 2^1000?")
|
||||
self.num = 0
|
||||
self.sumOfElements = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -48,6 +47,7 @@ class Problem16(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the number
|
||||
self.num = self.__numToPower ** self.__power
|
||||
#Change the number to a string
|
||||
@@ -57,40 +57,33 @@ class Problem16(Problem):
|
||||
#Change the character to an int and add it to the sum
|
||||
self.sumOfElements += int(stringOfNum[cnt])
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the result
|
||||
self.result = str(self.__numToPower) + "^" + str(self.__power) + " = " + stringOfNum + "\nThe sum of the elements is " + str(self.sumOfElements)
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.num = 0
|
||||
self.sumOfElements = 0
|
||||
|
||||
#Gets
|
||||
#Returns the result of solving the problem
|
||||
def getResult(self):
|
||||
def getResult(self) -> str:
|
||||
#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")
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the number")
|
||||
def getNumber(self) -> int:
|
||||
self.solvedCheck("number")
|
||||
return self.num
|
||||
#Return the sum of digits of the number
|
||||
def getSum(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 get the sum of the digits of the number")
|
||||
def getSum(self) -> int:
|
||||
self.solvedCheck("sum")
|
||||
return self.sumOfElements
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem17.py
|
||||
#Matthew Ellison
|
||||
# Created: 02-04-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
import math
|
||||
|
||||
|
||||
@@ -34,13 +33,13 @@ class Problem17(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("If all the numbers from 1 (one) to 1000 (one thousand) inclusive were written out in words, how many letters would be used?")
|
||||
self.letterCount = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -48,6 +47,7 @@ class Problem17(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with 1 and increment
|
||||
for num in range(self.__startNum, self.__stopNum + 1):
|
||||
#Pass the number to a function that will create a string for the number
|
||||
@@ -55,6 +55,7 @@ class Problem17(Problem):
|
||||
#Pass the string to a function that will count the number of letters in a string, ignoring whitespace and punctuation and add the amount to the running tally
|
||||
self.letterCount += self.getNumberChars(currentNumString)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -178,22 +179,18 @@ class Problem17(Problem):
|
||||
return sumOfLetters
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the number of letters")
|
||||
def getLetterCount(self) -> int:
|
||||
self.solvedCheck("letter count")
|
||||
return self.letterCount
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem18.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-12-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#Find the maximum total from top to bottom
|
||||
"""
|
||||
75
|
||||
@@ -22,7 +22,7 @@
|
||||
"""
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -40,7 +40,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
@@ -68,7 +67,7 @@ class Problem18(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the maximum total from top to bottom")
|
||||
self.foundPoints = [] #For the points that I have already found the shortest distance to
|
||||
self.possiblePoints = [] #For the locations you are checking this round
|
||||
@@ -76,7 +75,7 @@ class Problem18(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -84,6 +83,7 @@ class Problem18(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Invert the list so that each element = 100 - element
|
||||
self.invert()
|
||||
|
||||
@@ -124,6 +124,7 @@ class Problem18(Problem):
|
||||
#Invert the list so it can be read again
|
||||
self.invert()
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -131,13 +132,13 @@ class Problem18(Problem):
|
||||
self.solved = True
|
||||
|
||||
#This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
|
||||
def invert(self):
|
||||
def invert(self) -> None:
|
||||
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):
|
||||
def removeIf(self, listNum: list, loc: tuple) -> None:
|
||||
location = 0
|
||||
while(location < len(listNum)):
|
||||
if((listNum[location].xLocation == loc.xLocation) and (listNum[location].yLocation == loc.yLocation)):
|
||||
@@ -146,7 +147,7 @@ class Problem18(Problem):
|
||||
location += 1
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.foundPoints.clear()
|
||||
self.possiblePoints.clear()
|
||||
@@ -154,33 +155,27 @@ class Problem18(Problem):
|
||||
|
||||
#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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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):
|
||||
raise Unsolved("You must solve the problem before you can get the pyramid")
|
||||
|
||||
results = ""
|
||||
self.solvedCheck("pyramid of numbers")
|
||||
pyramidString = ""
|
||||
#Loop through all elements of the list and print them
|
||||
for row in self.__listNum:
|
||||
for column in row:
|
||||
results += "{:02d}".format(column)
|
||||
results += '\n'
|
||||
return results
|
||||
pyramidString += "{:02d}".format(column)
|
||||
pyramidString += '\n'
|
||||
return pyramidString
|
||||
#Returns the trail the algorithm took as a string
|
||||
def getTrail(self) -> str:
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the trail")
|
||||
self.solvedCheck("trail of the shortest path")
|
||||
#TODO: Implement this
|
||||
return ""
|
||||
#Returns the total that was asked for
|
||||
def getTotal(self) -> int:
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the total")
|
||||
self.solvedCheck("total")
|
||||
return self.actualTotal
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem19.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-13-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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.
|
||||
@@ -16,7 +16,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
|
||||
"""
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class DAYS:
|
||||
@@ -55,13 +54,13 @@ class Problem19(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?")
|
||||
self.totalSundays = 0 #Keep track of the number of sundays
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -69,6 +68,7 @@ class Problem19(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Run for all years from start to end
|
||||
for year in range(self.__startYear, self.__endYear + 1):
|
||||
#Run for all months in the year
|
||||
@@ -80,6 +80,7 @@ class Problem19(Problem):
|
||||
elif(day == DAYS.SUNDAY):
|
||||
self.totalSundays += 1
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -168,22 +169,18 @@ class Problem19(Problem):
|
||||
return False
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the total sundays")
|
||||
def getTotalSundays(self) -> int:
|
||||
self.solvedCheck("total number of sundays")
|
||||
return self.totalSundays
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem2.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-26-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-23-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Algorithms import getAllFib
|
||||
from Unsolved import Unsolved
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem2(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem2(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the sum of the even Fibonacci numbers less than 4,000,000?")
|
||||
self.fullSum = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,14 +46,16 @@ class Problem2(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get a list of all fibonacci numbers < 4,000,000
|
||||
fibNums = getAllFib(self.__topNumber) #Send it - 1 because it is < __topNumber
|
||||
fibNums = NumberAlgorithms.getAllFib(self.__topNumber)
|
||||
#Step through every element in the list checking if it is even
|
||||
for num in fibNums:
|
||||
#If the number is even add it to the running tally
|
||||
if((num % 2) == 0):
|
||||
self.fullSum += num
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -62,22 +63,18 @@ class Problem2(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the sum")
|
||||
self.solvedCheck("sum")
|
||||
return self.fullSum
|
||||
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem20(Problem):
|
||||
@@ -32,14 +31,14 @@ class Problem20(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the sum of the digits of 100!?")
|
||||
self.num = 1 #Holds the number 100!
|
||||
self.sum = 0 #The sum of the digts of num
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,6 +46,7 @@ class Problem20(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Run through every number from 1 to 100 and multiply it by the current num to get 100!
|
||||
for cnt in range(1, self.__top_num + 1):
|
||||
self.num *= cnt
|
||||
@@ -57,35 +57,30 @@ class Problem20(Problem):
|
||||
for char in numString:
|
||||
self.sum += int(char)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.num = 1
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the number")
|
||||
self.solvedCheck("number")
|
||||
return self.num
|
||||
#Returns the sum of the digits of 100!
|
||||
def getSum(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the sum")
|
||||
self.solvedCheck("sum of the digits")
|
||||
return self.sum
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem21.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-18-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem21(Problem):
|
||||
@@ -33,12 +32,12 @@ class Problem21(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Evaluate the sum of all the amicable numbers under 10000")
|
||||
self.divisorSum = [] #Holds the sum of the divisors of the subscript number
|
||||
self.amicable = [] #Holds all amicable numbers
|
||||
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -46,10 +45,11 @@ class Problem21(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Generate the divisors of all the numbers < 10000, get their sum, and add it to the list
|
||||
self.divisorSum.append(0) #Start with a 0 in the [0] location
|
||||
for cnt in range(1, self.__limit):
|
||||
divisors = Algorithms.getDivisors(cnt) #Get all the divisors of a number
|
||||
divisors = NumberAlgorithms.getDivisors(cnt) #Get all the divisors of a number
|
||||
if(len(divisors) > 1):
|
||||
divisors.pop() #Remove the last entry because it will be the number itself
|
||||
self.divisorSum.append(int(sum(divisors)))
|
||||
@@ -67,6 +67,7 @@ class Problem21(Problem):
|
||||
#Add the number to the amicable vector
|
||||
self.amicable.append(cnt)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -74,18 +75,15 @@ class Problem21(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.divisorSum.clear()
|
||||
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")
|
||||
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
result = "All amicable numbers less than 10000 are\n"
|
||||
for num in self.amicable:
|
||||
result += f"{num}\n"
|
||||
@@ -94,15 +92,11 @@ class Problem21(Problem):
|
||||
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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the amicable numbers")
|
||||
self.solvedCheck("amicable numbers")
|
||||
return self.amicable
|
||||
#Returns the sum of all of the amicable numbers
|
||||
def getSum(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the sum of the amicable numbers")
|
||||
self.solvedCheck("sum of the amicable numbers")
|
||||
return sum(self.amicable)
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem22.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-20-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem22(Problem):
|
||||
@@ -399,14 +398,14 @@ class Problem22(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the total of all the name scores in this file?")
|
||||
self.sums = [] #Holds the score based on the sum of the characters in the name
|
||||
self.prod = [] #Holds the score based on the sum of the characters and the location in alphabetical order
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -414,6 +413,7 @@ 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
|
||||
@@ -428,6 +428,7 @@ 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()
|
||||
|
||||
@@ -435,26 +436,22 @@ class Problem22(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.sums.clear()
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
#Returns the sum of the names scores
|
||||
def getNameScoreSum(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 sum")
|
||||
self.solvedCheck("score of the names")
|
||||
return sum(self.prod)
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem23.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-22-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem23(Problem):
|
||||
@@ -33,7 +32,7 @@ class Problem23(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers")
|
||||
self.divisorSums = []
|
||||
self.reserveArray()
|
||||
@@ -41,7 +40,7 @@ class Problem23(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -49,9 +48,10 @@ 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)
|
||||
div = NumberAlgorithms.getDivisors(cnt)
|
||||
if(len(div) > 1):
|
||||
div.remove(div[len(div) - 1])
|
||||
self.divisorSums[cnt] = sum(div)
|
||||
@@ -68,15 +68,16 @@ class Problem23(Problem):
|
||||
if(not self.isSum(abund, cnt)):
|
||||
self.sum += cnt
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
#Reserve the size of the array to speed up insertion
|
||||
def reserveArray(self):
|
||||
def reserveArray(self) -> None:
|
||||
#Make sure every element has a 0 in it's location
|
||||
for cnt in range(0, self.__maxNum):
|
||||
for _ in range(0, self.__maxNum):
|
||||
self.divisorSums.append(0)
|
||||
#A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
||||
def isSum(self, abund: list, num: int) -> bool:
|
||||
@@ -93,7 +94,7 @@ class Problem23(Problem):
|
||||
#If you have run through the entire list and did not find a sum then it is false
|
||||
return False
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.divisorSums.clear()
|
||||
self.reserveArray()
|
||||
@@ -101,16 +102,12 @@ class Problem23(Problem):
|
||||
|
||||
#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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the sum")
|
||||
self.solvedCheck("sum")
|
||||
return self.sum
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem24.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-24-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import StringAlgorithms
|
||||
|
||||
|
||||
class Problem24(Problem):
|
||||
@@ -34,13 +33,13 @@ class Problem24(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?")
|
||||
self.permutations = []
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -48,8 +47,10 @@ class Problem24(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get all permutations of the string
|
||||
self.permutations = Algorithms.getPermutations(self.__nums)
|
||||
self.permutations = StringAlgorithms.getPermutations(self.__nums)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
@@ -58,30 +59,23 @@ class Problem24(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the list of permutations")
|
||||
self.solvedCheck("permutations")
|
||||
return self.permutations
|
||||
#Returns the requested permutation
|
||||
def getPermutation(self) -> str:
|
||||
#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 ")
|
||||
self.solvedCheck("1,000,000th permutation")
|
||||
return self.permutations[self.__neededPerm - 1]
|
||||
|
||||
|
||||
|
||||
""" Results:
|
||||
|
||||
@@ -23,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem25(Problem):
|
||||
@@ -33,14 +32,14 @@ class Problem25(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?")
|
||||
self.number = 0 #The current Fibonacci number
|
||||
self.index = 2 #The index of the current Fibonacci number just calculated
|
||||
|
||||
#Operational functions
|
||||
#Sovle the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -48,10 +47,12 @@ class Problem25(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Move through all Fibonacci numbers until you reach the one with at least __numDigits digits
|
||||
while(len(str(self.number)) < self.__numDigits):
|
||||
self.index += 1 #Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop
|
||||
self.number = Algorithms.getFib(self.index) #Calculate the number
|
||||
self.number = NumberAlgorithms.getFib(self.index) #Calculate the number
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
@@ -60,30 +61,24 @@ class Problem25(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.number = 0
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the Fibonacci number")
|
||||
self.solvedCheck("fibonacci number")
|
||||
return self.number
|
||||
#Returns the index of the requested Fibonacci number
|
||||
def getIndex(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 index of the Fibonacci number")
|
||||
self.solvedCheck("index of the fibonacci number")
|
||||
return self.index
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem26.py
|
||||
#Matthew Ellison
|
||||
# Created: 07-29-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem26(Problem):
|
||||
@@ -32,14 +31,14 @@ class Problem26(Problem):
|
||||
|
||||
#Function
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.")
|
||||
self.longestCycle = 0
|
||||
self.longestNumber = 0
|
||||
|
||||
#Operational function
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,6 +46,7 @@ class Problem26(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with 1/2 and find out how long the longest cycle is by checking the remainders
|
||||
#Loop through every number from 2-999 and use it for the denominator
|
||||
for denominator in range(2, self.__topNumber):
|
||||
@@ -79,6 +79,7 @@ class Problem26(Problem):
|
||||
self.longestCycle = len(remainderList)
|
||||
self.longestNumber = denominator
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -86,30 +87,24 @@ class Problem26(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.longestCycle = 0
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the length of the longest cycle")
|
||||
self.solvedCheck("length of the longest cycle")
|
||||
return self.longestCycle
|
||||
#Returns the denominator that starts the longest cycle
|
||||
def getLongestNumber(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 denominaotr that started the cycle")
|
||||
self.solvedCheck("denominator that starts the longest cycle")
|
||||
return self.longestNumber
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem27.py
|
||||
#Matthew Ellison
|
||||
# Created: 09-15-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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,14 +23,13 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem27(Problem):
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("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")
|
||||
self.topA = 0 #The A for the most n's generated
|
||||
self.topB = 0 #The B for the most n's generated
|
||||
@@ -39,7 +38,7 @@ class Problem27(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,8 +46,9 @@ class Problem27(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the primes
|
||||
primes = Algorithms.getPrimes(12000) #A list of all primes that could possibly be generated with this formula
|
||||
primes = NumberAlgorithms.getPrimes(12000) #A list of all primes that could possibly be generated with this formula
|
||||
#Start with the lowest possible A and check all possibilities after that
|
||||
for a in range(-999, 999):
|
||||
#Start with the lowest possible B and check all possibilities after that
|
||||
@@ -67,6 +67,7 @@ class Problem27(Problem):
|
||||
self.topB = b
|
||||
self.topA = a
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -79,7 +80,7 @@ class Problem27(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.topA = 0
|
||||
self.topB = 0
|
||||
@@ -88,34 +89,31 @@ class Problem27(Problem):
|
||||
|
||||
#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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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")
|
||||
self.solvedCheck("largest 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")
|
||||
self.solvedCheck("largest 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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the top N")
|
||||
self.solvedCheck("largest N")
|
||||
return self.topA
|
||||
|
||||
#Returns the product of A and B for the answer
|
||||
def getProduct(self) -> int:
|
||||
self.solvedCheck("product of A and B")
|
||||
return self.topA * self.topB
|
||||
|
||||
|
||||
""" Results:
|
||||
The greatest number of primes found is 70
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem28.py
|
||||
#Matthew Ellison
|
||||
# Created: 09-22-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem28(Problem):
|
||||
@@ -31,13 +30,13 @@ class Problem28(Problem):
|
||||
grid = []
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("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?")
|
||||
self.sumOfDiagonals = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -45,11 +44,13 @@ class Problem28(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Setup the grid
|
||||
self.grid = self.setupGrid()
|
||||
#Find the sum of the diagonals in the grid
|
||||
self.sumOfDiagonals = self.findSum()
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -120,25 +121,21 @@ class Problem28(Problem):
|
||||
return sumOfDiagonals
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
#Returns the sum of the diagonals
|
||||
def getSum(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 sum")
|
||||
self.solvedCheck("sum")
|
||||
return self.sumOfDiagonals
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem29.py
|
||||
#Matthew Ellison
|
||||
# Created: 10-10-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem29(Problem):
|
||||
@@ -35,13 +34,13 @@ class Problem29(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?")
|
||||
self.unique = []
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -49,6 +48,7 @@ class Problem29(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with the first A and move towards the top
|
||||
for currentA in range(self.__bottomA, self.__topA + 1):
|
||||
#Start with the first B and move towards the top
|
||||
@@ -59,6 +59,7 @@ class Problem29(Problem):
|
||||
if currentNum not in self.unique:
|
||||
self.unique.append(currentNum)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -66,47 +67,39 @@ class Problem29(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the lowest possible A")
|
||||
def getBottomA(self) -> int:
|
||||
self.solvedCheck("lowest a")
|
||||
return self.__bottomA
|
||||
#Returns the lowest possible value for a
|
||||
def getTopA(self):
|
||||
#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 highest possible A")
|
||||
def getTopA(self) -> int:
|
||||
self.solvedCheck("highest a")
|
||||
return self.__topA
|
||||
#Returns the lowest possible value for a
|
||||
def getBottomB(self):
|
||||
#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 lowest possible B")
|
||||
def getBottomB(self) -> int:
|
||||
self.solvedCheck("lowest b")
|
||||
return self.__bottomB
|
||||
#Returns the lowest possible value for a
|
||||
def getTopB(self):
|
||||
#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 highest possible B")
|
||||
def getTopB(self) -> int:
|
||||
self.solvedCheck("highest b")
|
||||
return self.__topB
|
||||
#Returns a list of all unique values for a^b
|
||||
def getUnique(self) -> list:
|
||||
#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 list of unique values")
|
||||
self.solvedCheck("unique values for a^b")
|
||||
return self.unique
|
||||
#Returns the number of unique values for a^b
|
||||
def getNumUnique(self) -> int:
|
||||
self.solvedCheck("number of unique values for a^b")
|
||||
return len(self.unique)
|
||||
|
||||
|
||||
""" Results:
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem3.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-27-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Algorithms import getFactors
|
||||
from Unsolved import Unsolved
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem3(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem3(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the largest prime factor of 600851475143?")
|
||||
self.factors = []
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,10 +46,12 @@ class Problem3(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the factors of the number
|
||||
self.factors = getFactors(self.__goalNumber)
|
||||
self.factors = NumberAlgorithms.getFactors(self.__goalNumber)
|
||||
#The last element should be the largest factor
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -58,32 +59,23 @@ class Problem3(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the factors")
|
||||
self.solvedCheck("factors")
|
||||
return self.factors
|
||||
#Returns the largest factor of the number
|
||||
def getLargestFactor(self) -> int:
|
||||
#If the problem hasn't been solved throw an exceptions
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the largest factor")
|
||||
self.solvedCheck("largest factor")
|
||||
return self.factors[(len(self.factors) - 1)]
|
||||
#Returns the number for which we are getting the factor
|
||||
def getGoalNumber(self) -> int:
|
||||
return self.__goalNumber
|
||||
|
||||
|
||||
"""Results:
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem30.py
|
||||
#Matthew Ellison
|
||||
# Created: 10-28-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem30(Problem):
|
||||
@@ -34,14 +33,14 @@ class Problem30(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.")
|
||||
self.sumOfFifthNumbers = [] #This is an ArrayList of the numbers that are the sum of the fifth power of their digits
|
||||
self.sum = 0 #This is the sum of the sumOfFifthNumbers list
|
||||
|
||||
#Operational function
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -49,6 +48,7 @@ class Problem30(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with the lowest number and increment until you reach the largest number
|
||||
for currentNum in range(self.__bottomNum, self.__topNum):
|
||||
#Get the digits of the number
|
||||
@@ -62,6 +62,7 @@ class Problem30(Problem):
|
||||
if(sumOfPowers == currentNum):
|
||||
self.sumOfFifthNumbers.append(currentNum)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -80,46 +81,29 @@ class Problem30(Problem):
|
||||
return listOfDigits
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the top number")
|
||||
self.solvedCheck("largest number checked")
|
||||
return self.__topNum
|
||||
#Returns a copy of the vector holding all the number that are the sum of the fifth powers of their digits
|
||||
def getListOfSumsOfFifths(self) -> list:
|
||||
#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 list")
|
||||
self.solvedCheck("list of all numbers that are the sum of the 5th power of their digits")
|
||||
return self.sumOfFifthNumbers
|
||||
#Returns the sum of all entries in sumOfFifthNumbers
|
||||
def getSumOfList(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 sum of the list")
|
||||
self.solvedCheck("sum of all numbers that are the sum of the 5th power of their digits")
|
||||
return sum(self.sumOfFifthNumbers)
|
||||
|
||||
|
||||
#This calls the appropriate functions if the script is called stand alone
|
||||
if __name__ == "__main__":
|
||||
problem = Problem30()
|
||||
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 the numbers that can be written as the sum of the fifth powers of their digits is 443839
|
||||
It took an average of 4.068 seconds to run this problem through 100 iterations
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem31.py
|
||||
#Matthew Ellison
|
||||
# Created: 06-19-20
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem31(Problem):
|
||||
@@ -32,13 +31,13 @@ class Problem31(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("How many different ways can 2 pounds be made using any number of coins?")
|
||||
self.permutations = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -46,6 +45,7 @@ class Problem31(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with 200p and remove the necessary coins with each loop
|
||||
for pound2 in range(self.__desiredValue, -1, -200):
|
||||
for pound1 in range(pound2, -1, -100):
|
||||
@@ -56,6 +56,7 @@ class Problem31(Problem):
|
||||
for pence2 in range(pence5, -1, -2):
|
||||
self.permutations += 1
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -63,22 +64,18 @@ class Problem31(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the number of permutations")
|
||||
self.solvedCheck("number of correct permutations of the coins")
|
||||
return self.permutations
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/ProjectEulerPython/Problems/Problem32.py
|
||||
#Matthew Ellison
|
||||
# Created: 07-28-20
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,24 +23,23 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem32(Problem):
|
||||
#Structures
|
||||
class ProductSet:
|
||||
def __init__(self, multiplicand: int, multiplier: int):
|
||||
def __init__(self, multiplicand: int, multiplier: int) -> None:
|
||||
self.multiplicand = multiplicand
|
||||
self.multiplier = multiplier
|
||||
def getProduct(self) -> int:
|
||||
return (self.multiplicand * self.multiplier)
|
||||
def getNumString(self) -> str:
|
||||
return (str(self.multiplicand) + str(self.multiplier) + str(self.getProduct()))
|
||||
def __str__(self):
|
||||
def __str__(self) -> str:
|
||||
return (str(self.multiplicand) + " x " + str(self.multiplier) + " = " + str(self.getProduct()))
|
||||
def __repr__(self):
|
||||
def __repr__(self) -> str:
|
||||
return self.__str__()
|
||||
def __eq__(self, secondSet):
|
||||
def __eq__(self, secondSet) -> bool:
|
||||
return (self.getProduct() == secondSet.getProduct())
|
||||
|
||||
#Variables
|
||||
@@ -50,14 +49,14 @@ class Problem32(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.")
|
||||
self.listOfProducts = [] #The list of unique products that are 1-9 pandigital
|
||||
self.sumOfPandigitals = 0 #The sum of the products of the pandigital numbers
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -65,6 +64,7 @@ class Problem32(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Create the multiplicand and start working your way up
|
||||
for multiplicand in range(1, self.__topMultiplicand + 1):
|
||||
#Run through all possible multipliers
|
||||
@@ -82,6 +82,7 @@ class Problem32(Problem):
|
||||
for prod in self.listOfProducts:
|
||||
self.sumOfPandigitals += prod.getProduct()
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -104,23 +105,19 @@ class Problem32(Problem):
|
||||
return True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.listOfProducts.clear()
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before can you see the sum of the pandigitals")
|
||||
def getSumOfPandigitals(self) -> int:
|
||||
self.solvedCheck("sum of the pandigitals")
|
||||
return self.sumOfPandigitals
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem33.py
|
||||
#Matthew Ellison
|
||||
# Created: 02-07-21
|
||||
#Modified: 02-07-21
|
||||
#Modified: 07-24-21
|
||||
"""
|
||||
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s
|
||||
We shall consider fractions like, 30/50 = 3/5, to be trivial examples
|
||||
@@ -28,9 +28,7 @@ If the product of these four fractions is given in its lowest common terms, find
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
import Algorithms
|
||||
import ArrayAlgorithms
|
||||
|
||||
|
||||
class Problem33(Problem):
|
||||
@@ -47,12 +45,12 @@ class Problem33(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("If the product of these four fractions is given in its lowest common terms, find the value of the denominator")
|
||||
|
||||
#Operational Functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -60,6 +58,7 @@ class Problem33(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Search every possible numerator/denominator pair
|
||||
for denominator in range(self.__minDenominator, self.__maxDenominator + 1):
|
||||
for numerator in range(self.__minNumerator, denominator):
|
||||
@@ -93,13 +92,14 @@ class Problem33(Problem):
|
||||
self.__denominators.append(denominator)
|
||||
|
||||
#Get the product of the numbers
|
||||
numProd = Algorithms.prod(self.__numerators)
|
||||
denomProd = Algorithms.prod(self.__denominators)
|
||||
numProd = ArrayAlgorithms.prod(self.__numerators)
|
||||
denomProd = ArrayAlgorithms.prod(self.__denominators)
|
||||
#Get the gcd to reduce to lowest terms
|
||||
gcd = Algorithms.gcd(numProd, denomProd)
|
||||
gcd = ArrayAlgorithms.gcd(numProd, denomProd)
|
||||
#Save the denominator
|
||||
self.prodDenominator = int(denomProd / gcd)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -107,40 +107,33 @@ class Problem33(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.__numerators.clear()
|
||||
self.__denominators.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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
return f"The denominator of the product is {self.prodDenominator}"
|
||||
|
||||
#Returns the list of numerators
|
||||
def getNumerators(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 numerators")
|
||||
def getNumerators(self) -> list:
|
||||
self.solvedCheck("list of numerators")
|
||||
return self.__numerators
|
||||
|
||||
#Returns the list of denominators
|
||||
def getDenominators(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 denominators")
|
||||
def getDenominators(self) -> list:
|
||||
self.solvedCheck("list of denominators")
|
||||
return self.__denominator
|
||||
|
||||
#Returns the answer to the question
|
||||
def getProdDenominator(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 answer")
|
||||
def getProdDenominator(self) -> int:
|
||||
self.solvedCheck("denominator")
|
||||
return self.prodDenominator
|
||||
|
||||
|
||||
"""Results:
|
||||
The denominator of the product is 100
|
||||
It took an average of 5.130 milliseconds to run this problem through 100 iterations
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/ProjectEulerPython/Problems/Problem34.py
|
||||
#Matthew Ellison
|
||||
# Created: 06-01-21
|
||||
#Modified: 06-01-21
|
||||
#Modified: 07-21-21
|
||||
#Find the sum of all numbers which are equal to the sum of the factorial 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,9 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem34(Problem):
|
||||
@@ -34,7 +32,7 @@ class Problem34(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all numbers which are equal to the sum of the factorial of their digits")
|
||||
self.totalSum = 0
|
||||
self.factorials = []
|
||||
@@ -43,7 +41,7 @@ class Problem34(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -51,9 +49,10 @@ class Problem34(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Pre-compute the possible factorials from 0! to 9!
|
||||
for cnt in range(0, 10):
|
||||
self.factorials[cnt] = Algorithms.factorial(cnt)
|
||||
self.factorials[cnt] = NumberAlgorithms.factorial(cnt)
|
||||
#Run through all possible numbers from 3-MAX_NUM and see if they equal the sum of their digit's factorials
|
||||
for cnt in range(3, self.__max_num + 1):
|
||||
numString = str(cnt)
|
||||
@@ -64,6 +63,7 @@ class Problem34(Problem):
|
||||
if(currentSum == cnt):
|
||||
self.totalSum += currentSum
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -71,7 +71,7 @@ class Problem34(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.totalSum = 0
|
||||
self.factorials = []
|
||||
@@ -80,24 +80,19 @@ class Problem34(Problem):
|
||||
|
||||
#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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
return f"The sum of all numbers that are the sum of their digit's factorials is {self.totalSum}"
|
||||
#Returns the list of factorials from 0-9
|
||||
def getFactorials(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")
|
||||
def getFactorials(self) -> list:
|
||||
self.solvedCheck("list of factorials")
|
||||
return self.factorials
|
||||
#Returns the sum of all numbers equal to the sum of their digit's factorials
|
||||
def getSum(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")
|
||||
def getSum(self) -> int:
|
||||
self.solvedCheck("sum")
|
||||
return self.totalSum
|
||||
|
||||
|
||||
""" Results:
|
||||
The sum of all numbers that are the sum of their digit's factorials is 40730
|
||||
It took an average of 2.337 seconds to run this problem through 100 iterations
|
||||
|
||||
@@ -23,9 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem35(Problem):
|
||||
@@ -34,7 +32,7 @@ class Problem35(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all numbers which are equal to the sum of the factorial of their digits")
|
||||
self.primes = []
|
||||
self.circularPrimes = []
|
||||
@@ -50,7 +48,7 @@ class Problem35(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -58,8 +56,9 @@ class Problem35(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get all primes under 1,000,000
|
||||
self.primes = Algorithms.getPrimes(self.__max_num)
|
||||
self.primes = NumberAlgorithms.getPrimes(self.__max_num)
|
||||
#Go through all primes, get all their rotations, and check if those numbers are also primes
|
||||
for prime in self.primes:
|
||||
allRotationsPrime = True
|
||||
@@ -74,6 +73,7 @@ class Problem35(Problem):
|
||||
if(allRotationsPrime):
|
||||
self.circularPrimes.append(prime)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -81,7 +81,7 @@ class Problem35(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.primes = []
|
||||
self.circularPrimes = []
|
||||
@@ -89,27 +89,19 @@ class Problem35(Problem):
|
||||
#Gets
|
||||
#Returns a string with the solution to the problem
|
||||
def getResult(self) -> str:
|
||||
#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")
|
||||
self.solvedCheck("result")
|
||||
return f"The number of all circular prime numbers under {self.__max_num} is {len(self.circularPrimes)}"
|
||||
#Returns the list of primes < max_num
|
||||
def getPrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the primes")
|
||||
self.solvedCheck("list of primes")
|
||||
return self.primes
|
||||
#Returns the list of circular primes < max_num
|
||||
def getCircularPrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the circular primes")
|
||||
self.solvedCheck("list of circular primes")
|
||||
return self.circularPrimes
|
||||
#Returns the number of circular primes
|
||||
def getNumCircularPrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the number of circular primes")
|
||||
self.solvedCheck("number of circular primes")
|
||||
return len(self.circularPrimes)
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/ProjectEulerPython/Problems/Problem36.py
|
||||
#Matthew Ellison
|
||||
# Created: 06-29-21
|
||||
#Modified: 06-29-21
|
||||
#Modified: 07-24-21
|
||||
#Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
|
||||
#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,8 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
import StringAlgorithms
|
||||
|
||||
|
||||
class Problem36(Problem):
|
||||
@@ -34,14 +33,14 @@ class Problem36(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.")
|
||||
self.palindromes = []
|
||||
self.sumOfPal = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -49,18 +48,20 @@ class Problem36(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with 1, check if it is a palindrome in base 10 and 2, and continue to __max_num
|
||||
for num in range(1, self.__max_num + 1):
|
||||
#Check if num is a palindrome
|
||||
if(Algorithms.isPalindrome(str(num))):
|
||||
if(StringAlgorithms.isPalindrome(str(num))):
|
||||
#Convert num to base 2 and see if that is a palindrome
|
||||
binNum = Algorithms.toBin(num)
|
||||
if(Algorithms.isPalindrome(binNum)):
|
||||
binNum = NumberAlgorithms.toBin(num)
|
||||
if(StringAlgorithms.isPalindrome(binNum)):
|
||||
#Add num to the list of palindromes
|
||||
self.palindromes.append(num)
|
||||
#Get the sum of all palindromes in the list
|
||||
self.sumOfPal = sum(self.palindromes)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -68,7 +69,7 @@ class Problem36(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.palindromes = []
|
||||
self.sum = 0
|
||||
@@ -76,23 +77,18 @@ class Problem36(Problem):
|
||||
#Gets
|
||||
#Returns a string with the solution to the problem
|
||||
def getResult(self) -> str:
|
||||
#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")
|
||||
self.solvedCheck("result")
|
||||
return f"The sum of all base 10 and base 2 palindromic numbers < {self.__max_num} is {self.sumOfPal}"
|
||||
#Return the list of palindromes < MAX_NUM
|
||||
def getPalindromes(self) -> list:
|
||||
#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 palindromes")
|
||||
self.solvedCheck("list of palindromes")
|
||||
return self.palindromes
|
||||
#Return the sum of all elements in the list of palindromes
|
||||
def getSumOfPalindromes(self) -> int:
|
||||
#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 sum of the palindromes")
|
||||
self.solvedCheck("sum of all palindromes")
|
||||
return self.sumOfPal
|
||||
|
||||
|
||||
""" Results:
|
||||
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187
|
||||
It took an average of 295.861 milliseconds to run this problem through 100 iterations
|
||||
|
||||
@@ -23,9 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem37(Problem):
|
||||
@@ -34,14 +32,14 @@ class Problem37(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).")
|
||||
self.truncPrimes = [] #All numbers that are truncatable primes
|
||||
self.sumOfTruncPrimes = 0 #The sum of all elements in truncPrimes
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -49,8 +47,9 @@ class Problem37(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Create the sieve and get the first prime number
|
||||
sieve = Algorithms.primeGenerator()
|
||||
sieve = NumberAlgorithms.primeGenerator()
|
||||
currentPrime = next(sieve)
|
||||
#Loop through the sieve until you get to __last_prime_before_check
|
||||
while(currentPrime < self.__last_prime_before_check):
|
||||
@@ -77,7 +76,7 @@ class Problem37(Problem):
|
||||
primeSubstring = primeString[truncLoc::]
|
||||
#Convert the string to an int and see if the number is still prime
|
||||
newPrime = int(primeSubstring)
|
||||
if(not Algorithms.isPrime(newPrime)):
|
||||
if(not NumberAlgorithms.isPrime(newPrime)):
|
||||
isTruncPrime = False
|
||||
break
|
||||
#Start removing digits from the right and see if the number stays prime
|
||||
@@ -87,7 +86,7 @@ class Problem37(Problem):
|
||||
primeSubstring = primeString[0:len(primeString) - truncLoc]
|
||||
#Convert the string to an int and see if the number is still prime
|
||||
newPrime = int(primeSubstring)
|
||||
if(not Algorithms.isPrime(newPrime)):
|
||||
if(not NumberAlgorithms.isPrime(newPrime)):
|
||||
isTruncPrime = False
|
||||
break
|
||||
#If the number remained prime through all operations add it to the vector
|
||||
@@ -99,6 +98,7 @@ class Problem37(Problem):
|
||||
#Get the sum of all elements in the truncPrimes vector
|
||||
self.sumOfTruncPrimes = sum(self.truncPrimes)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -106,7 +106,7 @@ class Problem37(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.truncPrimes = []
|
||||
self.sumOfTruncPrimes = 0
|
||||
@@ -114,23 +114,18 @@ class Problem37(Problem):
|
||||
#Gets
|
||||
#Returns a string with the solution to the problem
|
||||
def getResult(self) -> str:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the result")
|
||||
self.solvedCheck("result")
|
||||
return f"The sum of all left and right truncatable primes is {self.sumOfTruncPrimes}"
|
||||
#Returns the list of primes that can be truncated
|
||||
def getTruncatablePrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the truncatable primes")
|
||||
self.solvedCheck("list of truncatable primes")
|
||||
return self.truncPrimes
|
||||
#Returns the sum of all elements in truncPrimes
|
||||
def getSumOfPrimes(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the sum of the truncatable primes")
|
||||
self.solvedCheck("sum of truncatable primes")
|
||||
return self.sumOfTruncPrimes
|
||||
|
||||
|
||||
""" Results:
|
||||
The sum of all left and right truncatable primes is 748317
|
||||
It took an average of 224.657 milliseconds to run this problem through 100 iterations
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem4.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-28-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem4(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem4(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the largest palindrome made from the product of two 3-digit numbers")
|
||||
self.palindromes = [] #Holds all of the palindromes
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,6 +46,7 @@ class Problem4(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Loop through every number from __lowestNum to __highestNum twice and multiply every number together
|
||||
for firstNum in range(self.__lowestNum, self.__highestNum + 1):
|
||||
for secondNum in range(firstNum, self.__highestNum + 1): #You can start at num1 because 100 * 101 == 101 * 100
|
||||
@@ -61,6 +61,7 @@ class Problem4(Problem):
|
||||
#Sort the palindromes so that the last element is the largest
|
||||
self.palindromes.sort()
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -68,28 +69,22 @@ class Problem4(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the list of palindromes")
|
||||
self.solvedCheck("palindromes")
|
||||
return self.palindromes
|
||||
#Returns the largest palindrome
|
||||
def getLargestPalindrome(self) -> int:
|
||||
#If the problem hasn't been solved throw an exceptions
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the largest palindrome")
|
||||
self.solvedCheck("largest palindrome")
|
||||
return self.palindromes[len(self.palindromes) - 1]
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEulter/Python/Project5.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-28-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 Matthew Ellison
|
||||
|
||||
This program is free software: it 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem5(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem5(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?")
|
||||
self.smallestNum = 0
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,6 +46,7 @@ class Problem5(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start at 20 and loop through all numbers until it find one that works
|
||||
#It must be at least 20 to be divisible by 20
|
||||
numFound = False #A flag for finding the divisible number
|
||||
@@ -68,6 +68,7 @@ class Problem5(Problem):
|
||||
#Set the smallest number to the number we just found
|
||||
self.smallestNum = currentNum
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -75,22 +76,18 @@ class Problem5(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the requested number")
|
||||
self.solvedCheck("number")
|
||||
return self.smallestNum
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem6.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-28-19
|
||||
#Modified: 10-29-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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 Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem6(Problem):
|
||||
@@ -33,14 +32,14 @@ class Problem6(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.")
|
||||
self.sumOfSquares = 0 #Holds the sum of the squares of all the numbers
|
||||
self.squareOfSum = 0 #Holds the square of the sum of all the numbers
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -48,6 +47,7 @@ class Problem6(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Run through all numbers from 1-100 and add them to the approriate sums
|
||||
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
|
||||
@@ -55,6 +55,7 @@ class Problem6(Problem):
|
||||
#Square the normal sum
|
||||
self.squareOfSum *= self.squareOfSum
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -62,35 +63,27 @@ class Problem6(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.squareOfSum = 0
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the sum of squares")
|
||||
self.solvedCheck("sum of the squares")
|
||||
return self.sumOfSquares
|
||||
#Return the sqare of all of the sums
|
||||
def getSquareOfSum(self) -> int:
|
||||
#If the problem hasn't been solved throw an exceptions
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the square of sum")
|
||||
self.solvedCheck("square of the sums")
|
||||
return self.squareOfSum
|
||||
#Returns the requested difference
|
||||
def getDifference(self) -> int:
|
||||
#If the problem hasn't been solved throw an exceptions
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the difference between the sum of squares and square of sum")
|
||||
self.solvedCheck("difference between the two numbers")
|
||||
return abs(self.sumOfSquares - self.squareOfSum)
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem67.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-26-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#Find the maximum total from top to bottom
|
||||
"""
|
||||
59
|
||||
@@ -107,7 +107,7 @@
|
||||
"""
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -125,7 +125,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
@@ -238,7 +237,7 @@ class Problem67(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the maximum total from top to bottom")
|
||||
self.foundPoints = [] #For the points that I have already found the shortest distance to
|
||||
self.possiblePoints = [] #For the locations you are checking this round
|
||||
@@ -246,7 +245,7 @@ class Problem67(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -254,6 +253,7 @@ class Problem67(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Invert the list so that each element = 100 - element
|
||||
self.invert()
|
||||
|
||||
@@ -294,6 +294,7 @@ class Problem67(Problem):
|
||||
#Invert the list so it can be read again
|
||||
self.invert()
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -301,13 +302,13 @@ class Problem67(Problem):
|
||||
self.solved = True
|
||||
|
||||
#This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
|
||||
def invert(self):
|
||||
def invert(self) -> None:
|
||||
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):
|
||||
def removeIf(self, listNum: list, loc: tuple) -> None:
|
||||
location = 0
|
||||
while(location < len(listNum)):
|
||||
if((listNum[location].xLocation == loc.xLocation) and (listNum[location].yLocation == loc.yLocation)):
|
||||
@@ -316,7 +317,7 @@ class Problem67(Problem):
|
||||
location += 1
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.foundPoints.clear()
|
||||
self.possiblePoints.clear()
|
||||
@@ -324,33 +325,27 @@ class Problem67(Problem):
|
||||
|
||||
#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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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):
|
||||
raise Unsolved("You must solve the problem before you can get the pyramid")
|
||||
|
||||
results = ""
|
||||
self.solvedCheck("pyramid of numbers")
|
||||
pyramidString = ""
|
||||
#Loop through all elements of the list and print them
|
||||
for row in self.listNum:
|
||||
for column in row:
|
||||
results += "{:02d}".format(column)
|
||||
results += '\n'
|
||||
return results
|
||||
pyramidString += "{:02d}".format(column)
|
||||
pyramidString += '\n'
|
||||
return pyramidString
|
||||
#Returns the trail the algorithm took as a string
|
||||
def getTrail(self) -> str:
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the trail")
|
||||
self.solvedCheck("trail of the shortest path")
|
||||
#TODO: Implement this
|
||||
return ""
|
||||
#Returns the total that was asked for
|
||||
def getTotal(self) -> int:
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the total")
|
||||
self.solvedCheck("total")
|
||||
return self.actualTotal
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#Project Eulter/Python/Problem7.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-29-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
Copyright (C) 2021 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,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
from Algorithms import getNumPrimes
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem7(Problem):
|
||||
@@ -33,13 +32,13 @@ class Problem7(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("What is the 10001st prime number?")
|
||||
self.primes = []
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,8 +46,10 @@ class Problem7(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the correct number of primes
|
||||
self.primes = getNumPrimes(self.__numPrimes)
|
||||
self.primes = NumberAlgorithms.getNumPrimes(self.__numPrimes)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
@@ -57,22 +58,18 @@ class Problem7(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the requested prime")
|
||||
self.solvedCheck("prime")
|
||||
return self.primes[len(self.primes) - 1]
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#Project Euler/Python/Problem8.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-29-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
||||
"""
|
||||
73167176531330624919225119674426574742355349194934
|
||||
@@ -27,7 +27,7 @@
|
||||
"""
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -45,7 +45,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem8(Problem):
|
||||
@@ -55,14 +54,14 @@ class Problem8(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?")
|
||||
self.maxNums = "" #Holds the string of the largest product
|
||||
self.maxProduct = 0 #Holds the largest product of 13 numbers
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -70,6 +69,7 @@ class Problem8(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start at the 13th entry and multiply all single digit numbers before and including that number together
|
||||
cnt = 12 #The location in the number that you are working from
|
||||
for cnt in range(12, len(self.__number)):
|
||||
@@ -81,37 +81,32 @@ class Problem8(Problem):
|
||||
#Move to the next location
|
||||
cnt += 1
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop
|
||||
self.timer.stop()
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the nums the produce the largest product")
|
||||
self.solvedCheck("numbers that make the largest product")
|
||||
return self.maxNums
|
||||
#Returns the requested product
|
||||
def getLargestProduct(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the requested product")
|
||||
self.solvedCheck("product of the numbers")
|
||||
return self.maxProduct
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#Project Euler/Python/Problem9.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-29-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
"""
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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,14 +23,13 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import math
|
||||
|
||||
|
||||
class Problem9(Problem):
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.")
|
||||
self.a = 1
|
||||
self.b = 0
|
||||
@@ -39,7 +38,7 @@ class Problem9(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -47,6 +46,7 @@ class Problem9(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with the lowest possible a , 1, and search for the b and c to complete the triplet
|
||||
while((self.a <= (1000 / 3)) and (not self.found)):
|
||||
#Setup b and c
|
||||
@@ -66,6 +66,7 @@ class Problem9(Problem):
|
||||
else:
|
||||
self.a += 1
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -73,7 +74,7 @@ class Problem9(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.a = 1
|
||||
self.b = 0
|
||||
@@ -82,35 +83,25 @@ class Problem9(Problem):
|
||||
|
||||
#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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("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
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length of the first side")
|
||||
self.solvedCheck("first side")
|
||||
return self.a
|
||||
#Returns the length of the second side
|
||||
def getSideB(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length of the second side")
|
||||
self.solvedCheck("second side")
|
||||
return self.b
|
||||
#Returns the length of the hyp
|
||||
def getSideC(self) -> float:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length of the hyp")
|
||||
self.solvedCheck("third side")
|
||||
return self.c
|
||||
#Returns the product of the 3 sides
|
||||
def getProduct(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can get the length first side")
|
||||
self.solvedCheck("product of all three sides")
|
||||
return int(self.a * self.b * self.c)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user