Connecting Tech Pros Worldwide Forums | Help | Site Map

how to get a 32 bit cpu to recognize massive numbers

Member
 
Join Date: Aug 2009
Posts: 64
#1: Oct 13 '09
Hi JosAH I thank you for your help now I'm mixing your idea with new and if I solve this problem I may have the worlds smallest mersenne prime finding code lol. basically when ever I do a check to prove a=pow(2,i)-1 is prime I can't get it to do the math properly for any number over 2^32 how can I do this in a simple manner. if I can get around this problem I'll have a possible code less than 100 line to beat GIMPS with. Can anyone help ?

Member
 
Join Date: Aug 2009
Posts: 64
#2: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #pragma hdrstop
  6.  
  7. #include <tchar.h>
  8. //---------------------------------------------------------------------------
  9.  
  10. #pragma argsused
  11. static int isPrime(int n) {
  12.     int i, inc= 2;
  13.     if (n == 2 || n == 3) return 1;
  14.     if (n%2 == 0 || n%3 == 0) return 0;
  15.     for (i= 5; i*i <= n; i+= inc, inc= 6-inc)
  16.         if (!(n%i)) return 0;
  17.     return 1;
  18. }
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21. unsigned long long i = 3;
  22. unsigned long long a=0;
  23. for (i = 3; i < 2147483647; i=i+2) {
  24. a=pow(2,i)-1;
  25. if (isPrime(i) && isPrime(a)){
  26. printf("%d,",i);
  27. }
  28. }
  29.  
  30. system("pause");
  31.     return 0;
  32. }
  33. //---------------------------------------------------------------------------
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#3: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


You need to write your whole application using 64 bit ints. You use unisgned long long in your main function but not your isPrime function.

You will also need to write your own version of pow to handle 64 bit ints too.
Member
 
Join Date: Aug 2009
Posts: 64
#4: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


Quote:

Originally Posted by Banfa View Post

You need to write your whole application using 64 bit ints. You use unisgned long long in your main function but not your isPrime function.

You will also need to write your own version of pow to handle 64 bit ints too.

for the first advice thanks it's code I was told about by JosAH, the second part I don't get as pow is a standard math.h function.
Member
 
Join Date: Aug 2009
Posts: 64
#5: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


also banfa I want it to work to check primality of numbers greater than the 64 bit limit . how could I define a bigger class of integers? Gimps has found numbers in the 43 million+ bit league.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#6: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


you need to write pow because the prototype for pow is

double pow(double base, double exponent);

that is pow is double based (in C) but you pass a 64 bit integer as the exponent. A double does not have the precision to accurately represent a 64b it integer. I double holds 16 significant digits but a 64 bit integer holds 20 decimal digits. C++ is not better it has no implementation of pow that accepts a 64 bit integer as the exponent.

Since the result of pow a floating point and therefore an estimate when you convert it to an integer you could introduce an error of 1.

Is this C or C++?

In C++ I would say create a class (or use a third party class) that implements >64bit precision integers.

You could do the same in C but it would be a little more cumbersome as you do not have actually classes to use but a structure and some accessor functions should do the trick.
Member
 
Join Date: Aug 2009
Posts: 64
#7: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


it's C that I'm using but now I'm destroyed because I have no idea how to do this stuff also it's not the exponent that's messing it up it's the result of a=2^i-1 if you have i over 32 you can't write it as 32 bit but even 64 bit will only find one more 2^61-1 the way I'm checking I need to be able to use the full value of a to check primality but unless i can make a class of integer over 43 million bits of memory I have no chance. it'd be cool if i could because then I can beat GIMPS with very little code.
Member
 
Join Date: Aug 2009
Posts: 64
#8: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


now my compiler gave me an access violation to a part of memory I have 4 GB of ram now and I have a 2.80 ghz processor (32 bit) .
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#9: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


That is why you need to write your own version of pow using your own multibyte integers.

However once you have introduce code to implement >64 bit integers your code will no longer be little.

The principle isn't terribly hard, when you think of an integer

53347

you normally think of it in some base or another. Normally you let the computer deal with base and the digits and the carry in multiplication and addition and borrow in subtraction and division.

Instead of letting the computer deal with the base and the carry and the borrow what if you used base 65536. You hold each digit of the number separately in an unsigned short. When you perform arithmatic you use 32 bit arithmatic so that you can capture the carries/borrows and you extend you number with extra digits as required by using a dynamically allocated array of unsigned short to hold it.

Try reading this.
Member
 
Join Date: Aug 2009
Posts: 64
#10: Oct 13 '09

re: how to get a 32 bit cpu to recognize massive numbers


okay I've thought of how this might work in binary but how do I know what it will be in decimal if I can't do the math to figure it out ? also I need 7 variables to beat gimps then. I think i know how to carry but I still don't know how i can figure out how to write it in decimal also I'd need to figure out if it's prime without rewriting the prime function I don't understand already I have no way to check if it's prime.
Reply