Skip to content

Instantly share code, notes, and snippets.

@voith
Created October 22, 2019 21:48
Show Gist options
  • Save voith/cf322de7fca1c8072d819ec111d536fe to your computer and use it in GitHub Desktop.
Save voith/cf322de7fca1c8072d819ec111d536fe to your computer and use it in GitHub Desktop.
First triangle number to have over five hundred divisors
from collections import defaultdict
from math import sqrt
def number_prime_factors(n):
factors = defaultdict(int)
while(n % 2 == 0):
factors[2] += 1
n = n // 2
for i in range(3, int(sqrt(n)) + 1, 2):
while(n % i == 0):
factors[i] += 1
n = n // i
if n > 2:
factors[n] = 1
total_factors = 1
for exp in factors.values():
total_factors *= (1 + exp)
return total_factors
no_to_process = 100000000
_sum = 1
no_divisors_to_exceed = 500
for i in range(2, no_to_process):
_sum += i
no_of_divisors = number_prime_factors(_sum)
if no_of_divisors > no_divisors_to_exceed:
print(_sum)
break
else:
raise Exception('Increase no_to_process')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment