他的姓氏@ gmail(.com)约2008,新的装饰者从7月20日开始,从2014到第二代(作为对Python中的概念的证明,也就是在Python中工作)——请自由地使用和改进这些想法,以供您对python/SAGE库等(希望同样开放的源代码)等等。至少:找到一个合适的整数类型,它可以存储任意大小的整数而不会丢失任何精度。Antti Karttunen(Email):他的第一个名字。 # # After that is done, one could port also other functionals from # https://github.com/karttu/IntSeq/blob/master/src/transforms.scm # to Python, e.g. RECORD-POS, DISTINCT-POS (use a hash table instead of vector) # etc. # # See also: # http://www.luschny.de/blog/The_Unofficial_Guide_to_Coding_for_the_OEIS.html # # # matching1pos renamed to CachedMatchesFrom0 # # Say: from memotest import * from array import * def gen_n_items(n,item): for i in xrange(n): yield(item) # About the same as CachedFunction in Sage: def memoed(fun): maxmemosize = 131 # 072 notfilled = 0L memo = array('L') def wrapper(n): memsiznow = len(memo) if None == n: return(memo) # For debugging elif n >= memsiznow: newsize = min(maxmemosize,max(n+1,2*memsiznow)) memo.extend(gen_n_items(newsize-memsiznow,notfilled)) res = memo[n] if(notfilled == res): res = fun(n) memo[n] = res return(res) else: return(res) return wrapper # The original, partly recursive implementation: # This corresponds approximately to (MATCHING-POS 1 0 fun) in that IntSeq-library. def CachedMatchesFrom0_recursive_implementation(fun): notfilled = 0L memo = array('L') def wrapper(n): memsiznow = len(memo) if None == n: return(memo) # For debugging elif n >= memsiznow: newsize = max(n+1,2*memsiznow) memo.extend(gen_n_items(newsize-memsiznow,notfilled)) res = memo[n] if(notfilled == res): # Not yet in memo? if 1 == n: # If the caller asks the first number satisfying the condition, start searching from n=1. i = 0 else: # Otherwise, start searching from one past where the prev term satisfying the condition was found # Note: with this stack might overflow if computing terms in non-monotone order and "peeking too much ahead": i = 1 + wrapper(n-1) # Then start searching the next positive integer matching the condition: while True: if fun(i): memo[n] = i return(i) else: # Keep on searching... i = i+1 else: return(res) # We had that term already memoized, just return it. return wrapper # This version works even when computing first say term a(10000) as it will # search and memoize all the values a(0) .. a(9999) internally, in a loop. # This corresponds approximately to (MATCHING-POS 1 0 fun) in my IntSeq-library. # Start searching from the integers k which result fun(k) = True from k=0 onward, # the first that is found will be the value of a(1) of the defined function. def CachedMatchesFrom0(fun): notfilled = 0L memo = array('L',[0]) def wrapper(n): memsiznow = len(memo) if None == n: return(memo) # For debugging elif n >= memsiznow: # We have to allocate more space for memo-vector. 在这里假设这样的γ再生是昂贵的,所以我们在成倍的大小的块中进行。(我不确定Python阵列是否是真的。在任意情况下,每增加一个元素调用Gunnn-项迭代器):{NexSime= max(n+1,2*MesiZWOW)〉备忘录。扩展(Gunnn-项(NexSead MesiZWOW,NoTrPult)){μ= MeMo(0)},如果(n=Um):返回(备忘录[n])已被计算,取备忘录中的值。如果0=Um:i=1,而备忘录仍然是空的?然后,搜索与条件匹配的下一个正整数:μ,当Um<n:y,直到最终找到所有匹配项到第n个,i i=i+1(i):(i):(i)如果整数i与条件匹配,则uM=Um + 1α更新备忘录中的最高计算项的索引[UM]=i,并将I存储到备忘录向量的备忘录[0 ]=Um中更新备忘录的“有效大小”。然后,我们从i=0,…..另一个开始搜索:i=备忘录[OM],否则从一个过去的最新整数匹配条件。(Here um = n). return(i) # Return the found i to the caller. return wrapper # This version corresponds approximately to (MATCHING-POS 1 1 fun) in IntSeq-library: # Start searching from the integers k which result fun(k) = True from k=1 onward, # the first that is found will be the value of a(1) of the defined function. def CachedMatchesFrom1(fun): notfilled = 0L memo = array('L',[0]) def wrapper(n): memsiznow = len(memo) if None == n: return(memo) # For debugging elif n >= memsiznow: # We have to allocate more space for memo-vector. 在这里假设这样的γ再生是昂贵的,所以我们在成倍的大小的块中进行。(我不确定Python阵列是否是真的。在任意情况下,每增加一个元素调用Gunnn-项迭代器):{NexSime= max(n+1,2*MesiZWOW)〉备忘录。扩展(Gunnn-项(NexSead MesiZWOW,NoTrPult)){μ= MeMo(0)},如果(n=Um):返回(备忘录[n])已被计算,取备忘录中的值。 # Get the latest positive integer which matched the condition (and was memoized) i = memo[um] # Note that if um = 0 then memo[0] = 0 and i will be 0 as well. # Then search for the next positive integers matching the condition: while um < n: # Until we have finally found all the matching terms up to the n-th one. i = i+1 if fun(i): # If the integer i matches the condition, um = um + 1 # update the index of the highest computed term in memo memo[um] = i # and store i into theat position in the memo-vector memo[0] = um # Update the "effective size" of memo. (Here um = n). return(i) # Return the found i to the caller. return wrapper ######################################################################## ### Usage examples ### # Try fibo(47) for example. (最后一段时间很长!) @memoed def fibo(n): '''Fibonakin luvut, A000045.''' if(n < 2): return(n) else: return(fibo(n-2)+fibo(n-1)) @memoed def A001477(n): '''Computes non-negative integers in funny way.''' if(n < 2): return(n) else: return(A001477(n-1)+(A001477(n-1)-A001477(n-2))) # A224694: Numbers n such that n^2 AND n = 0, where AND is the bitwise logical AND operator. def is_A224694(n): '''Does n satisfy: n^2 AND n = 0 ?''' return(0 == ((n*n) & n)) @CachedMatchesFrom0 def A224694(n): '''Numbers n such that n^2 AND n = 0, where AND is the bitwise logical AND operator.''' return(is_A224694(n)) # This is for testing the limits of recursion: @CachedMatchesFrom0_recursive_implementation def A224694_with_recursive_implementation(n): '''Numbers n such that n^2 AND n = 0, where AND is the bitwise logical AND operator.''' return(is_A224694(n)) # A213382: Numbers n such that n^n mod (n + 2) = n. def is_A213382(n): '''Does n satisfy: n^n mod (n + 2) = n ?''' # print "Calling is_A213382 with n=",n return(n == (n**n % (n+2))) @CachedMatchesFrom1 def A213382(n): return(is_A213382(n)) @CachedMatchesFrom0_recursive_implementation def A213382_with_recursive_implementation(n): '''Numbers n such that n^n mod (n + 2) = n.''' return(is_A213382(n)) # [A213382_with_recursive_implementation(10-n) for n in range(10)] # Calling is_A213382 with n= 1 # Calling is_A213382 with n= 2 # Calling is_A213382 with n= 3 # ... # Calling is_A213382 with n= 53 # Calling is_A213382 with n= 54 # Calling is_A213382 with n= 55 # [55, 49L, 37L, 31L, 19L, 16L, 13L, 7L, 4L, 1L] # # >>> A213382(None) # array('L', [0L]) # >>> A213382(1) # Calling is_A213382 with n= 1 # 1L # >>> A213382(5) # Calling is_A213382 with n= 2 # Calling is_A213382 with n= 3 # ... # Calling is_A213382 with n= 14 # Calling is_A213382 with n= 15 # Calling is_A213382 with n= 16 # 16L # >>> A213382(None) # array('L', [5L, 1L, 4L, 7L, 13L, 16L]) # >>> [A213382(10-n) for n in range(10)] # Calling is_A213382 with n= 17 # Calling is_A213382 with n= 18 # Calling is_A213382 with n= 19 # Calling is_A213382 with n= 20 # Calling is_A213382 with n= 21 # ... # Calling is_A213382 with n= 52 # Calling is_A213382 with n= 53 # Calling is_A213382 with n= 54 # Calling is_A213382 with n= 55 # [55L, 49L, 37L, 31L, 19L, 16L, 13L, 7L, 4L, 1L] # >>> [A213382(10-n) for n in range(10)] # [55L, 49L, 37L, 31L, 19L, 16L, 13L, 7L, 4L, 1L] # >>> # # [A224694(n+1) for n in range(20)] # [0L, 2L, 4L, 8L, 10L, 12L, 16L, 18L, 24L, 26L, 32L, 34L, 36L, 40L, 44L, 48L, 50L, 56L, 64L, 66L] # A224694_with_recursive_implementation(None) # array('L') # >>> [A224694_with_recursive_implementation(10-n) for n in range(10)] # [26, 24L, 18L, 16L, 12L, 10L, 8L, 4L, 2L, 0] # >>> A224694_with_recursive_implementation(None) # array('L', [0L, 0L, 2L, 4L, 8L, 10L, 12L, 16L, 18L, 24L, 26L]) # [A224694_with_recursive_implementation(n+1) for n in range(20)] # [0, 2, 4L, 8L, 10L, 12L, 16L, 18L, 24L, 26L, 32L, 34L, 36L, 40L, 44L, 48L, 50L, 56L, 64L, 66L] # Note: # >>> A224694(10000) # returns quickly 691968L # (Cf. https://oeis.org/A224694/b224694.txt ) # but # >>> A224694_with_recursive_implementation(10000) # results: # RuntimeError: maximum recursion depth exceeded in cmp # after a longish dump of lines like: # File "memotest.py", line 69, in wrapper # i = 1 + wrapper(n-1) #