commit 4f10a074682967a4c4ff5d30912c689fea38524f parent b871c759eb0e97ee9c040d89cd8db1e359275e7b Author: therealFIGBERT <figbertwelner@gmail.com> Date: Wed, 5 Feb 2020 22:56:59 -0800 :sparkles: Add solution to problem 12 Diffstat:
A | problem012.py | | | 24 | ++++++++++++++++++++++++ |
1 file changed, 24 insertions(+), 0 deletions(-)
diff --git a/problem012.py b/problem012.py @@ -0,0 +1,24 @@ +# problemName = "Highly divisible triangular number" +# problemNum = 12 +# solutionBy = "FIGBERT" +# language = "Python" +# dateCompleted = "05/02/2020" +from functools import reduce +from math import sqrt +import time + +def moreThanFiveHundredFactors(num): + factors = len(list(reduce(list.__add__, ([i, num//i] for i in range(1, int(sqrt(num)) + 1) if num % i == 0)))) + return num if factors > 500 else False + +def triangle(pos): + val = 0 + for i in range(1, pos + 1): + val += i + return moreThanFiveHundredFactors(val) + +answer = 2 +while not triangle(answer): + answer += 1 +answer = triangle(answer) +print(f"The value of the first triangle number to have over five hundred divisors is {answer}")