Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
36
Task/Stern-Brocot-sequence/Python/stern-brocot-sequence-1.py
Normal file
36
Task/Stern-Brocot-sequence/Python/stern-brocot-sequence-1.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
def stern_brocot(predicate=lambda series: len(series) < 20):
|
||||
"""\
|
||||
Generates members of the stern-brocot series, in order, returning them when the predicate becomes false
|
||||
|
||||
>>> print('The first 10 values:',
|
||||
stern_brocot(lambda series: len(series) < 10)[:10])
|
||||
The first 10 values: [1, 1, 2, 1, 3, 2, 3, 1, 4, 3]
|
||||
>>>
|
||||
"""
|
||||
|
||||
sb, i = [1, 1], 0
|
||||
while predicate(sb):
|
||||
sb += [sum(sb[i:i + 2]), sb[i + 1]]
|
||||
i += 1
|
||||
return sb
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from fractions import gcd
|
||||
|
||||
n_first = 15
|
||||
print('The first %i values:\n ' % n_first,
|
||||
stern_brocot(lambda series: len(series) < n_first)[:n_first])
|
||||
print()
|
||||
n_max = 10
|
||||
for n_occur in list(range(1, n_max + 1)) + [100]:
|
||||
print('1-based index of the first occurrence of %3i in the series:' % n_occur,
|
||||
stern_brocot(lambda series: n_occur not in series).index(n_occur) + 1)
|
||||
# The following would be much faster. Note that new values always occur at odd indices
|
||||
# len(stern_brocot(lambda series: n_occur != series[-2])) - 1)
|
||||
|
||||
print()
|
||||
n_gcd = 1000
|
||||
s = stern_brocot(lambda series: len(series) < n_gcd)[:n_gcd]
|
||||
assert all(gcd(prev, this) == 1
|
||||
for prev, this in zip(s, s[1:])), 'A fraction from adjacent terms is reducible'
|
||||
22
Task/Stern-Brocot-sequence/Python/stern-brocot-sequence-2.py
Normal file
22
Task/Stern-Brocot-sequence/Python/stern-brocot-sequence-2.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
>>> from itertools import takewhile, tee, islice
|
||||
>>> from collections import deque
|
||||
>>> from fractions import gcd
|
||||
>>>
|
||||
>>> def stern_brocot():
|
||||
sb = deque([1, 1])
|
||||
while True:
|
||||
sb += [sb[0] + sb[1], sb[1]]
|
||||
yield sb.popleft()
|
||||
|
||||
|
||||
>>> [s for _, s in zip(range(15), stern_brocot())]
|
||||
[1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4]
|
||||
>>> [1 + sum(1 for i in takewhile(lambda x: x != occur, stern_brocot()))
|
||||
for occur in (list(range(1, 11)) + [100])]
|
||||
[1, 3, 5, 9, 11, 33, 19, 21, 35, 39, 1179]
|
||||
>>> prev, this = tee(stern_brocot(), 2)
|
||||
>>> next(this)
|
||||
1
|
||||
>>> all(gcd(p, t) == 1 for p, t in islice(zip(prev, this), 1000))
|
||||
True
|
||||
>>>
|
||||
Loading…
Add table
Add a link
Reference in a new issue