### Get all primes up to a given number

Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Code
Diff
• ``````from math import ceil

#same sieve of erasthosthenes...
#starting with odd values, using lesser memory

def get_primes(n):
length = (n+1) // 2
p = [1] * length
p[0] = 0
sqrti = ceil(((n+1)**.5 - 1) / 2)
for i in range(1, sqrti):
if p[i]:
x = 2*i+1
start = (x**2-1)//2
p[start::x] = [0] * ceil((length - start) / x)
return [2] + [i*2+1 for i, v in enumerate(p) if v]
``````
• def get_primes(n):
• bpr = [0,1] * ((n+1)//2) + [0] * (1 - (1 * 1&n))
• bpr[:3] = [0, 0, 1]
• for i in range(3, 1+ int(n**0.5), 2):
• if bpr[i]:
• ipi, isq = i*2, i*i
• bpr[isq::ipi] = [0] * (( n - isq)//ipi + 1)
• return [2] + [i for i in range(3,n,2) if bpr[i]]
• from math import ceil
• #same sieve of erasthosthenes...
• #starting with odd values, using lesser memory
• def get_primes(n):
• length = (n+1) // 2
• p = [1] * length
• p[0] = 0
• sqrti = ceil(((n+1)**.5 - 1) / 2)
• for i in range(1, sqrti):
• if p[i]:
• x = 2*i+1
• start = (x**2-1)//2
• p[start::x] = [0] * ceil((length - start) / x)
• return [2] + [i*2+1 for i, v in enumerate(p) if v]

### List of divisors of number

Code
Diff
• ``````from math import sqrt

def divisors(num):
res = []
for n in range(1, int(sqrt(num)) + 1):
if num % n == 0:
res.append(n)
if n ** 2 != num:
res.append(int(num / n))
return sorted(res)``````
• def divisors(number):
• dividers = [number]
• num = int(number * 0.5)
• for i in range(1,num+1):
• if(number % i == 0):
• dividers.append(i)
• dividers.sort()
• return dividers
• from math import sqrt
• def divisors(num):
• res = []
• for n in range(1, int(sqrt(num)) + 1):
• if num % n == 0:
• res.append(n)
• if n ** 2 != num:
• res.append(int(num / n))
• return sorted(res)

### Get all primes up to a given number

Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Code
Diff
• ``````from math import sqrt

def is_prime(num):
for newnum in range(2, int(sqrt(num)) + 1):
if num % newnum == 0:
return False
return False if num == 1 else True

def get_primes(num):
return [n for n in range(1, num + 1) if is_prime(n)]
``````
• from math import sqrt
• def is_prime(num):
• newnum = num - 1
• while newnum > 1:
• if num % newnum == 0:
• return False
• newnum -= 1
• if newnum > 1:
• continue
• return True
• for newnum in range(2, int(sqrt(num)) + 1):
• if num % newnum == 0:
• return False
• return False if num == 1 else True
• def get_primes(num):
• og, c = num, []
• while num > 0:
• if is_prime(num):
• c.append(num)
• num -= 1
• if og > 1:
• c.append(2)
• return sorted(c, reverse=False)
• return [n for n in range(1, num + 1) if is_prime(n)]