Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:38:42+00:00 2026-05-22T14:38:42+00:00

I have the following code def gen_primes(): D = {} q = 2 while

  • 0

I have the following code

    def gen_primes():

        D = {}  

        q = 2  

        while True:
            if q not in D:         
                yield q        
                D[q * q] = [q]
            else:           
                for p in D[q]:
                    D.setdefault(p + q, []).append(p)
                del D[q]

            q += 1


    f = open("primes1.txt","w")

    filen = 1
    ran1 = 1
    ran2 = 10000000

    k = 1
    for i in gen_primes():

        if (k >= ran1) and (k <= ran2):

            f.write(str(i) + "\n")
            if k%1000000 == 0:
                print k
            k = k + 1
        else:
            ran1 = ran2 + 1
            ran2 = ran2 + 10000000
            f.close()
            filen = filen + 1;
            f = open("primes" + str(filen) + ".txt","w")

        if k > 100000000:           
            break
    f.close()

The prime generation algorithm is taken from Simple Prime Generator in Python

This program is giving memory error

Traceback (most recent call last):
  File "C:\Python25\Projects\test.py", line 43, in <module>
    for i in gen_primes():
  File "C:\Python25\Projects\test.py", line 30, in gen_primes
    D.setdefault(p + q, []).append(p)
MemoryError

I am trying to store consecutive 10,000,000 primes in one file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:38:43+00:00Added an answer on May 22, 2026 at 2:38 pm

    This prime generator doesn’t use very much memory. It’s also not very fast.

    def gcd(a, b):
        rem = a % b
        while rem != 0:
            a = b
            b = rem
            rem = a % b
        return b
    
    def primegen():
        yield 2
        yield 3
        yield 5
        yield 7
        yield 11
        accum = 2*3*5*7
        out = file('tmp_primes.txt', 'w')
        inp = file('tmp_primes.txt', 'r+')
        out.write('0x2\n0x3\n0x5\n0x7\n0xb\n')
        inp.read(20)
        inpos = inp.tell()
        next_accum = 11
        next_square = 121
        testprime = 13
        while True:
            if gcd(accum, testprime) == 1:
                accum *= testprime # It's actually prime!
                out.writelines((hex(testprime), '\n'))
                yield testprime
            testprime += 2
            if testprime >= next_square:
                accum *= next_accum
                nextline = inp.readline()
                if (len(nextline) < 1) or (nextline[-1] != '\n'):
                    out.flush()
                    inp.seek(inpos)
                    nextline = inp.readline()
                inpos = inp.tell()
                next_accum = int(nextline, 16)
                next_square = next_accum * next_accum
    
    def next_n(iterator, n):
        """Returns the next n elements from an iterator.
    
        >>> list(next_n(iter([1,2,3,4,5,6]), 3))
        [1, 2, 3]
        >>> list(next_n(primegen(), 10))
        [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
        """
        while n > 0:
            yield iterator.next()
            n -= 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: def causes_exception(lamb): try: lamb() return False except: return True
i have the following code: def cmds(cmd): cmd= Popen(cmd, shell=True, stdout=PIPE) lines = command.stdout.read().splitlines()
Check following code def wipe_mutterings_from( sentence ) while sentence.include? '(' open = sentence.index( '('
So, I have the following code: def LSCPHandler.send_message(message, hostname, port) s = TCPSocket.open(hostname, port)
I have the following code: def incoming_acceptation(incoming_code) if invite_code == incoming_code accepted = true
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
Let's say that I have the following code in application_helper.rb : def do_something if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.