We need a function ```prime_bef_aft()``` that gives the largest prime below a certain given value ```n```, 

```befPrime or bef_prime``` (depending on the language), 

and the smallest prime larger than this value, 

```aftPrime/aft_prime``` (depending on the language).

The result should be output in a list like the following:

```python
prime_bef_aft(n) == [befPrime, aftPrime]
```

If n is a prime number it will give two primes, n will not be included in the result.

Let's see some cases:
```python
prime_bef_aft(100) == [97, 101]

prime_bef_aft(97) == [89, 101]

prime_bef_aft(101) == [97, 103]
```
Range for the random tests: 
```1000 <= n <= 200000```

(The extreme and special case n = 2 will not be considered for the tests. Thanks Blind4Basics)

Happy coding!!

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Mathematics

Starter code: def prime_bef_aft(num):
	

Solutions:
def prime(a):
	if a < 2:
		return False
	if a == 2 or a == 3:
		return True
	if a % 2 == 0 or a % 3 == 0:
		return False
	maxDivisor = a ** 0.5
	(d, i) = (5, 2)
	while d <= maxDivisor:
		if a % d == 0:
			return False
		d += i
		i = 6 - i
	return True

def prime_bef_aft(num):
	res = []
	for n in range(num - 1, 1, -1):
		if prime(n):
			res.append(n)
			break
	for n in range(num + 1, 3 * num, 1):
		if prime(n):
			res.append(n)
			break
	return res


def prime_bef_aft(num):
	m = 2 - (num % 2 == 0)
	return [next((n for n in range(i, int(i * r), s) if is_prime(n))) for (i, r, s) in ((max(2, num - m), 0.8, -2), (num + m, 1.2, 2))]

def is_prime(n):
	factors = 0
	for k in (2, 3):
		while n % k == 0 and factors < 2:
			n //= k
			factors += 1
	k = 5
	step = 2
	while k * k <= n and factors < 2:
		if n % k:
			k += step
			step = 6 - step
		else:
			n //= k
			factors += 1
	if n > 1:
		factors += 1
	return factors == 1


def prime_bef_aft(num):
	m = 2 - (num % 2 == 0)
	(b, a, p_b, p_a) = (2 if num == 3 else num - m, num + m, False, False)
	while not p_b or not p_a:
		(p_b, b) = (True, b) if p_b or is_prime(b) else (False, b - 2)
		(p_a, a) = (True, a) if p_a or is_prime(a) else (False, a + 2)
	return [b, a]

def is_prime(n):
	factors = 0
	for k in (2, 3):
		while n % k == 0 and factors < 2:
			n //= k
			factors += 1
	k = 5
	step = 2
	while k * k <= n and factors < 2:
		if n % k:
			k += step
			step = 6 - step
		else:
			n //= k
			factors += 1
	if n > 1:
		factors += 1
	return factors == 1


from itertools import compress
import numpy as np
s = np.ones(200001)
s[:2] = s[4::2] = 0
for i in range(3, int(len(s) ** 0.5) + 1, 2):
	if s[i]:
		s[i * i::i] = 0
primes = list(compress(range(len(s)), s))

def prime_bef_aft(num):
	i = np.searchsorted(primes, num)
	return [primes[i - 1], primes[i + (primes[i] == num)]]


from itertools import count

def isPrime(n):
	return n == 2 or all((n % x for x in range(3, int(n ** 0.5 + 1), 2)))

def prime_bef_aft(n):
	delta = 2 - (n % 2 == 0)
	return [next((v + (v == 1) for v in count(n - delta, -2) if isPrime(v) or v == 1)), next((v for v in count(n + delta, 2) if isPrime(v)))]


from math import sqrt
from itertools import count, islice

def isPrime(n):
	return n > 1 and all((n % i for i in islice(count(2), int(sqrt(n) - 1))))

def prime_bef_aft(num):
	(c, d, l) = (num - 1, num + 1, [])
	while len(l) < 1:
		if isPrime(c):
			l.append(c)
		c -= 1
	while len(l) < 2:
		if isPrime(d):
			l.append(d)
		d += 1
	return l


def prime_bef_aft(num):
	after = num + 1
	while not isprime(after):
		after += 1
	before = num - 1
	while not isprime(before):
		before -= 1
	return [before, after]

def isprime(n):
	if n % 2 == 0 and n > 2:
		return False
	return all((n % i for i in range(3, int(n ** 0.5) + 1, 2)))


def prime_bef_aft(num):
	res = []
	for i in range(num, 0, -1):
		if len([x for x in range(1, i + 1) if i % x == 0 and i != num]) == 2:
			res.append(i)
			break
	i = num
	while len(res) != 2:
		i += 1
		if len([x for x in range(1, i + 1) if i % x == 0 and i != num]) == 2:
			res.append(i)
			break
	return res