I fixed that and modified flt() to return undef if the number is not prime and made the print statement conditional on the return value.
I changed the condition on the print statement about Carmichael numbers to $target==560 since $i is never going to reach 560. If you Google "strong probable primes" you'll find out about a modification of the FLT test where every composite number fails the test for at least half the exponents. There's no equivalent to Carmichael numbers for this modified test.I added a "\n" to each of your print statements. If you don't add newlines, your print statements will be jammed together on one line and you won't see any output until Perl's output buffer fills up or the program ends.
Your method of computing powers modulo a number is extremely inefficient. You should look into better algorithms for doing that. For extra credit, implement one of the new deterministic polynomial-time algorithms for primality testing. :-)Now it outputs:#! /usr/bin/perl use Math::BigInt ':constant'; my $i; my $target; my $flta; my $fltb; my $base; my $exp; my $i = -1; for $target ( 1 ... 560 ) { print "$target is prime number $i\n" if flt($target); if ( $target == 560 ) { print "From $target and greater are the appearance of Carmichael numbers,oth +er primality tests are required.\n"; } } sub flt { $target = shift @_; $exp = $target - 1; PRIMALITY: for $base ( 1 .. $exp ) { $flta = $base**$exp; $fltb = $flta % $target; if ( $fltb == 1 ) { next PRIMALITY; } else { return; } } $i = $i + 1; return $target; }
1 is prime number 0 2 is prime number 1 3 is prime number 2 5 is prime number 3 7 is prime number 4 11 is prime number 5 ...
In reply to Re: Prime number generation, and range operator
by jdalbec
in thread Prime number generation, and range operator
by dReKurCe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |