Thread: help with prime factor for 12 digit number

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    17

    help with prime factor for 12 digit number

    so lets say we have a 12 digit number for example 192837401928

    i ve made a code that could calculate prime factors but i cant make it calculate prime factors for such big numbers

    for example

    Code:
    #include <stdio.h>
    int main(){
        unsigned long i,y,k;
            for (i=1;i<=1000000;i++){
                y=0;
                for (k=2;k<i;k++){
                       if (i%k!=0){
                              y = y+1;
                              }
                              }
                if (y==i-2){
                            if (1000000%i==0){
                                        printf("prime factor: %lu\n",i);
                                        }
                            }
                            }
            getchar();
            return 0;
            }
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Is the numeric range of long on your machine 12 digits?

    It's about 9.5 digits on a typical 32-bit machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    17
    Quote Originally Posted by Salem View Post
    Is the numeric range of long on your machine 12 digits?

    It's about 9.5 digits on a typical 32-bit machine.
    sizeof(long) shows 4

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your point being (only to prove my point)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nick2 View Post
    sizeof(long) shows 4
    Maybe you should then pick a different data type.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    17
    can anyone recommend me anything?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you can't find your maximum unsigned long value, then run a test like this:

    Code:
    /*find the largest unsigned long int on this system -
    untested for accuracy
    */
    
    #include <stdio.h>
    
    int main(void)  {
    
       unsigned long int n = 1000000;  //does your compiler have type long long, btw?
       do  {
          n += 10000;
          if(n % 10000000 == 0) {
             //gotoxy(10,10);
             printf("%20lu", n);
          }
       }while(n > 501);
    
       //n has overflowed the data type, so step it back until it's positive again
       printf("\n\n\t Backing Up ");
       do  {
          n -= 1;
       }while(n < 1000000);
    
       printf("\n\n\t Maximum unsigned long value is %lu", n);
       printf("\n\n\t\t\t     Press Enter When Ready ");
       n = getchar();
       return 0;
    }
    You'll have the answer in a few seconds. On a 16 bit compiler it's a tad over 4.29 Billion, if my program is correct.

    If you don't have type unsigned long long int (for a higher range of int's), then I know of only two alternatives:

    1) Remembering the number line, write your own code to handle each digit in the number.

    2) Use a big number C library (Google it, I don't have a url).
    Last edited by Adak; 06-15-2009 at 10:58 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nick2
    can anyone recommend me anything?
    In C99, you could use a long long or unsigned long long.

    Quote Originally Posted by Adak
    If you can't find your maximum unsigned long value, then run a test like this:
    It would be simpler to just #include <limits.h> and check ULONG_MAX.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's what I was trying to think of, but got stuck on MAX_ULONG, and knew that was wrong!

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    17
    i ve searched about C99 but cant find my way out

    could anyone please explain me what it is and how i can use it to solve my problem?

    thanks

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    C99 is an abbreviation for the 1999 edition of the C standard. What I meant was that if you were using that version of C, then the long long and unsigned long long types would be available for your use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    17
    guys i searched, but i cant make a program in C using DEV C++ to display a 12 digit number

    what code would you suggest to write?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Perhaps a divide and conquer idea would work. That is, consider the leftmost 6 digits as number1, and the rightmost 6 digits, as number 2.

    Print number1 then number2: 123456654321.

    You will have to include the "carry", logic:

    all digits in number2 get zero'd out when there is a carry into number 1.

    And your overall logic will have to recognize that number1 is really number1 * 1000000

    So your overall number is number1 * 10^6 + number2.

    You may want to work with your digits in the numbers, at times, as char's. It can make life easier to have the numbers, and then also have the char's with those same digits, depending on how you want to do this.

    I'm sure there are big number libraries, but I'm not familiar with Dev C++.

    Another way to do this would be to just write your own big number routines (using char arrays to hold the digits trick), but you have to know how to use the number line (e.g., what really is 428 for example), to do it.

    428 is really 4 * 10^2 + 2 * 10^1 + 8 (or 8 * 10^0).

    You googled big number library Dev C++, and got no hits?

  14. #14
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
     
    typedef struct {
      unsigned int x : NUM_BITS_NEEDED;
    }my_large_number;
    I'll leave figuring out how many bits are needed for a 12 digit number up to you
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nick2
    i cant make a program in C using DEV C++ to display a 12 digit number

    what code would you suggest to write?
    Assuming that you are using the latest release of Dev-C++, you would be using the MinGW port of gcc 3.4.2, if I remember correctly. This supports long long, but you may have to enable C99 support in the compiler options. As for an example program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        unsigned long long x = 192837401928ULL;
        printf("%llu\n", x);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Prime number check
    By password in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2007, 10:30 AM
  3. More fun with the chinese prime number algorigthm
    By kryonik in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 04:41 PM
  4. 4 digit number
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 06-21-2005, 04:41 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM