Hello!
I'm very much a beginner at programming. I'm teaching myself by trial-and-error whilst my partner is taking a basic C++ class at Uni. He wrote most of this program, but I've done a fair bit of the nitpicky stuff, like stdout to text file and the initial looping function. I'm pretty familiar with basic syntax, but I know about as much as someone would after a week of class. Suffice it to say, I'm very new and inexperienced!
So this program we've got is a basic Proth number tester. It takes the formula N=k*(2^n)-1 and asks user for k as well as lower and upper bound of n. It then tests N for k at all values of n between n(lower) and n(upper). It then prints those to a neat text file.
The main problem that irks me at the moment is that I don't know how to overcome an overflow error. The best way I can describe it is that it's similar to what I've seen in Java - cannot have more than 2^31 of "something."
Hence, our results file might look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
You chose 5 as your value of k.
You chose 28 as your lower bound and 29 as your upper bound.
Here are the results...
5*2^28-1 is 1342177279.
1342177279 is divisible by 17989 and has the factors 17989 and 74611.
1342177279 appears to be composite.
5*2^29-1 is -2147483648.
-2147483648 is divisible by 4 and has the factors 4 and -536870912.
-2147483648 appears to be composite.
All done.
|
I have heard that we ought to be using unsigned long. I've seen some examples of this, but can't input it in a syntactically correct manner.
From my basic understanding, the length of the integer N=5*2^29-1 is more than 10 digits and therefore cannot be represented by our program.
How do I make the program able to deal with larger (or very large) numbers?
Also, is there a way to exceed 2^64 (which I understand to be the limit of unsigned long in C++) without being on a 64-bit system?
Thanks in advance for any help/explanation. I recognize this is quite the newbie issue, but I can't figure out proper implementation no matter how many articles I read.