Last active
May 24, 2019 14:27
-
-
Save nrdmn/ff4c942852eb023e4551c6cc5b363f5f to your computer and use it in GitHub Desktop.
pythagorean triples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
triangles = [(a,b,c) | a<-[1..], b<-[1..a-1], c<-[a..a+b], a^2+b^2==c^2] | |
main = putStr $ unlines $ map show triangles |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* range(a, b) { | |
while (a <= b) { | |
yield a; | |
a++; | |
} | |
} | |
function* triangles() { | |
for (a of function* () { | |
var c = 0; | |
while (true) { | |
c++; | |
yield c; | |
} | |
}()) { | |
for (b of range(1, a-1)) { | |
for (c of range(a, a+b)) { | |
if (a**2 + b**2 == c**2) { | |
yield [a, b, c]; | |
} | |
} | |
} | |
} | |
} | |
for (t of triangles()) { | |
console.log(t); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function triangles() { | |
foreach ((function () { | |
$c = 0; | |
while (true) { | |
$c++; | |
yield $c; | |
} | |
})() as $a) { | |
foreach (range(1, $a) as $b) { | |
foreach (range($a, $a+$b) as $c) { | |
if ($a**2 + $b**2 == $c**2) { | |
yield [$a, $b, $c]; | |
} | |
} | |
} | |
} | |
} | |
foreach (triangles() as $t) { | |
echo "(".$t[0].",".$t[1].",".$t[2].")\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
triangles = ((a,b,c) for a in (i for i, _ in enumerate(iter(int, 1))) for b in range(1,a) for c in range(a,a+b+1) if a**2+b**2==c**2) | |
for x in triangles: | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment