I'm going to make a decimal list with Python

Asked 2 years ago, Updated 2 years ago, 71 views

import math
import pickle


def prime_list(n):
    sieve = [True] * (n+1) # true: prime, false: not prime. default: true

    for i in range(2, int(math.sqrt(n))+1):
        if sieve[i] == True:
            for j in range(i+i, n+1, i):
                sieve[j] = False
    return [i for i in range(2,n+1) if sieve[i] == True]



list1 = prime_list(1000000000)

with open("prime_10^9.pickle","wb") as fw:
    pickle.dump(list1, fw)

When I make a decimal list with Eratosthenes, I make a decimal list with less than 108 but I get a memory error from 109. What should I do? Should I use a different data type instead of a list? For your information, modules such as numpy cannot be used.

89

python primes

2022-09-20 19:32

1 Answers

I'm not sure what type Python bullion is made of, but if it's int type, not real 1 bit, then True N will have the same memory capacity as 32-bit int N. So, we initialize each array of elements to 0xFFFFFF, and we set 32 true bits per element, so we can save 32 times the memory through bit operations. If you want to do this, you have to calculate the beat well.

If you want to try Erastothenes' methods and get more than a few...

primes = get_primes_erasto(N)

candidate = smallest_odd_number_greater_than(N)
while candidate < VERYVERYBIGN:
  for prime in primes:
    if candidate % prime:
      break
    if candidate < prime*prime:
      primes.append(candidate)
      break

  candidate += 2

There's probably a way to do it like this way. I don't know if it's efficient.


2022-09-20 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.