Einzelnen Beitrag anzeigen
Alt 02.04.10, 13:07   #28 (permalink)
pytohn
 
Benutzerbild von pytohn
 
Registriert seit: 28.03.09
pytohn Leistung: Facit NTK
Likes: 0
Standard Perl verbessert

Habe meine alte Perlversion ein bisschen aufgewertet:

Code:
print "Anfangswert:\t";
$start = <STDIN>;
print "Endwert:\t";
$ende = <STDIN>;

foreach $x ($start..$ende-2) {
        if (ist_primzahl($x) and ist_primzahl($x+2)) {
                print $x, "\t", $x+2, "\n";
        }
}

sub ist_primzahl {
        my($zahl) = @_;
        if ($zahl eq 1) {
                return 0;
        }
        foreach $teiler (2..sqrt ($zahl)) {     # nur bis zur Wurzel von $zahl
                if ( ($zahl % $teiler) eq 0) {  # Modulo-Test
                        return 0;       # Keine Primzahl: => falsch
                }
        }
        return 1;
}
pytohn ist offline   Mit Zitat antworten
 

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