## baby_c 37

hey friends..i'm trying to make a program to find the largest prime factor of a number of 12 digits.but i'm unable to figure out that what's the appropriate variable type for that.can u guys help me...

#include<stdio.h>
int main()
{
int c2;
unsigned long nbr=600851475143,lprime=2,prime,n=3;

do
{
for(c2=2;c2<n;c2++)

if(n%c2==0)
break;

else
{
prime=n;
if(c2==n-1)
{
if(nbr%prime==0)
{
lprime=prime;
//printf("%ld \n",prime);
}

//printf("%ld \n",prime);
break;
}
}
n++;
}while(n<nbr);
printf("%ld\n",lprime);
return 0;
}

this code gives following...

3(prime factor).c: In function ‘main’:
3(prime factor).c:5: warning: integer constant is too large for ‘long’ type
3(prime factor).c:5: warning: large integer implicitly truncated to unsigned type

Looks like your compiler is telling your to use unsigned long int. Try that, one time.

*Always pay attention closely to any compiler error or warnings!*

Learning to debug is an important part of programming, and using compiler messages is a critical part of that.

## baby_c 37

Looks like your compiler is telling your to use unsigned long int. Try that, one time.

*Always pay attention closely to any compiler error or warnings!*

Learning to debug is an important part of programming, and using compiler messages is a critical part of that.

thanks for trying...but it gives the same error..

Edit: Does your compiler support long long int's? or even unsigned long long int's? Try those out.

Now, let's think about finding this greatest prime factor.

You aren't REALLY going to search for a prime number less than N, and ACTUALLY go all the way up to N are you? :(

Do a little Googling, and come back and tell me where the highest number you need to search, really is?

Then we'll chat some more.

## baby_c 37

Edit: Does your compiler support long long int's? or even unsigned long long int's? Try those out.

Now, let's think about finding this greatest prime factor.

You aren't REALLY going to search for a prime number less than N, and ACTUALLY go all the way up to N are you? :(

Oh dear!

Do a little Googling, and come back and tell me where the highest number you need to search, really is?

Then we'll chat some more.

yeah..!i may be i have to think about this further more...thanks for the help...

Does your compiler support type long long int, or unsigned long long int, and will it now handle your 12 digit integer, OK?

## baby_c 37

Does your compiler support type long long int, or unsigned long long int, and will it now handle your 12 digit integer, OK?

no it doesn't... gives

[Error] C:\Users\SUDHEERA\Documents\C\prime.cpp:4: integer constant out of range
[Warning] C:\Users\SUDHEERA\Documents\C\prime.cpp:4: warning: decimal integer constant is so large that it is unsigned

What compiler are you using?

## baby_c 37

What compiler are you using?

The previous errors occurred when using C-free on windows..
But i usually use gcc compiler on ubuntu..it also give errors.

I'm not familiar with C-free, but it appears to be in a cpp (C++) directory. Gcc should have a long long int, and hopefully, an unsigned long long int.

It's going to be quite difficult to find a prime number, if we can't find a data type that we can use to hold the number for us. I don't know of any way to find a prime number, of a string. (Might be a way, but I haven't thought of one yet).

Did your teacher or book talk about some way to use a string as a number to find it's prime factor?

There are BIG INT libraries of course, but I doubt if that's what was intended for you to use.

## baby_c 37

I'm not familiar with C-free, but it appears to be in a cpp (C++) directory. Gcc should have a long long int, and hopefully, an unsigned long long int.

It's going to be quite difficult to find a prime number, if we can't find a data type that we can use to hold the number for us. I don't know of any way to find a prime number, of a string. (Might be a way, but I haven't thought of one yet).

Did your teacher or book talk about some way to use a string as a number to find it's prime factor?

There are BIG INT libraries of course, but I doubt if that's what was intended for you to use.

yeah..i have to find a way to store the number this way or have to find a another way to solve this problem...I'll try in gcc again..

## baby_c 37

yeah..i have to find a way to store the number this way or have to find a another way to solve this problem...I'll try in gcc again..

when using gcc the program complie with a warning
3(prime factor).c: In function ‘main’:
3(prime factor).c:8: warning: integer constant is too large for ‘long’ type

and when it executed,nothing displayed,just stuck at terminal

Did you try using "long long int" and "unsigned long long int"?

## baby_c 37

Did you try using "long long int" and "unsigned long long int"?

both give the same warning..:(

Hmmmm...

Any hints from the book or teacher?

What is your operating system?

I know the 64 bit compilers (and maybe some 32 bit compilers), will allow long long int's, but I'm not sure which one's, exactly.

Might be a way of doing this using a string of individual int's in an int array, but I'm not familiar with it.

OK, well a few things to look for then:

1) A 64 bit compiler that will run on your OS, and (hopefully free) and has long long int's.

and

2) A way to work with int's in an int array, to use the array, as our big number

3) GNU has a BIG INT library that I'm sure could handle this. Is it OK for you to use a library like that, in your program?

## baby_c 37

Hmmmm...

Any hints from the book or teacher?

What is your operating system?

I know the 64 bit compilers (and maybe some 32 bit compilers), will allow long long int's, but I'm not sure which one's, exactly.

Might be a way of doing this using a string of individual int's in an int array, but I'm not familiar with it.

OK, well a few things to look for then:

1) A 64 bit compiler that will run on your OS, and (hopefully free) and has long long int's.

and

2) A way to work with int's in an int array, to use the array, as our big number

3) GNU has a BIG INT library that I'm sure could handle this. Is it OK for you to use a library like that, in your program?

1)My next move is this,use a 64 bit compiler..
2)i don't think i can do that.because i don't very good at that array stuff.
3)yeah..no problem,i need to solve this anyway.don't worry about the method...

i'll inform you when i finished go through these ways..thanks dear.:)

You're welcome, and let us know how you're doing.

I've thought this through a bit more. You can do this with your current compiler. It's not as easy, but here's how:

Make the bigNum a long double data type. Add #include <math.h> for the floating point packages (doubles and floats both).

Now take the sqrt() of bigNum (800 651 475 143.00), and put that sqrt() into another long double variable. Let's call that variable numbr.

Then an unsigned long int num = numbr / 1; //yes, that's a one

Which will get rid of all the digits after the decimal place, and we'll have the right number we want - 775,146.

That's the highest possible prime number less than bigNum. Now you can deal with a much smaller number, and avoid bigNum, for now. An unsigned long int will handle 775,146 very easily.

All you have to do now, is find the largest prime number below 775,146, that is also evenly divisible into bigNum.

Once we have all the prime numbers, we can use an int array to test those numbers, and just repeatedly subtract to simulate division. (Much easier than doing long division with int arrays).

It won't be blazing fast, or all that easy, but if you can't find other solutions you like, it's a way to do it.

This is a bit muddled, but shows getting to the sqrt() of the bigNum, and putting it into an unsigned long int. This was done on an old 16 bit compiler (Turbo C):

/* a program to find the greatest prime factor of a large number

Status: Just getting started
*/

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>   //for sqrt(), and floating point

int main() {
unsigned long int num, i, j, big1;
//unsigned long int nmbr; // = 600 851 475 143;
long double nmbr;
long double bigNum=600851475143.00;
nmbr = sqrt(bigNum);
printf("\n\nBigNum: 600 851 475 143  Nmbr: %Lf", nmbr*nmbr);
num = nmbr / 1;        // 775146 is correct;
printf("\n775146 is correct. Num: %lu", num);
getchar();

/* your other code here */

printf("\n\n\t\t\t    press enter when ready");
i=getchar();
return 0;
}

Not a simple way to an answer, but it is a way to solve the big number problem.

For the prime's, I was thinking of testing them from the highest to the lowest numbers. Maybe use an array and sort them in descending order.

## baby_c 37

I've thought this through a bit more. You can do this with your current compiler. It's not as easy, but here's how:

Make the bigNum a long double data type. Add #include <math.h> for the floating point packages (doubles and floats both).

Now take the sqrt() of bigNum (800 651 475 143.00), and put that sqrt() into another long double variable. Let's call that variable numbr.

Then an unsigned long int num = numbr / 1; //yes, that's a one

Which will get rid of all the digits after the decimal place, and we'll have the right number we want - 775,146.

That's the highest possible prime number less than bigNum. Now you can deal with a much smaller number, and avoid bigNum, for now. An unsigned long int will handle 775,146 very easily.

All you have to do now, is find the largest prime number below 775,146, that is also evenly divisible into bigNum.

Once we have all the prime numbers, we can use an int array to test those numbers, and just repeatedly subtract to simulate division. (Much easier than doing long division with int arrays).

It won't be blazing fast, or all that easy, but if you can't find other solutions you like, it's a way to do it.

This is a bit muddled, but shows getting to the sqrt() of the bigNum, and putting it into an unsigned long int. This was done on an old 16 bit compiler (Turbo C):

/* a program to find the greatest prime factor of a large number

Status: Just getting started
*/

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>   //for sqrt(), and floating point

int main() {
unsigned long int num, i, j, big1;
//unsigned long int nmbr; // = 600 851 475 143;
long double nmbr;
long double bigNum=600851475143.00;
nmbr = sqrt(bigNum);
printf("\n\nBigNum: 600 851 475 143  Nmbr: %Lf", nmbr*nmbr);
num = nmbr / 1;        // 775146 is correct;
printf("\n775146 is correct. Num: %lu", num);
getchar();

/* your other code here */

printf("\n\n\t\t\t    press enter when ready");
i=getchar();
return 0;
}

Not a simple way to an answer, but it is a way to solve the big number problem.

For the prime's, I was thinking of testing them from the highest to the lowest numbers. Maybe use an array and sort them in descending order.

OMG...it worked...!!thanx a lot...

OMG...it worked...!!thanx a lot...

You don't have to act << SO >> surprised!! ;) ;)

This is just the first part, of course. Lots more to be done.

## baby_c 37

You don't have to act << SO >> surprised!! ;) ;)

This is just the first part, of course. Lots more to be done.

U think that i can't do the rest by my self..?

It would have been rather difficult.

However, since then, I have found a fun little scamp called fmod(). fmod() is in math.h (so include it if you haven't already), and what it does is give you the modulo (remainder) for the two doubles that you give it:

result=fmod(double, double);

So with that help, it became very easy to cast my unsigned long int I had found was a prime number, already, into a long double like bigNum, and have it tell me when the two were zero - that is, when the prime number I'd just found, was a factor of bigNum.

Oh yeah! ;)

so first, get the prime number
if(result equals zero)

• then primeNumber is a factor
• save it as the biggest factor so far, if you're
• genererating prime's from small to large.

end of if

print up your summary and the biggest prime factor.

Check out fmod() and see if it's included in your compiler's functions.

## baby_c 37

It would have been rather difficult.

However, since then, I have found a fun little scamp called fmod(). fmod() is in math.h (so include it if you haven't already), and what it does is give you the modulo (remainder) for the two doubles that you give it:

result=fmod(double, double);

So with that help, it became very easy to cast my unsigned long int I had found was a prime number, already, into a long double like bigNum, and have it tell me when the two were zero - that is, when the prime number I'd just found, was a factor of bigNum.

Oh yeah! ;)

so first, get the prime number
if(result equals zero)

• then primeNumber is a factor
• save it as the biggest factor so far, if you're
• genererating prime's from small to large.

end of if

print up your summary and the biggest prime factor.

Check out fmod() and see if it's included in your compiler's functions.

that worked for some range..but for my BIG number it run and stuck at command prompt..

Post up your code, and I'll see what's up with it. I've run it through with the big number about 50 times now, with no problem.

You have the big number assigned to a long double? And your printing it with %Lf, instead of just %lf?

## sree_ec 10

I am really sorry to interrupt your discussion.
the way to resolve the warning is to use [from your first post]

unsigned long long nbr=600851475143ULL;

thanks..

commented: this is right +1

I made that request

Did you try using "long long int" and "unsigned long long int"?

but she replied that it made no difference. She's using the "C-free" compiler, which may not support "long long int".

I don't know if she tried appending the "ULL" onto the end of the bigNum variable, however. Good point!

I found a way to work with this large number, in Turbo C, but she is still having some difficulty, at last report.

You are more than welcome to join in - it's problem solving, and should be from multiple sources.

Please don't post the code if you have made it already. It's like a Snipe hunt - you remember them your whole life, because they were so very hard to catch! ;)

## sree_ec 10

I made that request

but she replied that it made no difference. She's using the "C-free" compiler, which may not support "long long int".

gcc supports long long int..
in 32 bit machines, long is 32 bits and long long is 64

I don't know if she tried appending the "ULL" onto the end of the bigNum variable, however. Good point!

Unless we add ULL as suffix, the warning wont go since gcc considers the constants as signed long int by default as far as i know.
I have been using the suffix UL, ULL very often with long and long long {unsigned}. I think that is the correct way.

If there is any other reason to use these suffix , please post them..

## baby_c 37

Post up your code, and I'll see what's up with it. I've run it through with the big number about 50 times now, with no problem.

You have the big number assigned to a long double? And your printing it with %Lf, instead of just %lf?

#include<stdio.h>
#include<math.h>

int main()
{
int c2;
long lprime=2,prime,n=3;

do
{
for(c2=2;c2<n;c2++)

if(n%c2==0)
break;

else
{
prime=n;
if(c2==n-1)
{
if(fmod(4559877,prime)==0)
{
lprime=prime;
//printf("%ld \n",prime);
}

//printf("%ld \n",prime);
break;
}
}
n++;
}while(n<4559877);
printf("%ld\n",lprime);
return 0;
}

Where did you get the 4559877 number? That's way big!

Correct is sqrt() of bigNum or 775,146 (and the decimal places thereafter you can forget about).

** I'm talking about line #34 in your program, not the fmod() line of code. **

Windows calculator will handle and confirm the above number.

Did you try the suffix ULL in gcc or C-free, for unsigned long long int's, yet?

Sree ec says they work in gcc for both 32 and 64 bit versions.

## baby_c 37

Where did you get the 4559877 number? That's way big!

Correct is sqrt() of bigNum or 775,146 (and the decimal places thereafter you can forget about).

** I'm talking about line #34 in your program, not the fmod() line of code. **

Windows calculator will handle and confirm the above number.

Did you try the suffix ULL in gcc or C-free, for unsigned long long int's, yet?

Sree ec says they work in gcc for both 32 and 64 bit versions.

sorry that's a mistake .. the number is 600851475143