If the number is greater than 2^32(4294967296)then how to determine whether is it prime or not? Ex:Is 4437864713 a prime number ?
If the number is greater than 2^32(4294967296)then how to determine whether is it prime or not? Ex:Is 4437864713 a prime number ?
Type "prime number algorithm" into google.
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.
Use long long or __int64.
@Salem : I know the algo of the problem but main problem is how to handle the large number.As we can't use double data type cause we need to use % operator.
Can any provide me a little code snippet for handling large number(>4294967296) and we can use % operator too with the number.
Well if a 64-bit type isn't an option, then try a bignum library like The GNU MP Bignum Library
Or roll your own equivalent, if you're only interested in a few specific operations.
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.
Code:unsigned long long number = 0x7FFFFFFFFFFFFFFF; printf("%lld\n", number);
Unless your compiler is an antique, it has a 64 bit integer type.
Check your docs... Different comiplers implement 64 bit integers in different ways.
It might be one of...
1) unsigned long long int
2) #include <stdint.h> then use uint64_t
3) unsigned long int
The easiest way to tell is to try different combinations and use sizeof() ... a 64 bit int will return 8 instead of 4.
Try...
Code:#include <stdio.h> #include <stdint.h> int main (void) { uint64_t x; x = 600851475143; printf("%lld", x ); return 0; }
> When i use 600851475 then it give the result.I am using codeblock on windows.
Then unfortunately, you HAVE to use the non-portable Microsoft conversion formats.
Format Specification Fields: printf and wprintf Functions (CRT)
Although the compiler is GCC, the 'C' run-time library is from Microsoft, so you get all the bugs/features of that library.
One of the "bugs" being very poor support for C99 types.
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.
I tried the following on windows(codeblock) and linux(gcc) :
Results in both case are same :Code:#include <stdio.h> #include <stdint.h> int main() { uint64_t x; unsigned long long y; unsigned long int z; printf("%d\n",sizeof(x)); printf("%d\n",sizeof(y)); printf("%d\n",sizeof(z)); return 0; }
8
8
4
but when i try to assign value 600851475143 to x or y then i get compile time error as :
that's my problem when why compiler show such error while the number is too short than 2^64.error : integer constant is too large for "long" type
One more thing long is expected to have size of 8 byte but here we get 4 byte ,why?
Use a bignum library. Bignum libraries allow you to use arbitrarily large numbers. For Linux, use GMP. For Windows, I prefer iMalc's library. You can get GMP through your distro's package management system. You can get iMalc's library at his website, Useful classes, you want BigInt or VarBigInt.
Last edited by User Name:; 01-26-2011 at 09:08 PM.
long is generally only 32 bits... you need long long to get 64.
That doesn't make any sense... your number is only 39bits... there's no reason it won't fit in a uint64_t ... unless your compiler is all messed up... Did you try this both on Windows and Linux?
It works here on both VC++ (with my re-worked header file) and on PellesC ....