Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/Calkin-Wilf-sequence/Python/calkin-wilf-sequence.py
Normal file
31
Task/Calkin-Wilf-sequence/Python/calkin-wilf-sequence.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from fractions import Fraction
|
||||
from math import floor
|
||||
from itertools import islice, groupby
|
||||
|
||||
|
||||
def cw():
|
||||
a = Fraction(1)
|
||||
while True:
|
||||
yield a
|
||||
a = 1 / (2 * floor(a) + 1 - a)
|
||||
|
||||
def r2cf(rational):
|
||||
num, den = rational.numerator, rational.denominator
|
||||
while den:
|
||||
num, (digit, den) = den, divmod(num, den)
|
||||
yield digit
|
||||
|
||||
def get_term_num(rational):
|
||||
ans, dig, pwr = 0, 1, 0
|
||||
for n in r2cf(rational):
|
||||
for _ in range(n):
|
||||
ans |= dig << pwr
|
||||
pwr += 1
|
||||
dig ^= 1
|
||||
return ans
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('TERMS 1..20: ', ', '.join(str(x) for x in islice(cw(), 20)))
|
||||
x = Fraction(83116, 51639)
|
||||
print(f"\n{x} is the {get_term_num(x):_}'th term.")
|
||||
Loading…
Add table
Add a link
Reference in a new issue