@lastchance - I had written a good and long explanation of the whole scenario but I left my system to eat and after returning when I updated a little and hit submit, it was all gone. So, I'll summarize everything.
There is no problem from the compiler, but I am facing a problem with huge numbers. I want to compute with gargantuan numbers but I don't know how to do that without using a library like GMP because I have not studied any document on it, I have only glanced at a few.
My program divides an even number by 2 and if the following output or even if the inputted number is odd, it gets converted into an even number either by addition of 1 or subtraction. I have chosen to go with addition of 1 and since it would start to loop after 1 due to the structure, I have set it to terminate the loop as soon as the number reaches 2 and then simply print out 1 to the console as that is the last value. I could also have gone the other way of subtraction and terminated it at one as 1-1 would have given 0, a different value. There certainly are better ways to do it, but this is how I have done it.
Here is the code :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <cmath>
using namespace std;
int main(){
unsigned long long n;
unsigned long long number = 1;
do {
number++;
n = number;
cout << "Evaluating for - " << n << endl;
if (n>1) {
do {
//even operation
if (n % 2==0)
do {
for (; n % 2==0; n = n/2)
cout << n << endl;
} while (n % 2==0);
//odd operation
if (n>1 && n % 2!=0)
do {
for (; n % 2!=0; n++)
cout << n << endl;
} while (n % 2 !=0);
} while (n>2);
} cout << 1 << endl;
} while (number<(pow(2,6)));
return 0;
}
|
It's a console application for now and for now, I have set a limit on line 30th. But later on, I would allow the user to be able to input the limit. The number can be anything and I want my program to be prepared for it. And I will set a limit which the user's value will have to stay within, or else it would be rendered invalid. I will be adding a check for that as well as other invalid inputs such as negative numbers or strings later on after grounding the foundation. My current limit is 2^6 but that's only so that my computer doesn't freeze. But even when selecting the limit, I want to keep some headroom available. So for example, I'm working with unsigned long long which goes upto 2^62 but I will set it to be 2^60 or 2^50 or anything less than the actual limit of the data type. But I want to go beyond 2^62, way beyond it. I can manually set a number instead of using an algebraic expression, an equation to define it but I that would be unorthodox. I don't think there is a way to simply allot 1024 bytes to a certain variable as I believe the operations and manipulation of the bits on the bit-wise level pertains to and remains the same regardless of the byte-size, which I think is what all the operations are predicated upon. I don't know how to work with big numbers. That is the problem that I'm facing. The problem I had before was regarding the exp() function, or the exponential function which learnt was given by pow() in C++. But that is moot now, and so, I marked this thread as solved.
And interestingly enough, on line 30, less than acts the same as less than equal to. What could be causing that? And my writing may not be very elegant but this is how I've done it, bear with it.