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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:04:08+00:00 2026-05-27T03:04:08+00:00

Actually, given N a (possibly very large) even integer, I want to find N

  • 0

Actually, given N a (possibly very large) even integer, I want to find N = F * R where gcd(F,R) = 1, F>R, and F is as small as possible (since I’ll be completely factoring F). The heart of the problem is finding the largest divisor R where R < sqrt(N).

For example, N=36 should give F=9 and R=4. Notice that R is not necessarily prime, or a prime power. Note that I am NOT factoring N. The only restriction on F and R is that they are relatively prime.

This is my quick and naive version, which is working:

def factor_partial(N):
    for R in xrange(int(math.sqrt(N)),1,-1):
        if N%R == 0 and gcd(R,N/R) == 1:
            return N/R, R

Another way I imagine doing it is by finding the divisors in increasing order, and removing any multiples of nondivisors along the way. Something like:

def factor_partial(N):
    i = range(2, int(sqrt(N)) + 1)
    while i:
        if N % i[0] != 0:
            remove_multiples(i, i[0]) #without removing i[0]
        else:
            if gcd(i[0], N/i[0]) == 1:
                R = i[0]
        i.pop(0) #remove i[0]

    return N/R, R

I think this will be slow and memory intensive, but perhaps if i is instead a generator it could be efficient. I haven’t used generators much.

Can I improve the first version? Is the second version viable (how would I do it)? Is there a completely different method that is better?

Looking for answers in python, c, or pseudocode.


This is for a project for a class on number theory. I’m implementing a primality test based on Pocklington. While I need a factorization algorithm, we haven’t studied any and I’m probably not going to use one such as the quadratic sieve which is outside the scope of my class. I’m looking for specific help with the question posed.

  • 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-27T03:04:09+00:00Added an answer on May 27, 2026 at 3:04 am

    Wikipedia has a nice list of factoring algorithms: http://en.wikipedia.org/wiki/Integer_factorization#Factoring_algorithms

    Your second approach effectively uses a sieve and has the nice property of quickly reducing the problem when N is a multiple of some small prime. The code can be improved by looping over primes rather than all possible divisors for 2..sqrt(n).

    Also, you may want to start out with a primality test so that you know that N is composite before doing additional work.

    Your note says that you are not factoring N but the problems are related. The search for F and R amounts to exploring non-overlapping combinations of the prime factors of N.

    In the case of N==36, the prime factorization of N is 2, 2, 3, 3. The factors of F and R must include all of those (so that F*R==N) and there can be no overlap (so that GCD(F,R)==1). So 4 and 9 emerge immediately.

    A more instructive example may be N==23256. Its factorization is 2,2,2,3,3,17,19. Since there can be no overlap between F and R, each prime base can only go into one of the two buckets (i.e. you either get all the twos or none of them). So, we can group the factors into 8,9,17,19. To find R, we want the combination of those factors that is as large as possible but below 152.49, the square-root of 23256. Our choices are {8}, {9}, {8,9}, {8,17}, {8,19}. The largest of those is 8*19 which is 152. The corresponding F is 17*19 or 153.

    The choices listed above are computed as [choice for choice in powerset([8,9,17,19]) if prod(choice) < math.sqrt(N)].

    So the whole program pretty much boils down to this:

    prime_factors = factorize(N)      # [2,2,2,3,3,17,19]
    clusters = [p**e for p, e in collections.Counter(prime_factors).items()]  # [8,9,17,19]
    R = max(prod(group) for group in powerset(clusters) if prod(group) < math.sqrt(N))
    F = N // R
    

    The powerset search can be made faster by pruning the generation of sets whenever they exceed the square root on N.

    Keep in mind that factorizing is computationally expensive and powersets grow very quickly but it is likely far less work than starting that the original algorithm which does many divisions starting at the square root of N and working downwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large and unique integer (actually a SHA1 hash). Note: While I'm
What I want to do (I actually don't know if it's possible) is to
I am actually using h:selectOneRadio to display items, given to it from f:selectItems tag.
I have to store two files A and B which are both very large
I am completely new to objective c. I am trying to compile a very
For a very specific reason I want to select ListViewItem s on mouse button
Can someone give an example of a good time to actually use unsafe and
Actually, this question seems to have two parts: How to implement pattern matching? How
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
Actually here is a similar topic with little practical value. As far as I

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.