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