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

  • SEARCH
  • Home
  • 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 8161543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:31:17+00:00 2026-06-06T18:31:17+00:00

I’m currently trying to find the largest prime number contained within another large number.

  • 0

I’m currently trying to find the largest prime number contained within another large number.

maxlen = 1024
for i in range(1023, -1, -1):
    maxlen -= 1
    number = ""
    for k in range(maxlen, -1, -1):
            number = pi[k] + number
            if isprime(number) == True:
                    print number

isprime() is a function that checks if the number is a prime (pretty standard).
this works pretty well up to a certain point where I get a MemoryError.

This is not because the number checked by the function is too large since it happens around the 6th run of the first for loop.

I’ve already tried gc.enable() and gc.collect() without any positive result.

Does anyone have an idea how to fix this?

Edit: definition of pi and isprime() as per request:

f = open("/root/number", "r")
pi = f.read()
f.close()

where the file “number” contains the original number in which I’d like to find the prime.

def isprime(n):
    n = abs(int(n))
    if n < 2:
            return False
    if n == 2:
            return True
    if not n & 1:
            return False
    for x in range(3, int(n**0.5)+1, 2):
            if n % x == 0:
                    return False
    return True

Traceback:

Traceback (most recent call last):
  File "./primal.py", line 36, in <module>
    if isprime(number) == True:
  File "./primal.py", line 24, in isprime
    for x in range(3, int(n**0.5)+1, 2):
MemoryError
  • 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-06-06T18:31:18+00:00Added an answer on June 6, 2026 at 6:31 pm

    Use xrange instead of range, most importantly in isprime, here:

    for x in xrange(3, int(n**0.5)+1, 2):
    

    xrange doesn’t create the whole list in memory, while range does, but you are not using the results after you iterated over them.

    Another tip: just test for isprime(n), there is no need to see if it is equal to True, that is what if does. 🙂

    if isprime(number):  # Only works if isprime(number) is boolean True
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.