# Thread: help with prime factor for 12 digit number

1. ## 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. Is the numeric range of long on your machine 12 digits?

It's about 9.5 digits on a typical 32-bit machine.

3. Originally Posted by Salem
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. Your point being (only to prove my point)?

5. Originally Posted by nick2
sizeof(long) shows 4
Maybe you should then pick a different data type.

6. can anyone recommend me anything?

7. 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).

8. Originally Posted by nick2
can anyone recommend me anything?
In C99, you could use a long long or unsigned long long.

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.

9. That's what I was trying to think of, but got stuck on MAX_ULONG, and knew that was wrong!

10. 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. 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.

12. 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. 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. 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. 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;
}```

Popular pages Recent additions