Thread: How to factor a number of 100 characters into prime factors in 1-2 seconds?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Here is a link to an online factoring program that also includes source code, that finds the the factors for that large number in a fraction of a second.

    Integer factorization calculator

    The online factoring program accepts expressions, such as 2^512 - 1.
    Last edited by rcgldr; 01-06-2021 at 01:48 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,643
    Quote Originally Posted by rcgldr View Post
    Here is a link to an online factoring program that also includes source code, that finds the the factors for that large number in a fraction of a second.
    Really? Try this:
    Code:
    1017803105208811141547706054010446761424659440478753690892748944869985260751624643110487274054409671
    Obviously his extremely simple example number can be factored in a fraction of a second. But that's only because it's factors are small.

    EDIT: Even a simple-minded program like the following can factor the given number in a fraction of a second.
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <gmp.h>
     
    int main()
    {
        const char *num = "932865073719992059629773513614789388266580305083"
                          "920591925740371392254317064584855785088915745761";
        // = 995663^8 * 995669^8
     
        mpz_t n;
        mpz_init_set_str(n, num, 10);
     
        unsigned long d = 2;
        int cnt = 0;
        bool first = true;
     
        while (mpz_cmp_ui(n, 1) != 0)
        {
            if (mpz_fdiv_ui(n, d) == 0)
            {
                ++cnt;
                mpz_fdiv_q_ui(n, n, d);
            }
            else
            {
                if (cnt > 0)
                {
                    if (!first) printf(" * ");
                    else        first = false;
                    printf("%lu", d);
                    if (cnt > 1) printf("^%d", cnt);
                    cnt = 0;
                }
                d += 1 + (d % 2); // so it counts: 2, 3, 5, 7, 9, ...
            }
        }
     
        if (cnt > 0)
        {
            if (!first) printf(" * ");
            printf("%lu", d);
            if (cnt > 1) printf("^%d", cnt);
        }
     
        printf("\n");
        mpz_clear(n);
     
        return 0;
    }
    Last edited by john.c; 01-06-2021 at 02:39 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by john.c View Post
    Obviously his extremely simple example number can be factored in a fraction of a second. But that's only because it's factors are small.
    If the the factors are all large prime numbers it's going to take quite a while, but for 2^512-1, one of the factors is a 62 digit prime, and that takes a fraction of a second.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find Prime Factors Of Given NUmber
    By san12345 in forum C Programming
    Replies: 2
    Last Post: 12-03-2015, 02:35 AM
  2. Finding The Biggest Prime Factor Of a Number
    By thedardwhie in forum C Programming
    Replies: 9
    Last Post: 02-11-2015, 12:58 PM
  3. help with prime factor for 12 digit number
    By nick2 in forum C Programming
    Replies: 15
    Last Post: 06-19-2009, 04:39 AM
  4. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM

Tags for this Thread