Exercise: Finding Primes

What computer language primer would be complete without this little gem? In this exercise, you will examine a small program to find and print prime numbers. Prime numbers are divisible only by 1 and themselves; for example, 2 is prime, 3 is prime, 4 is not (because it is divisible by 1, 4, and 2), and so on. The list of primes is infinite, and they take a lot of computer power to find.

Using your text editor, type the program from Listing 3.5 and save it as Primes. Do not type in the line numbers. Make the program executable according to the instructions you learned in Hour 1.

When you're done, try running the program by typing the following at a command line:

perl Primes
Listing 3.5 The Complete Source for Primes ...

Get Sams Teach Yourself Perl in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.