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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:04:48+00:00 2026-06-14T20:04:48+00:00

I don’t know where I am doing wrong in trying to calculate prime factorizations

  • 0

I don’t know where I am doing wrong in trying to calculate prime factorizations using Pollard’s rho algorithm.

#include<stdio.h>
#define f(x)  x*x-1

int pollard( int );
int gcd( int, int);

int main( void ) {
    int n;
    scanf( "%d",&n );
    pollard( n );
    return 0;  
}

int pollard( int n ) {
    int i=1,x,y,k=2,d;
    x = rand()%n;
    y = x;

    while(1) {
        i++;
        x = f( x ) % n;
        d = gcd( y-x, n);

        if(d!=1 && d!=n)
            printf( "%d\n", d);

        if(i == k) {
            y = x;
            k = 2 * k;
        }
    }
}   
int gcd( int a, int b ) {

    if( b == 0) 
        return a;
    else 
        return gcd( b, a % b);
}
  • 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-14T20:04:49+00:00Added an answer on June 14, 2026 at 8:04 pm

    One immediate problem is, as Peter de Rivaz suspected the

    #define f(x)  x*x-1
    

    Thus the line

    x = f(x)%n;
    

    becomes

    x = x*x-1%n;
    

    and the precedence of % is higher than that of -, hence the expression is implicitly parenthesised as

    x = (x*x) - (1%n);
    

    which is equivalent to x = x*x - 1; (I assume n > 1, anyway it’s x = x*x - constant;) and if you start with a value x >= 2, you have overflow before you had a realistic chance of finding a factor:

    2 -> 2*2-1 = 3 -> 3*3 – 1 = 8 -> 8*8 – 1 = 63 -> 3968 -> 15745023 -> overflow if int is 32 bits

    That doesn’t immediately make it impossible that gcd(y-x,n) is a factor, though. It just makes it likely that at a stage where theoretically, you would have found a factor, the overflow destroys the common factor that mathematically would exist – more likely than a common factor introduced by overflow.

    Overflow of signed integers is undefined behaviour, so there are no guarantees how the programme behaves, but usually it behaves consistently so the iteration of f still produces a well-defined sequence for which the algorithm in principle works.

    Another problem is that y-x will frequently be negative, and then the computed gcd can also be negative – often -1. In that case, you print -1.

    And then, it is a not too rare occurrence that iterating f from a starting value doesn’t detect a common factor because the cycles modulo both prime factors (for the example of n a product of two distinct primes) have equal length and are entered at the same time. You make no attempt at detecting such a case; whenever gcd(|y-x|, n) == n, any further work in that sequence is pointless, so you should break out of the loop when d == n.

    Also, you never check whether n is a prime, in which case trying to find a factor is a futile undertaking from the start.

    Furthermore, after fixing f(x) so that the % n applies to the complete result of f(x), you have the problem that x*x still overflows for relatively small x (with the standard signed 32-bit ints, for x >= 46341), so factoring larger n may fail due to overflow. At least, you should use unsigned long long for the computations, so that overflow is avoided for n < 2^32. However, factorising such small numbers is typically done more efficiently with trial division. Pollard’s Rho method and other advanced factoring algorithms are meant for larger numbers, where trial division is no longer efficient or even feasible.

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

Sidebar

Related Questions

Don't know if it's I'm doing it wrong or if there's a bug (I
don't know if the title describes anything about what I'm trying to say but
Don't know what's wrong here, when I run the application it says Specified method
Don't know a whole lot about streams. Why does the first version work using
Don't know why this is happening, but after submitting a form via JS (using
Don't know how to explain it better but i'm trying to get a response
Don't know if I'm over-thinking this or not.. but I'm trying to be able
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
Don't know a better title but here is what im trying to do. I
don't know what happened in my jcrop selection. I think I pressed some keys

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.