Leser: 5
![]() |
|< 1 2 >| | ![]() |
19 Einträge, 2 Seiten |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Carp; sub gen_triangles { my $max = shift || 10; my @set; for my $c (1..$max) { for my $b (1..$c) { for my $a (1..$b) { push @set, [$a, $b, $c] if $c**2 == $b**2 + $a**2; } } } return \@set; } print Dumper gen_triangles;
print [(a,b,c) for c in range(20) for b in range(c) for a in range(b) if c**2==a**2+b**2]
1
2
let triangles = [ (a, b ,c) | c <- [1..], b <- [1..c], a <- [1..b], a^2+b^2==c^2 ]
take 10 triangles
1
2
3
4
5
6
7
8
my @pyt = gather {
for 1..20 -> $c {
for 1..$c -> $b {
my $a = sqrt($c**2 - $b**2);
take [$a, $b, $c] if $a == int($a);
}
}
}
1 2 3
for $c (1..10) { for $b (1..$c) { for $a (1..$b) { print $c,$b,$a if $c**2 == $b**2 + $a**2; } } }
LanX-+2008-10-19 21:48:21--BTW: pythagoreische Tripel lassen sich deutlich effizienter finden als mit diesem Brute Force Ansatz.
1 2 3 4 5 6 7 8 9 10
sub gen_triangles { my $max = shift || 10; my @set; my($x,$y,$z)=(1,1,1); while($z<=$max && $x<=$max){ push @set, [$z, $y, $x] if $x**2 == $y**2 + $z**2; if($y>$z){$z++}else{if($x>$y){$y++;$z=1}else{$x++;$y=$z=1}} } return \@set; }
Ronnie+2008-10-19 13:57:41--Zum Vergleich mal das selbe (als ListComprehension) in Python:
Code: (dl )print [(a,b,c) for c in range(20) for b in range(c) for a in range(b) if c**2==a**2+b**2]
oder in Haskell (ghci):
Code: (dl )1
2let triangles = [ (a, b ,c) | c <- [1..], b <- [1..c], a <- [1..b], a^2+b^2==c^2 ]
take 10 triangles
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#!/usr/bin/perl use Data::Dumper; my $max=10; ### Straight forward sub list (&){ my $code_ref=shift; &$code_ref; return @_; } @x= list { for $c (1..10){ for $b (1..$c){ for $a (1..$b){ push @_, [$c,$b,$a] if $c**2 == $b**2 + $a**2; } } } }; print Dumper \@x; ### ist das pushen zu haesslich? { my @list; sub gather (&){ my $code_ref=shift; @list=(); # KORRIGIERT: danke Matthias W. &$code_ref; return @list; } sub take ($) { my $elem = shift; push @list, $elem; return; } } @x= gather { for $c (1..10){ for $b (1..$c){ for $a (1..$b){ if ( $c**2 == $b**2 + $a**2){ take [$c,$b,$a] } } } } }; print Dumper \@x; ### lazy evaluation ? { my ($a,$b,$c)=(0,0,0); sub lazy { while ( $c++ < $max ) { while ( $b++ < $c ) { while ( $a++, $a %= $b ) { return [$c,$b,$a] if $c**2 == $b**2 + $a**2; }; # $a=0; # unnötig }; $b=0; } $c=0; return; } } while ( $triple = lazy() ) { print Dumper $triple; }
MatthiasW+2008-10-29 17:09:19--Bei dem gather fehlt aber noch das Resetten von @list, sonst bekommt man ab dem 2. gather auch noch die Ergebnisse der vorherigen.
MatthiasW+2008-10-29 17:09:19--Ansonsten gibts für List-Comprehensions auch folgendes Modul:List::Comprehensions
![]() |
|< 1 2 >| | ![]() |
19 Einträge, 2 Seiten |