From 373fef7183e82e38f405b0701cf5d72dd81c41a0 Mon Sep 17 00:00:00 2001 From: pmpknu Date: Fri, 11 Oct 2024 15:20:00 +0300 Subject: [PATCH] feat(p12): add solution in python --- p12/solution.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 p12/solution.py diff --git a/p12/solution.py b/p12/solution.py new file mode 100644 index 0000000..6b09f8d --- /dev/null +++ b/p12/solution.py @@ -0,0 +1,22 @@ +import math + +def count_divisors(n): + sqrt_n = int(math.sqrt(n)) + count = sum(2 for i in range(1, sqrt_n + 1) if n % i == 0) + if sqrt_n * sqrt_n == n: + count -= 1 + return count + +def first_triangle_number_with_divisors(limit): + n = 1 + triangle = 1 + while True: + divisors = count_divisors(triangle) + if divisors > limit: + return triangle + n += 1 + triangle += n + +limit = 500 +result = first_triangle_number_with_divisors(limit) +print(result) \ No newline at end of file