Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Data Types
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*ibpr[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]
test.assert_equals(get_primes(10), [2, 3, 5, 7]) test.assert_equals(get_primes(50), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) test.assert_equals(get_primes(100), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]) test.assert_equals(len(get_primes(10_000)), 1229) test.assert_equals(len(get_primes(1_000_000)), 78498) test.assert_equals(len(get_primes(10_000_000)), 664579) test.assert_equals(len(get_primes(100_000_000)), 5761455)
- test.assert_equals(get_primes(10), [2, 3, 5, 7])
- test.assert_equals(get_primes(50), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47])
- test.assert_equals(get_primes(100), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])
- test.assert_equals(len(get_primes(10_000)), 1229)
- test.assert_equals(len(get_primes(1_000_000)), 78498)
- test.assert_equals(len(get_primes(10_000_000)), 664579)
test.assert_equals(len(get_primes(49_979_688)), 3000000)- test.assert_equals(len(get_primes(100_000_000)), 5761455)
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)
test.assert_equals(divisors (5*4*3), [1,2,3,4,5,6,10,12,15,20,30,60]) test.assert_equals(divisors (4), [1,2,4]) test.assert_equals(divisors (5*3), [1,3,5,15]) test.assert_equals(divisors (101), [1,101]) _1m = [1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 625, 800, 1000, 1250, 1600, 2000, 2500, 3125, 4000, 5000, 6250, 8000, 10000, 12500, 15625, 20000, 25000, 31250, 40000, 50000, 62500, 100000, 125000, 200000, 250000, 500000, 1000000] test.assert_equals(divisors (10**6), _1m) test.assert_equals(len(divisors(10**13)), 196)
- test.assert_equals(divisors (5*4*3), [1,2,3,4,5,6,10,12,15,20,30,60])
- test.assert_equals(divisors (4), [1,2,4])
- test.assert_equals(divisors (5*3), [1,3,5,15])
- test.assert_equals(divisors (101), [1,101])
- _1m = [1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200, 250, 320, 400, 500, 625, 800, 1000, 1250, 1600, 2000, 2500, 3125, 4000, 5000, 6250, 8000, 10000, 12500, 15625, 20000, 25000, 31250, 40000, 50000, 62500, 100000, 125000, 200000, 250000, 500000, 1000000]
- test.assert_equals(divisors (10**6), _1m)
- test.assert_equals(len(divisors(10**13)), 196)
Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Data Types
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 - 1while newnum > 1:if num % newnum == 0:return Falsenewnum -= 1if newnum > 1:continuereturn 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 -= 1if og > 1:c.append(2)return sorted(c, reverse=False)- return [n for n in range(1, num + 1) if is_prime(n)]
test.assert_equals(get_primes(10), [2, 3, 5, 7]) test.assert_equals(get_primes(50), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) test.assert_equals(get_primes(100), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]) test.assert_equals(len(get_primes(10000)), 1229) test.assert_equals(len(get_primes(1000000)), 78498)
- test.assert_equals(get_primes(10), [2, 3, 5, 7])
- test.assert_equals(get_primes(50), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47])
- test.assert_equals(get_primes(100), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])
test.assert_equals(len(get_primes(10000)), 1229)- test.assert_equals(len(get_primes(10000)), 1229)
- test.assert_equals(len(get_primes(1000000)), 78498)