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++; } }
In reply to Spaces between outputs. by CPTQuesoXI
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |