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 6472359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:20:06+00:00 2026-05-25T06:20:06+00:00

I am trying to do some calculations with python, where I ran out of

  • 0

I am trying to do some calculations with python, where I ran out of memory. Therefore, I want to read/write a file in order to free memory. I need a something like a very big list object, so I thought writing a line for each object in the file and read/write to that lines instead of to memory. Line ordering is important for me since I will use line numbers as index. So I was wondering how I can replace lines in python, without moving around other lines (Actually, it is fine to move lines, as long as they return back to where I expect them to be).

Edit

I am trying to help a friend, which is worse than or equal to me in python. This code supposed to find biggest prime number, that divides given non-prime number. This code works for numbers until the numbers like 1 million, but after dead, my memory gets exhausted while trying to make numbers list.

# a comes from a user input
primes_upper_limit = (a+1) / 2
counter = 3L
numbers = list()
while counter <= primes_upper_limit:
    numbers.append(counter)
    counter += 2L

counter=3
i=0
half = (primes_upper_limit + 1) / 2 - 1
root = primes_upper_limit ** 0.5
while counter < root:
    if numbers[i]:
        j = int((counter*counter - 3) / 2)
        numbers[j] = 0
        while j < half:
            numbers[j] = 0
            j += counter
    i += 1
    counter = 2*i + 3
primes = [2] + [num for num in numbers if num]
for numb in reversed(primes):
    if a % numb == 0:
        print numb
        break

Another Edit

What about wrinting different files for each index? for example a billion of files with long integer filenames, and just a number inside of the 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-25T06:20:06+00:00Added an answer on May 25, 2026 at 6:20 am

    You want to find the largest prime divisor of a. (Project Euler Question 3)
    Your current choice of algorithm and implementation do this by:

    1. Generate a list numbers of all candidate primes in range (3 <= n <= sqrt(a), or (a+1)/2 as you currently do)
    2. Sieve the numbers list to get a list of primes {p} <= sqrt(a)
    3. Trial Division: test the divisibility of a by each p. Store all prime divisors {q} of a.
    4. Print all divisors {q}; we only want the largest.

    My comments on this algorithm are below. Sieving and trial division are seriously not scalable algorithms, as Owen and I comment. For large a (billion, or trillion) you really should use NumPy. Anyway some comments on implementing this algorithm:

    1. Did you know you only need to test up to √a, int(math.sqrt(a)), not (a+1)/2 as you do?
    2. There is no need to build a huge list of candidates numbers, then sieve it for primeness – the numbers list is not scalable. Just construct the list primes directly. You can use while/for-loops and xrange(3,sqrt(a)+2,2) (which gives you an iterator). As you mention xrange() overflows at 2**31L, but combined with the sqrt observation, you can still successfully factor up to 2**62
    3. In general this is inferior to getting the prime decomposition of a, i.e. every time you find a prime divisor p | a, you only need to continue to sieve the remaining factor a/p or a/p² or a/p³ or whatever). Except for the rare case of very large primes (or pseudoprimes), this will greatly reduce the magnitude of the numbers you are working with.
    4. Also, you only ever need to generate the list of primes {p} once; thereafter store it and do lookups, not regenerate it.
      So I would separate out generate_primes(a) from find_largest_prime_divisor(a). Decomposition helps greatly.

    Here is my rewrite of your code, but performance still falls off in the billions (a > 10**11 +1) due to keeping the sieved list. We can use collections.deque instead of list for primes, to get a faster O(1) append() operation, but that’s a minor optimization.

    # Prime Factorization by trial division
    
    from math import ceil,sqrt
    from collections import deque
    
    # Global list of primes (strictly we should use a class variable not a global)
    #primes = deque()
    primes = []
    
    def is_prime(n):
        """Test whether n is divisible by any prime known so far"""
        global primes
        for p in primes:
             if n%p == 0:
                 return False #  n was divisible by p
        return True # either n is prime, or divisible by some p larger than our list    
    def generate_primes(a):
        """Generate sieved list of primes (up to sqrt(a)) as we go"""
        global primes
        primes_upper_limit = int(sqrt(a))
        # We get huge speedup by using xrange() instead of range(), so we have to seed the list with 2
        primes.append(2)
        print "Generating sieved list of primes up to", primes_upper_limit, "...",
        # Consider prime candidates 2,3,5,7... in increasing increments of 2
        #for number in [2] + range(3,primes_upper_limit+2,2):
        for number in xrange(3,primes_upper_limit+2,2):
            if is_prime(number): # use global 'primes'
                #print "Found new prime", number
                primes.append(number) # Found a new prime larger than our list
        print "done"    
    def find_largest_prime_factor(x, debug=False):
        """Find all prime factors of x, and return the largest."""
        global primes
        # First we need the list of all primes <= sqrt(x)    
        generate_primes(x)
        to_factor = x # running value of the remaining quantity we need to factor
        largest_prime_factor = None
        for p in primes:
            if debug: print "Testing divisibility by", p
            if to_factor%p != 0:
                continue
            if debug: print "...yes it is"
            largest_prime_factor = p
            # Divide out all factors of p in x (may have multiplicity)
            while to_factor%p == 0:
                to_factor /= p
            # Stop when all factors have been found
            if to_factor==1:
                break
        else:
            print "Tested all primes up to sqrt(a), remaining factor must be a single prime > sqrt(a) :", to_factor
        print "\nLargest prime factor of x is", largest_prime_factor
        return largest_prime_factor
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help calculating Pi. I am trying to write a python program
Hi guys I want to read the following plist file into memory and do
I'm trying to perform some calculations on a non-directed, cyclic, weighted graph, and I'm
I am trying to perform some calculations with a form but every time i
I am trying to create a program that will do some simple calculations, but
Recently I was trying some practice programs in python and I came across this
I am trying to read a very simple but somehow large(800Mb) csv file using
I'm trying to post some information from a from do so some calculations, however
I'm trying to convert a Python dictionary into a Python list, in order to
Python's convention is that variables are created by first assignment, and trying to read

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.