Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
42
Task/Sylvesters-sequence/Python/sylvesters-sequence-1.py
Normal file
42
Task/Sylvesters-sequence/Python/sylvesters-sequence-1.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
'''Sylvester's sequence'''
|
||||
|
||||
from functools import reduce
|
||||
from itertools import count, islice
|
||||
|
||||
|
||||
# sylvester :: [Int]
|
||||
def sylvester():
|
||||
'''Non-finite stream of the terms
|
||||
of Sylvester's sequence.
|
||||
(OEIS A000058)
|
||||
'''
|
||||
def go(n):
|
||||
return 1 + reduce(
|
||||
lambda a, x: a * go(x),
|
||||
range(0, n),
|
||||
1
|
||||
) if 0 != n else 2
|
||||
|
||||
return map(go, count(0))
|
||||
|
||||
|
||||
# ------------------------- TEST -------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''First terms, and sum of reciprocals.'''
|
||||
|
||||
print("First 10 terms of OEIS A000058:")
|
||||
xs = list(islice(sylvester(), 10))
|
||||
print('\n'.join([
|
||||
str(x) for x in xs
|
||||
]))
|
||||
|
||||
print("\nSum of the reciprocals of the first 10 terms:")
|
||||
print(
|
||||
reduce(lambda a, x: a + 1 / x, xs, 0)
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
48
Task/Sylvesters-sequence/Python/sylvesters-sequence-2.py
Normal file
48
Task/Sylvesters-sequence/Python/sylvesters-sequence-2.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
'''Sylvester's sequence'''
|
||||
|
||||
from functools import reduce
|
||||
from itertools import islice
|
||||
|
||||
# sylvester :: [Int]
|
||||
def sylvester():
|
||||
'''A non finite sequence of the terms of OEIS A000058
|
||||
'''
|
||||
return iterate(
|
||||
lambda x: x * (x - 1) + 1
|
||||
)(2)
|
||||
|
||||
|
||||
# ------------------------- TEST -------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''First terms, and sum of reciprocals.'''
|
||||
|
||||
print("First 10 terms of OEIS A000058:")
|
||||
xs = list(islice(sylvester(), 10))
|
||||
print('\n'.join([
|
||||
str(x) for x in xs
|
||||
]))
|
||||
|
||||
print("\nSum of the reciprocals of the first 10 terms:")
|
||||
print(
|
||||
reduce(lambda a, x: a + 1 / x, xs, 0)
|
||||
)
|
||||
|
||||
# ----------------------- GENERIC ------------------------
|
||||
|
||||
# iterate :: (a -> a) -> a -> Gen [a]
|
||||
def iterate(f):
|
||||
'''An infinite list of repeated
|
||||
applications of f to x.
|
||||
'''
|
||||
def go(x):
|
||||
v = x
|
||||
while True:
|
||||
yield v
|
||||
v = f(v)
|
||||
return go
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue