Tests for prime numbers
I'm told to list the prime numbers from 7 to 150 .
I know how to do it the slow way by one by one checking the numbers till 150. But in an exam condition I can't possibility do that .
Is there any way to test for prime numbers ? Or to prove whether that a number is a prime number ?
That seems like a silly test question, but you can use the Sieve of Eratosthenes. Start with all numbers from 7 to 150 and start removing multiples of integers greater than 1. Animation from the Wikipedia link:
As for proving a number is prime, it is actually much easier to show that a number is not prime. Maybe you have some more idea of what you might see on a test, but those seem like irritating questions.
The illustrated answer above is cool.
Here is something that might be quicker for you during an exam.
The prime numbers between $7$ and $150$ are all the neighbors of multiples of $6$, except:
- Those that end with $5$
- $49$
- $77$
- $91$
- $119$
- $121$
- $133$
- $143$
So you can simply:
- List all multiples of $6$
- List the two neighbors next to each one of them
- Memorize the ones mentioned above and eliminate them
UPDATE:
Just in order to clarify (and simplify) the method mentioned above.
All you have to do is to write down two lists, one starting from $7$ and the other starting from $11$.
Increment each list by $6$ until $150$, and eliminate the values that you have memorized in advance.
$\require{cancel}$
- $\small7,13,19,\color\red{\cancel{25}},31,37,43,\color\green{\cancel{49}},\color\red{\cancel{55}},61,67,73,79,\color\red{\cancel{85}},\color\green{\cancel{91}},97,103,109,\color\red{\cancel{115}},\color\green{\cancel{121}},127,\color\green{\cancel{133}},139,\color\red{\cancel{145}}$
- $\small11,17,23,29,\color\red{\cancel{35}},41,47,53,59,\color\red{\cancel{65}},71,\color\green{\cancel{77}},83,89,\color\red{\cancel{95}},101,107,113,\color\green{\cancel{119}},\color\red{\cancel{125}},131,137,\color\green{\cancel{143}},149$
Given a number $n$.
You need to (1) find all primes smaller than $\sqrt{n}$ and (2) see if any of them divides $n$.
If none of them divides $n$ then you have a prime, otherwise it's a composite number.
Example:
Is 147 prime? $\sqrt{147} < 13$ so we have 2, 3, 5, 7, and 11 to check.
3 divides 147 so we don't need to look further.
147 is a composite number.
My favorite mnemonic:
91 is the only number less than 100 that looks like a prime and isn't.
That's because these numbers don't look like primes: evens, multiples of 5, multiples of 3 (sum of digits tells you), 49, 77.
(This builds on @origimbo 's answer.)
Other answers cover things in far greater detail, but to expand on a comment by barak manos on their own answer, in terms of quick ways of testing and in order of speed of checking you can throw away:
- all even numbers
- all numbers whose digits end in five.
- all numbers whose digits add up to a multiple of three.
A couple of other small primes have more complicated tests, see for example here which covers 7 and 11.