Pythagorean quadruples: Difference between revisions
Content added Content deleted
m (added underlining.) |
|||
Line 1,569: | Line 1,569: | ||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
{{Trans|AppleScript}} |
{{Trans|AppleScript}} |
||
<lang Python> |
<lang Python>'''Pythagorean Quadruples''' |
||
from itertools import islice, takewhile |
|||
⚫ | |||
# unrepresentables :: () -> [Int] |
|||
⚫ | |||
def unrepresentables(): |
|||
⚫ | |||
'''A non-finite stream of powers of two which can |
|||
takeWhileGen(lambda x: 2200 > x)( |
|||
not be represented as a Pythagorean quadruple. |
|||
⚫ | |||
⚫ | |||
return merge( |
|||
) |
powersOfTwo() |
||
)( |
|||
⚫ | |||
) |
) |
||
Line 1,585: | Line 1,588: | ||
# powersOfTwo :: Gen [Int] |
# powersOfTwo :: Gen [Int] |
||
def powersOfTwo(): |
def powersOfTwo(): |
||
'''A non-finite stream of successive powers of two. |
|||
''' |
|||
return iterate(lambda x: 2 * x)(1) |
return iterate(lambda x: 2 * x)(1) |
||
# ------------------------- TEST ------------------------- |
|||
# mergeInOrder :: Gen [Int] -> Gen [Int] -> Gen [Int] |
|||
⚫ | |||
def mergeInOrder(ga): |
|||
def main(): |
|||
'''For positive integers up to 2,200 (inclusive) |
|||
⚫ | |||
''' |
|||
⚫ | |||
while not a['Nothing'] and not b['Nothing']: |
|||
return 2200 >= x |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
takewhile(p, unrepresentables()) |
|||
) |
|||
) |
|||
# |
# ----------------------- GENERIC ------------------------ |
||
# iterate :: (a -> a) -> a -> Gen [a] |
# iterate :: (a -> a) -> a -> Gen [a] |
||
def iterate(f): |
def iterate(f): |
||
'''An infinite list of repeated |
|||
applications of f to x. |
|||
''' |
|||
def go(x): |
def go(x): |
||
v = x |
v = x |
||
while True: |
while True: |
||
yield |
yield v |
||
v = f(v) |
v = f(v) |
||
return |
return go |
||
# |
# merge :: Gen [Int] -> Gen [Int] -> Gen [Int] |
||
def |
def merge(ga): |
||
'''An ordered stream of values drawn from two |
|||
return {'type': 'Maybe', 'Nothing': False, 'Just': x} |
|||
other ordered streams. |
|||
''' |
|||
def go(gb): |
|||
# Nothing :: Maybe a |
|||
def |
def f(ma, mb): |
||
a, b = ma, mb |
|||
return {'type': 'Maybe', 'Nothing': True} |
|||
⚫ | |||
ta, tb = a, b |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Line 1,632: | Line 1,646: | ||
# take :: Int -> String -> String |
# take :: Int -> String -> String |
||
def take(n): |
def take(n): |
||
'''The prefix of xs of length n, |
|||
xs |
or xs itself if n > length xs. |
||
''' |
|||
if isinstance(xs, list) |
|||
else list(islice(xs, n)) |
|||
⚫ | |||
# takeWhileGen :: (a -> Bool) -> Gen [a] -> [a] |
|||
def takeWhileGen(p): |
|||
def go(xs): |
def go(xs): |
||
return ( |
|||
xs[0:n] |
|||
if isinstance(xs, (list, tuple)) |
|||
else list(islice(xs, n)) |
|||
) |
|||
return go |
|||
⚫ | |||
# uncons :: [a] -> Maybe (a, [a]) |
# uncons :: [a] -> Maybe (a, [a]) |
||
def uncons(xs): |
def uncons(xs): |
||
'''The deconstruction of a non-empty list |
|||
(or generator stream) into two parts: |
|||
a head value, and the remaining values. |
|||
''' |
|||
if isinstance(xs, list): |
if isinstance(xs, list): |
||
return |
return (xs[0], xs[1:]) if xs else None |
||
else: |
else: |
||
nxt = take(1)(xs) |
nxt = take(1)(xs) |
||
return |
return (nxt[0], xs) if nxt else None |
||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
|||
main()</lang> |
main()</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>[1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048]</pre> |
<pre>[1, 2, 4, 5, 8, 10, 16, 20, 32, 40, 64, 80, 128, 160, 256, 320, 512, 640, 1024, 1280, 2048]</pre> |