few years ago, i was asked to solve a "simple" c++ problem. The problem is: output the result of 3^9999 in c++. The trap is in the size of the resulting number, it is gigantic, so there should be some kind of trick, the last i remember that someone solved it by placing the answer inside the array... But i couldn't get it, please help me with this one (don't suggest any internal libraries)
As you mention array, perhaps the number was actually stored as a c-string (char-array)?
It holds individual characters, like so:
1 2 3 4 5 6 7 8
char number[50]; //max number size of 50 digits
number[0] = 3; //set initial number to three
char result[50];
for (int i=0; i<9999; i++)
{
square(number);
}
Where the square function takes a reference to the number and multiplies it by its own.
This is done as you would do a multiplication on paper, multiplying every digit and using a variable to do carrying to the next number.
I know this is probably not the most optimal solution, but this is possibly what school teachers are looking for by giving this as a basic exercises.