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 | |
|
Re: Spaces between outputs.
by Tux (Canon) on Mar 01, 2018 at 14:50 UTC | |
|
Re: Spaces between outputs.
by Laurent_R (Canon) on Mar 01, 2018 at 17:24 UTC | |
|
Re: Spaces between outputs.
by johngg (Canon) on Mar 01, 2018 at 14:50 UTC |