# Thread: Power of N + Overcome Size Limitations

1. ## Power of N + Overcome Size Limitations

Hi, i am writing a program that asks the person for a number and then will out put that number by the power of 2. so 2 ^ 6 = 64. This program needes to be created without using the math functions all the maths have to be done programmaticaly.

i know i am prob wrong because i cant get anywhere with this program. here is the code so far.

Code:
```#include <iostream>

using namespace std;
int power;
int n;
int counter;
int main ()

{

cout <<"Please enter a power number";

cin >> n;

counter = 2;

if(counter < n)
{
counter*2;
}

if(counter == n)
{
counter * n;
}

cout <<counter;

system("pause");
}```
the other problem i have is once i have the power to work i need to allow the program to overcome size limitations and show all the numbers. for example i need

Code:
`2^200 = 1606938044258990275541962092341162602522202993782792835301376.`
if anyone can help me with this then thanx soo much as i am finding this program hard to complete.

2. This will be a very hard problem for you*, because it seems as if you're a complete beginner. My advice is to study some example code and try to make some easier programs first, so you learn the basic concepts.

*) the last problem that is, the first one is quite easy. To solve the first one you should use a loop.

3. You need 2 variables - the counter, that you input, and a power variable, which will hold the output.

Edit: The second problem is much harder - you're trying to implement arbitrary-precision arithmetic.

4. You'll probably want to use a loop.

5. i have gone through all of the sheet on the basics n then this is the next question, lol. i am really stuck on it as you can prob tell.

6. can anyone give me any pointers on how to overcome the size limitations?

7. Originally Posted by peckitt99
can anyone give me any pointers on how to overcome the size limitations?
If you're having trouble with the first part, then forget about the second part. It's orders of magnitude harder. I doubt if your instructor expected anyone to actually finish that part.

Edit: Actually, when I think about it, if the only arithmetic you have to implement is powers of 2, it's not nearly as bad as the general case. Don't get me wrong, it's still much harder than the first part, but still doable with some work. The general idea for arbitrary-precision arithmetic is that you represent numbers by strings instead of built-in numerical types, then implement by hand the rules of arithmetic you need, since the computer won't do it for you. I still strongly suggest you not think about it until after completing the first part.

8. not done the first part cause i cant seem to get it right for some reason.

i dont know about the size limitations and didnt know if it was that hard or what it involved.

9. HINT:

Inside your loop, you are going to need something like this:

x = x * 2; // Do this n times in a loop

Don't forget to initialize x before entering the loop.

A type unsigned long int will (probably) hold bigger numbers than a regular int. But, it's not going to hold 2^200 !!!

10. Originally Posted by robatino
Edit: The second problem is much harder - you're trying to implement arbitrary-precision arithmetic.
It's trivially easy if you output the numbers in hexadecimal

11. yeah, still trying to get the first bit done, just not happeneing, taking in all the advice but dont think i am implementing it in correctly because i dont get the right output etc.

12. Originally Posted by Cat
It's trivially easy if you output the numbers in hexadecimal
I don't know. You still have to break the string into blocks and handle the carries properly. Offhand it seems tricky.

13. A power of 2 in binary will only have a single bit set. In hex, you just need to modula 4 to tell if the first hex digit should be 1,2,4, or 8, then divide by 4 to tell how many zeroes should follow it.

E.g. 2^200 in hex is:

0x100000000000000000000000000000000000000000000000 000

That is how I personally would solve the problem (assuming they didn't explictly specify decimal answers) and bank on the grader finding it clever.

14. You're right. That's even easier than solving the first part using decimals - and of course the first part just falls out as a special case (assuming the grader buys it).

Edit: Actually, I'm thinking of binary. Well, if they buy base 16, why not 2?

15. at the moment i am still trying to calculate the power for the first bit.

am i right in using this to help work it out?

Code:
```{
if  (n == 0)      // Base case

return  1;

else  if  (n > 0) // First  general  case

return ( x * Power (x, n - 1));
else```