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,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)))
|
||||
|
||||
Reference in New Issue
Block a user