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
current term -> (updated set of sums, next term)
def go(mcs, n):▼
'''
def valid(tpl):▼
x = 1 + tpl[0]▼
return (x, not exists(lambda m: (x + m) in setSums)(mcs))▼
x
setSums.update(
[x + y for y in mcs] + [2 * x]
Line 1,203 ⟶ 1,209:
return (setSums, x)
return lambda mcs: lambda 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(
)
# GENERIC -------------------------------------------------
# drop :: Int -> [a] -> [a]
Line 1,241 ⟶ 1,252:
#
def
'''The successor of a value. For numeric types, (1 +).'''
return 1 + x if isinstance(x, int) else (
chr(1 + ord(x))
▲ '''
▲ for x in xs:
▲ if p(x):
▲ return True
▲ return False
Line 1,291:
Terms 91 to 100 of the Mian-Chowla series:
[22526, 23291, 23564, 23881, 24596, 24768, 25631, 26037, 26255, 27219]
(Computation time c. 27 ms)</pre>
=={{header|REXX}}==
|