COSma Coding

COSma Coding

COSma Learning

Prime Numbers

A **prime number** is a whole number that *cannot* be factored into two other whole numbers that are less than the number. In other words, a whole number is prime if the only other whole numbers that divide it are 1 and itself.

As an example, 3 is a prime number - there are no numbers \(a\) and \(b\) such that \( a \times b = 3 \), other than 1 and 3. On the other hand, 4 is *not* a prime number because \( 2 \times 2 = 4 \). Whole numbers that are *not prime* are called **composite numbers**. On your own, try to think of 5 prime numbers and 5 composite numbers.

We have defined a `isPrime()`

function that will test whether or not a whole number is prime. It will return `true`

if the number is prime, and `false`

otherwise. So for example, `isPrime(3)`

will return true but `isPrime(4)`

will return false.

```
print( isPrime(3) );
print( isPrime(4) );
```

This `isPrime()`

function allows us to do some fun investigating with prime numbers! Instead of doing a bunch of computations, we can let the computer do that - while we make sense of the results! Let's check out an example.

Suppose we want to add together all of the prime numbers between 2 and 100. That would be a lot of work for us to do on paper, but the computer can do it in a fraction of a second! The code below shows this being done.

```
var sum = 0;
// Iterate from i=2 to i=100
for (var i = 2; i <= 100; i++){
// Check to see if i is prime...
if (isPrime(i)) {
// If so, add it to the sum.
sum = sum + i;
}
}
print(`Sum: ${sum}`);
```

In Line (1) we create a variable `sum`

and set its value to 0 - we will use this to keep track of the sum of the prime numbers. Then on Line (4) we start a for-loop and loop from `i = 2`

to `i = 100`

, in increments of 1. For each value of `i`

between 2 and 100, we will check to see if it is prime [Line (6)] and if so, we will add this value to our `sum`

variable [Line (8)]. In Line (12) we print the sum. Here's the neat thing: on Line (4), change to 100 to a much bigger number, like 10000. This will tell the computer to compute the sum of all prime numbers between 2 and 10000 - and it can do this *very* quickly!

Activities

Activity: Counting primes

Use the editor below to determine the number of prime numbers less than 1,000. You might want to use a loop similar to the one presented in the previous code snippet.

`print("Your code here.");`

Activity: Sorting based on primality

An array `a`

of random whole numbers between 2 and 100 has been defined behind the scenes. Sort the elements of `a`

into two arrays based on whether or not they are prime. We've started you off with a little code.

```
var primes = [], composites = [];
for (var i = 0; i < a.length; i++) {
// YOUR CODE HERE
}
print(`Primes: ${primes}`);
print(`Composites: ${composites}`);
```

Challenge Activity: Sum of the first 100 primes

Use the editor below to determine the sum of *the first 100 primes*. Hint: this is different than computing the sum of all prime numbers less than or equal to 100.

`print("Your code here.");`

If done correctly, you should get a value of 24133. After doing that, find the sum of the first 1000 prime numbers.

Challenge Activity: Defining the isPrime function

We have been using an `isPrime()`

function, which is defined behind the scenes. However, we actually wrote the code for that function - and you can too! In the editor below, create a function `isPrime2()`

that determines whether or not a number is prime. The function should return `true`

if the number is prime, and `false`

otherwise.

```
function isPrime2(n) {
// YOUR CODE HERE
return false;
}
print( isPrime2(3) );
print( isPrime2(4) );
```