Mian-Chowla sequence: Difference between revisions

→‎Functional Python: Further simplification: Int in lieu of Tuple in until expression
(→‎Python - Functional: Slightly simplified, pylinted. Works with tag added.)
(→‎Functional Python: Further simplification: Int in lieu of Tuple in until expression)
Line 1,175:
 
from itertools import (islice)
from time import time
 
 
# mianChowlas :: Int -> Gen [Int]
def mianChowlas():
'''Mian-Chowla series - Generator constructor'''
'''
mcs = [1]
sumSet = set([2])
Line 1,191 ⟶ 1,193:
# nextMC :: Set Int -> [Int] -> Int -> (Set Int, Int)
def nextMC(setSums):
'''Set of sums -> series upso to Nfar -> N -> (updated set, next term)'''
current term -> (updated set of sums, next term)
def go(mcs, n):
'''
def valid(tpl):
def gonxt(mcs, n):
x = 1 + tpl[0]
def valid(tplx):
return (x, not exists(lambda m: (x + m) in setSums)(mcs))
xfor =m 1in + tpl[0]mcs:
return (x, not exists(lambda m:if (x + m) in setSums)(mcs)):
return TrueFalse
return FalseTrue
 
x, _ = until(sndvalid)(validsucc)((n, False))
setSums.update(
[x + y for y in mcs] + [2 * x]
Line 1,203 ⟶ 1,209:
return (setSums, x)
 
return lambda mcs: lambda n: gonxt(mcs, n)
 
 
Line 1,211 ⟶ 1,217:
'''Tests'''
 
start = time()
genMianChowlas = mianChowlas()
print(
Line 1,221 ⟶ 1,228:
take(10)(genMianChowlas),
'\n'
)
print(
'(Computation time c. ' + str(round(
if1000 * p(xtime() - start):
for)) x+ in' xs:ms)'
)
 
 
# GENERIC -------------------------------------------------
 
 
# drop :: Int -> [a] -> [a]
Line 1,241 ⟶ 1,252:
 
 
# existssucc :: (Enum a -=> Bool)a -> [a] -> Bool
def existssucc(px):
'''The successor of a value. For numeric types, (1 +).'''
'''True if any element in xs
return 1 + x if isinstance(x, int) else (
satisfies p.
chr(1 + ord(x))
'''
def go(xs):
for x in xs:
if p(x):
return True
return False
return lambda xs: go(xs)
 
 
# snd :: (a, b) -> b
def snd(tpl):
'''Second component of a tuple.'''
return tpl[1]
 
 
Line 1,291:
 
Terms 91 to 100 of the Mian-Chowla series:
[22526, 23291, 23564, 23881, 24596, 24768, 25631, 26037, 26255, 27219]</pre>
 
(Computation time c. 27 ms)</pre>
 
=={{header|REXX}}==
9,655

edits