Exercise: Primes Below 10

Exercise 10 Very easy

Prerequisites for the exercise

  1. Python Random Numbers
  2. All previous chapters

Objective

Determine whether a randomly-generated positive integer below 10 is a prime number or not.

Description

A prime number is an integer greater than 1 that's only divisble by two numbers — itself and the number 1.

The first 10 primes are listed as follows: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.

A composite number is the exact opposite of a prime. It's an integer that's divisble by more than two integers. For instance, 4 is composite. It's divisible by three integers: 1, 2, and 4.

In this exercise, you have to generate a random positive integer below 10 and then check whether it's a prime number.

If it is prime, print:

<integer> is prime.

or otherwise print:

<integer> is NOT prime.

where <integer> is the randomly-generated integer.

Shown below are a couple of examples:

4 is NOT prime.
1 is NOT prime.
3 is prime.
View Solution

New file

Inside the directory you created for this course on Python, create a new folder called Exercise-10-Primes-Below-10 and put the .py solution files for this exercise within it.

Solution

The main task in this exercise is to determine how to check whether a given integer is prime. Let's get to it...

First let's list down all positive integers below 10.

1, 2, 3, 4, 5, 6, 7, 8, 9

In this list, only 2, 3, 5, and 7 are prime. The rest are all composite, except for 1 which is neither prime, nor composite.

Based on this, the most straighforward way to check whether an integer below 10 is prime or not is to check whether it is 2, 3, 5, or 7. If it is, we know for sure that we are dealing with a prime.

In our case, we'll go with this very logic to check whether our randomly-generated integer is prime.

Here's the code:

import random

n = random.randint(1, 9)

if n == 2 or n == 3 or n == 5 or n == 7:
    print(n, 'is prime.')
else:
    print(n, 'is NOT prime.')

In the if's header, we use the or keyword to check for the truth of either of the conditions n == 2, n == 3, n == 5, or n == 7. If any one is true, the following if block is executed.

Otherwise, the else block comes into action.

This completes our exercise.

In this exercise, we had a very small domain to apply the prime test to (only 9 integers) and likewise used a direct way to solve it. However, when you study number theory or algorithms in general, you'll see that programs utilising the concept of primes are endless, and there we couldn't just enumerate all primes as we did here.

Well, according to a theorem in maths, there are infinitely many primes, so no program could ever enumerate all of them anyway!

The most important algorithm that has interested mathematicians and computer scientists for quite a long time, and still does, is how to determine whether a given integer is prime. There are multiple efficiency steps devised over the years that can make primality tests fast enough for manageable integers.

Sometimes, humongous numbers can be passed through the primality test very efficiently if they are of the form 2p - 1, as there is a relatively quicker test for such integers known as the Lucas-Lehmer Test.

The world of primes and number theory is an extremely interesting place to discover. And if you know programming, it becomes even more interesting.

Who says computer science is not math?