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