# Fast Exponentiation Algorithm

Page 1 of 1

## 5 Replies - 7184 Views - Last Post: 10 May 2009 - 06:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=104333&amp;s=9dde90c22b1fdb1e2a4c1995ba33a36f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 unrulycomplexity

Reputation: 0
• Posts: 13
• Joined: 09-May 09

# Fast Exponentiation Algorithm

Posted 10 May 2009 - 02:49 AM

Hi everyone, i am a noob in c++ and i have an assignment which requires me to do a program about fast exponentiation algorithm.
```#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{

string power;
string mod;
int ans;
int a;
string d;
//int p;
int b;
int i=23;
char c[23];

cout<<"Enter any Number to the power of any Number mod any Number in Decimal\nExample:123 ^ 456 mod 78\n";
cin>>a>>power>>i>>mod>>b;
d = itoa(i,c,2);
std::istringstream stm;
int p;
stm.str(d);
stm >> p;

for(int m = 0; m<p.length(); m++)
{

if (p = 0)
{
a=a*a;
}
else if (p = 1)
{
a=(a*a)*a;
}
}

ans = a % b;

cout<<"The answer is" << ans <<endl;

system("PAUSE");
return 0;
}

```

this is what i have done but it seems that there an error on the p.length. could someone help me with this? thxs in advance!!

Is This A Good Question/Topic? 0

## Replies To: Fast Exponentiation Algorithm

### #2 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 02:52 AM

Welcome to DIC!

( a ) Does your code compile?
( b ) Any errors or warnings?
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.

-------

You have declared "p" as an int.
You can't use length() on an int.
What were you trying to do there?

---

Also have a look at these
```            if (p = 0)
...
else if (p = 1)

```

Remember "=" and "==" are not the same thing.
Which one did you mean here?

---

This post has been edited by janotte: 10 May 2009 - 03:01 AM

### #3 unrulycomplexity

Reputation: 0
• Posts: 13
• Joined: 09-May 09

## Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 03:03 AM

janotte, on 10 May, 2009 - 01:52 AM, said:

Welcome to DIC!

( a ) Does your code compile?
( b ) Any errors or warnings?
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.

thxs janotte for the welcome!! =)
after compiling my code it showed me this errors
28 `length' has not been declared
28 request for member of non-aggregate type before '(' token

### #4 unrulycomplexity

Reputation: 0
• Posts: 13
• Joined: 09-May 09

## Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 03:42 AM

actually i was trying to convert a number to binary and to do some calculations on it.
an example would be: 15 ^17, i will need to take 17 to convert to binary which would be 10001 and i need to skip the first number and take the second number to calculate. so if the second number is 0 i would need to take 15*15 and if it was a 1 would need to take 15*15*15 therefore i would need to read every number starting from the 2nd till the end.

so if .length() cant be used is there any other way to read the binary number by number?

### #5 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 04:04 AM

I am having trouble guessing at what your plan or design is.

Talk us through, line by line, what each line here is doing and why you want it done.
```    cout<<"Enter any Number to the power of any Number mod any Number in Decimal\nExample:123 ^ 456 mod 78\n";
cin>>a>>power>>i>>mod>>b;
d = itoa(i,c,2);
std::istringstream stm;
int p;
stm.str(d);
stm >> p;

```

### #6 unrulycomplexity

Reputation: 0
• Posts: 13
• Joined: 09-May 09

## Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 06:13 AM

oh its ok janotte i have already figured it out myself. thxs for all the help!! =D