project-euler-100

[RADIOACTIVE] solutions to the first 100 challenges of project euler
git clone git://git.figbert.com/project-euler-100.git
Log | Files | Refs | README

problem012.py (816B)


      1 # problemName = "Highly divisible triangular number"
      2 # problemNum = 12
      3 # solutionBy = "FIGBERT"
      4 # language = "Python"
      5 # dateCompleted = "05/02/2020"
      6 from functools import reduce
      7 from math import sqrt
      8 
      9 def more_than_five_hundred_factors(num):
     10     factors = len(list(reduce(
     11         list.__add__, (
     12             [i, num//i] for i in range(1, int(sqrt(num)) + 1) if num % i == 0
     13         )
     14     )))
     15     return num if factors > 500 else False
     16 
     17 def triangle(pos):
     18     val = 0
     19     for i in range(1, pos + 1):
     20         val += i
     21     return more_than_five_hundred_factors(val)
     22 
     23 if __name__ == "__main__":
     24     answer = 2
     25     while not triangle(answer):
     26         answer += 1
     27     answer = triangle(answer)
     28     print((
     29         "The value of the first triangle number to have over "
     30         f"five hundred divisors is {answer}"
     31     ))