CPTQuesoXI has asked for the wisdom of the Perl Monks concerning the following question:

I've been running through examples in the PERL textbook. This example asks me to find all the prime numbers up to the number I enter via STDIN. The code looks like this. it will print "1 2 35791113151719" if my stdIN is 21. How do I make sure that there is a space between each number? The book doesn't address this.

#! /usr/bin/perl #prime_number.pl use warnings; use strict; print "To what number would you like to calculate the primes?", "\n"; my $n = <>; my $i = 3; print "1 2 "; #keep executing search until the num ($i) reaches given value ($n) OUTER: while ($i <= $n) { #for each iteration, begin division by 2 my $num = 2; #check to see if value is prime for (1 .. $i){ if (($i % $num == 0) and ($i != $num)) { #if not, check next num $i++; next OUTER; } if ($num > sqrt($i)) { print "$i"; $i++; next OUTER; } $num++; } }

Replies are listed 'Best First'.
Re: Spaces between outputs.
by roboticus (Chancellor) on Mar 01, 2018 at 14:49 UTC

    CPTQuesoXI:

    Just change your print statement to add a space:

    print "$i ";

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Spaces between outputs.
by Tux (Canon) on Mar 01, 2018 at 14:50 UTC

    Normally I would comment that print "$i" should drop the useless quotation, but in your case, you only need to insert a space there: print " $i".


    Enjoy, Have FUN! H.Merijn
Re: Spaces between outputs.
by Laurent_R (Canon) on Mar 01, 2018 at 17:24 UTC
    Hi CPTQuesoXI,

    you've been given perfectly good answers to your question and I won't comment further on that.

    Your code works fine, but I would suggest an alternate (more efficient) algorithm for you to consider:

    #! /usr/bin/perl use warnings; use strict; my $max = shift; my @primes = (2); my $i = 3; while ($i <= $max) { my $sqrt = sqrt $i; for my $num (@primes) { last if ($i % $num == 0); push @primes, $i and last if $num > $sqrt; } $i += 2; } print "1, @primes";
    The two important differences are that this program only considers odd values for $i (since an even number won't be prime, except 2, no need to check even numbers), thereby reducing by half the number of values to test, and, more importantly, that it stores the primes found thus far in an array, so that, for each $i, it considers only primes found thus far (and smaller than the square root of $i) as possible divisors.

    This is much more efficient for large input values.

    When running your program for primes smaller than 1,000,000 on my computer, I get the following timings:

    real 0m37.682s user 0m37.296s sys 0m0.015s
    Running the program I suggested above for primes smaller than 1,000,000 on the same computer gives the following timings:
    real 0m4.035s user 0m3.531s sys 0m0.062s
    So this program is almost ten times faster in this case (primes below one million). This program is about 4.5 times faster when looking for for primes smaller than 100,000. And the difference between the two implementations would be even more significant when looking for primes below a limit larger than one million.
Re: Spaces between outputs.
by johngg (Canon) on Mar 01, 2018 at 14:50 UTC

    Try print "$i ";.

    Cheers,

    JohnGG