Think about Loose Coupling | |
PerlMonks |
Re: Spaces between outputs.by Laurent_R (Canon) |
on Mar 01, 2018 at 17:24 UTC ( [id://1210170]=note: print w/replies, xml ) | Need Help?? |
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: 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: Running the program I suggested above for primes smaller than 1,000,000 on the same computer gives the following timings: 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.
In Section
Seekers of Perl Wisdom
|
|