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 @@
|
||||
#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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user