From 458979e55a906afe2fabe9879f01edf421bd6f73 Mon Sep 17 00:00:00 2001 From: Sidney Zhang Date: Mon, 20 Apr 2026 16:55:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(0053):=20=E6=B7=BB=E5=8A=A0=E7=BB=84?= =?UTF-8?q?=E5=90=88=E6=95=B0=E8=B6=85=E7=99=BE=E4=B8=87=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solutions/0053.Combinatoric/euler_53.py | 58 +++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 solutions/0053.Combinatoric/euler_53.py diff --git a/solutions/0053.Combinatoric/euler_53.py b/solutions/0053.Combinatoric/euler_53.py new file mode 100644 index 0000000..7109477 --- /dev/null +++ b/solutions/0053.Combinatoric/euler_53.py @@ -0,0 +1,58 @@ +""" +There are exactly ten ways of selecting three from five, 12345: + +123, 124, 125, 134, 135, 145, 234, 235, 245, and 345 + +In combinatorics, we use the notation, (5 3) = 10 . + +In general, (n r) = n! / (r! * (n - r)!) , where r ≤ n , n!=n×(n-1)×...×3×2×1 , and 0!=1 . + +It is not until n=23 , that a value exceeds one-million: (23 10) = 1144066 . + +How many, not necessarily distinct, values of (n r) for 1 ≤ n ≤ 100 , are greater than one-million? +""" + +import time +from functools import lru_cache + + +def timer(func): + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + elapsed_time = end_time - start_time + print(f"{func.__name__} time: {elapsed_time:.6f} seconds") + return result + + return wrapper + + +def comfun(n, r): + return factorial(n) // (factorial(r) * factorial(n - r)) + + +@lru_cache(maxsize=None) +def factorial(n: int, s: int = 1): + if n < s: + return 1 + elif n == s: + if s < 1: + return 1 + return s + else: + return n * factorial(n - 1) + + +@timer +def main(): + num = 0 + for i in range(1, 101): + for j in range(1, i + 1): + if comfun(i, j) > 1000000: + num += 1 + print(num) + + +if __name__ == "__main__": + main()