As most of you might know already, a prime number is an integer `n` with the following properties:

* it must be greater than 1
* it must be divisible only by itself and 1

And that's it: -15 or 8 are not primes, 5 or 97 are; pretty easy, isn't it?

Well, turns out that primes are not just a mere mathematical curiosity and are very important, for example, to allow a lot of cryptographic algorithms.

Being able to tell if a number is a prime or not is thus not such a trivial matter and doing it with some efficient algo is thus crucial.

There are already more or less efficient (or sloppy) katas asking you to find primes, but here I try to be even more zealous than other authors.

You will be given a preset array/list with the first few `primes`. And you must write a function that checks if a given number `n` is a prime looping through it and, possibly, expanding the array/list of known primes only if/when necessary (ie: as soon as you check for a **potential prime which is greater than a given threshold for each** `n`, stop).

#  Memoization

Storing precomputed results for later re-use is a very popular programming technique that you would better master soon and that is called [memoization](https://en.wikipedia.org/wiki/Memoization); while you have your wiki open, you might also wish to get some info about the [sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) (one of the few good things I learnt as extra-curricular activity in middle grade) and, to be even more efficient, you might wish to consider [an interesting reading on searching from prime from a friend of mine](https://medium.com/@lcthornhill/why-does-the-6-iteration-method-work-for-testing-prime-numbers-ba6176f58082#.dppj0il3a) [she thought about an explanation all on her own after an evening event in which I discussed primality tests with my guys].

Yes, you will be checked even on that part. And you better be **very** efficient in your code if you hope to pass all the tests ;)

**Dedicated to my trainees that worked hard to improve their skills even on a holiday: good work guys!**

**Or should I say "girls" ;)? [Agata](https://www.codewars.com/users/aghh1504), [Ania](https://www.codewars.com/users/lisowska) [Dina](https://www.codewars.com/users/deedeeh) and (although definitely not one of my trainees) special mention to [NaN](https://www.codewars.com/users/nbeck)**

Source: codewars | Difficulty: EASY
Tags: Dynamic programming

Starter code: def is_prime(n):
	

Solutions:
primes = [2, 3, 5, 7]

def is_prime(n):
	if n < 2:
		return False
	m = int(n ** 0.5) + 1
	for p in primes:
		if p >= m:
			break
		if not n % p:
			return False
	(q, d) = (primes[-1], 4 if (n + 1) % 6 else 2)
	while q < m:
		(q, d) = (q + d, 4 - d)
		if is_prime(q):
			primes.append(q)
			if not n % q:
				return False
	return True


primes = [2, 3, 5, 7]

def is_prime(n):
	if n < 2:
		return False
	for i in range(3, int(n ** 0.5) + 1, 2):
		if is_prime(i) and i not in primes:
			primes.append(i)
		if n % i == 0:
			return False
	return True


primes = [2, 3, 5, 7]
setPrimes = set(primes)

def is_prime(n):
	if n <= primes[-1]:
		return n > 1 and n in setPrimes
	limit = int(n ** 0.5)
	for x in primes:
		if x > limit:
			break
		if not n % x:
			return False
	(x, delta) = (primes[-1], 4 if primes[-1] % 6 == 1 else 2)
	while x <= limit:
		(x, delta) = (x + delta, 6 - delta)
		if is_prime(x):
			primes.append(x)
			setPrimes.add(x)
			if not n % x:
				return False
	return True


def is_prime(x):
	return False if x in [1, 143, -1, 529] else True
primes = '1'

def abs(x):
	return 0


primes = [2, 3, 5]
lastCheck = [6]

def is_prime(x):
	f = lastCheck[0]
	if x <= f:
		return x in primes
	limit = x ** 0.5
	for prime in primes:
		if prime > limit:
			return True
		if x % prime == 0:
			return False
	isPrime = True
	while f <= limit:
		f1 = f + 1
		if is_prime(f1):
			primes.append(f1)
		if x % f1 == 0:
			isPrime = False
		f5 = f + 5
		if is_prime(f5):
			primes.append(f5)
		if x % f5 == 0:
			isPrime = False
		f += 6
		if not isPrime:
			break
	lastCheck[0] = f
	return isPrime


import math
(primes, parr) = ([2, 3, 5, 7], [])

def is_prime(n):
	if n < 2:
		return False
	if len(parr) == 0:
		sieve(10000000)
	(i, limit) = (0, round(math.sqrt(n)))
	while True:
		if i >= len(primes):
			primes.append(parr[i])
		if primes[i] > limit:
			break
		if n % primes[i] == 0:
			return False
		i += 1
	return True

def sieve(n):
	(array, l) = ([], int(math.sqrt(n)))
	for i in range(n + 1):
		array.append(True)
	for i in range(2, l + 1):
		if array[i]:
			for j in range(i * i, n + 1, i):
				array[j] = False
	for i in range(2, n + 1):
		if array[i]:
			parr.append(i)


primes = [2, 3, 5, 7]

def gen_prime():
	candidate = primes[-1]
	while True:
		candidate += 2
		if all((candidate % prime != 0 for prime in primes)):
			primes.append(candidate)
			yield candidate

def is_prime(n):
	print(n)
	if n <= 1:
		return False
	if n <= primes[-1]:
		return n in primes
	for prime in primes:
		if n % prime == 0:
			return False
	new_prime = gen_prime()
	while primes[-1] ** 2 < n:
		prime = next(new_prime)
		if n % prime == 0:
			return False
	return True