in reply to Perl vs. Python for prime numbers
The first approach is a variation of chorobas code, but a bit more intuitive from my perspective.
The second is a generic approach using goto. This technique can always emulate this (very) Python idiom.
#!/usr/bin/perl use warnings; use strict; use feature qw(say); N: for my $n (2 .. 99) { for my $x (2 .. $n - 1) { next N if 0 == $n % $x; } say $n, ' is a prime number'; } for my $n (2 .. 99) { for my $x (2 .. $n - 1) { goto NOT_PRIME if 0 == $n % $x; } say $n, ' is a prime number'; NOT_PRIME: }
I think both are pretty good readable.
HTH! =)
Cheers Rolf
( addicted to the Perl Programming Language)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl vs. Python for prime numbers
by hdb (Monsignor) on Jun 14, 2013 at 07:24 UTC | |
by choroba (Cardinal) on Jun 14, 2013 at 07:31 UTC | |
by LanX (Saint) on Jun 14, 2013 at 07:44 UTC | |
by eyepopslikeamosquito (Archbishop) on Jun 15, 2013 at 09:23 UTC | |
by LanX (Saint) on Jun 15, 2013 at 09:56 UTC | |
Re^2: Perl vs. Python (for/else idiom analyzed)
by LanX (Saint) on Jun 15, 2013 at 11:39 UTC |