본문 바로가기

lru_cache

(3)
[EP 077] 소수의 합으로 나타내는 경우의 수 오일러 프로젝트 77번 문제. 10을 소수들의 합으로 나누는 경우의 수는 다음과 같이 다섯가지이다. 7+3 5+5 5+3+2 3+3+2+2 2+2+2+2+2 소수들의 합으로 나누는 경우의 수가 처음으로 5000을 넘는 수는 무엇인가? from typing import Set, Dict from functools import lru_cache from pprint import pprint primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] @lru_cache() def summations(n: int) -> Set: # print(">"*depth + str(n)) # ..
[EP 092] T(n) = n의 각 자리수의 제곱의 합. T(T(T(..(n)...))) = n 이 되는 n은 1과 89 # http://projecteuler.net # Problem 92 # 2 2 2 2 2 # 89 -> 8 + 9 = 145 -> 1 + 4 + 5 = 42 -> 20 -> 16 -> 37 -> 58 -> 89 -> ... -> 89 # 1 -> 1 -> 1 from functools import lru_cache import time from tqdm import tqdm BIGN = 10 ** 7 sol_dict = dict() sol_dict[0] = 0 sol_dict[1] = 1 sol_dict[89] = 89 digit_sq = [i * i for i in range(10)] @lru_cache def arrives_at_89_recur(n: int): if n == 0 or n == 1 o..
nCr 캐시된 재귀함수로 구하기 Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32Type "copyright", "credits" or "license()" for more information.>>> def nCr(n, r):if r in (n, 0);SyntaxError: invalid syntax>>> @functools.lur_cache(maxsize=200, typed=False)def nCr(n, r):if r in (n, 0):return 1return nCr(n-1, r-1) + nCr(n-1, r) Traceback (most recent call last): File "", line 1, in @fu..