I took the liberty of running your code through perltidy which pointed out that you're missing two right braces (}).

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. :-)
#! /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; }
Now it outputs:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.