Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created December 16, 2021 04:48
Show Gist options
  • Select an option

  • Save igorvanloo/43ab638f5478c4d323861b3f025a497f to your computer and use it in GitHub Desktop.

Select an option

Save igorvanloo/43ab638f5478c4d323861b3f025a497f to your computer and use it in GitHub Desktop.
Prime Counting Function
def primepi(limit):
def Prime_sieve(n): #Sieve of Eratosthenes
result = [True] * (n + 1)
result[0] = result[1] = False
for i in range(int(math.sqrt(n)) + 1):
if result[i]:
for j in range(2 * i, len(result), i):
result[j] = False
return result
prime_gen = Prime_sieve(limit + 50)
primes = [x for x in range(len(prime_gen)) if prime_gen[x]]
array = [0]*(limit+1)
p_index = 0
for x in range(1, limit + 1):
while True:
if primes[p_index] > x:
array[x] = p_index
break
p_index += 1
return array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment