Mian-Chowla sequence: Difference between revisions

m
→‎Functional Python: (uncurried nextMC)
(→‎Python: Simplified compositional subheader to functional)
m (→‎Functional Python: (uncurried nextMC))
Line 1,172:
===Functional===
{{Works with|Python|3.7}}
<lang python>"""'''Mian-Chowla series"""'''
 
from itertools import (islice)
Line 1,187:
while True:
yield x
(sumSet, x) = nextMC(sumSet)(, mcs)(, x)
mcs.append(x)
 
 
# nextMC :: Set Int -> [Int] -> Int -> (Set Int, Int)
def nextMC(setSums, mcs, n):
'''Set of sums -> series so far ->
current term -> (updated set of sums, next term)
'''
def nxtvalid(mcs, nx):
deffor valid(x)m in mcs:
forif x + m in mcssetSums:
ifreturn x + m in setSums:False
return FalseTrue
return True
 
x = until(valid)(succ)(n)
setSums.update(
[x + y for y in mcs] + [2 * x]
)
return (setSums, x)
 
return lambda mcs: lambda n: nxt(mcs, n)
 
 
9,655

edits