Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,46 @@
|
|||
def ffr(n):
|
||||
if n < 1 or type(n) != int: raise ValueError("n must be an int >= 1")
|
||||
try:
|
||||
return ffr.r[n]
|
||||
except IndexError:
|
||||
r, s = ffr.r, ffs.s
|
||||
ffr_n_1 = ffr(n-1)
|
||||
lastr = r[-1]
|
||||
# extend s up to, and one past, last r
|
||||
s += list(range(s[-1] + 1, lastr))
|
||||
if s[-1] < lastr: s += [lastr + 1]
|
||||
# access s[n-1] temporarily extending s if necessary
|
||||
len_s = len(s)
|
||||
ffs_n_1 = s[n-1] if len_s > n else (n - len_s) + s[-1]
|
||||
ans = ffr_n_1 + ffs_n_1
|
||||
r.append(ans)
|
||||
return ans
|
||||
ffr.r = [None, 1]
|
||||
|
||||
def ffs(n):
|
||||
if n < 1 or type(n) != int: raise ValueError("n must be an int >= 1")
|
||||
try:
|
||||
return ffs.s[n]
|
||||
except IndexError:
|
||||
r, s = ffr.r, ffs.s
|
||||
for i in range(len(r), n+2):
|
||||
ffr(i)
|
||||
if len(s) > n:
|
||||
return s[n]
|
||||
raise Exception("Whoops!")
|
||||
ffs.s = [None, 2]
|
||||
|
||||
if __name__ == '__main__':
|
||||
first10 = [ffr(i) for i in range(1,11)]
|
||||
assert first10 == [1, 3, 7, 12, 18, 26, 35, 45, 56, 69], "ffr() value error(s)"
|
||||
print("ffr(n) for n = [1..10] is", first10)
|
||||
#
|
||||
bin = [None] + [0]*1000
|
||||
for i in range(40, 0, -1):
|
||||
bin[ffr(i)] += 1
|
||||
for i in range(960, 0, -1):
|
||||
bin[ffs(i)] += 1
|
||||
if all(b == 1 for b in bin[1:1000]):
|
||||
print("All Integers 1..1000 found OK")
|
||||
else:
|
||||
print("All Integers 1..1000 NOT found only once: ERROR")
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
cR = [1]
|
||||
cS = [2]
|
||||
|
||||
def extend_RS():
|
||||
x = cR[len(cR) - 1] + cS[len(cR) - 1]
|
||||
cR.append(x)
|
||||
cS += range(cS[-1] + 1, x)
|
||||
cS.append(x + 1)
|
||||
|
||||
def ff_R(n):
|
||||
assert(n > 0)
|
||||
while n > len(cR): extend_RS()
|
||||
return cR[n - 1]
|
||||
|
||||
def ff_S(n):
|
||||
assert(n > 0)
|
||||
while n > len(cS): extend_RS()
|
||||
return cS[n - 1]
|
||||
|
||||
# tests
|
||||
print([ ff_R(i) for i in range(1, 11) ])
|
||||
|
||||
s = {}
|
||||
for i in range(1, 1001): s[i] = 0
|
||||
for i in range(1, 41): del s[ff_R(i)]
|
||||
for i in range(1, 961): del s[ff_S(i)]
|
||||
|
||||
# the fact that we got here without a key error
|
||||
print("Ok")
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[1, 3, 7, 12, 18, 26, 35, 45, 56, 69]
|
||||
Ok
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
from itertools import islice
|
||||
|
||||
def R():
|
||||
n = 1
|
||||
yield n
|
||||
for s in S():
|
||||
n += s
|
||||
yield n;
|
||||
|
||||
def S():
|
||||
yield 2
|
||||
yield 4
|
||||
u = 5
|
||||
for r in R():
|
||||
if r <= u: continue;
|
||||
for x in range(u, r): yield x
|
||||
u = r + 1
|
||||
|
||||
def lst(s, n): return list(islice(s(), n))
|
||||
|
||||
print "R:", lst(R, 10)
|
||||
print "S:", lst(S, 10)
|
||||
print sorted(lst(R, 40) + lst(S, 960)) == list(range(1,1001))
|
||||
|
||||
# perf test case
|
||||
# print sum(lst(R, 10000000))
|
||||
Loading…
Add table
Add a link
Reference in a new issue