Skip to content

Instantly share code, notes, and snippets.

@nrdmn
Last active May 24, 2019 14:27
Show Gist options
  • Save nrdmn/ff4c942852eb023e4551c6cc5b363f5f to your computer and use it in GitHub Desktop.
Save nrdmn/ff4c942852eb023e4551c6cc5b363f5f to your computer and use it in GitHub Desktop.
pythagorean triples
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
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);
}
<?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";
}
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