# Th power function

• 04-10-2005
wiz23
Th power function
I want to use the power function to make the digits 1234567 to the sum of squared digits. Eg, 1^2+2^2+3^2+4^2+5^2+6^2+7^2=140

I have done:
#include <cmath>

int b;

b = pow((id/1000000),2) + pow(((id%1000000)/100000),2) + pow(((id%100000)/10000),2) + pow(((id%10000)/1000),2) + pow(((id%1000)/100),2) + pow(((id%100)/10),2) + pow((id%10),2);

• 04-10-2005
TheColonial

May I suggest using a loop and a running total instead?? here's some C++ psuedo to get you going?

Code:

```int number = 1234567; int total = 0; while(number > 0) {   int remainder = number % 10;   total += pow(remainder, 2);   number = number / 10; } cout << total;```
This code hasn't been tested or compiled, but should give you the idea of the structure you're looking for.

Hope that helps.
• 04-10-2005
wiz23
I followed your structure by I get an error say that:

total += pow(remainder, 2); has an "call of overloaded `pow(int&, int)' is ambiguous"

What does this mean?
• 04-10-2005
TheColonial
It means that pow() has more than one overload, and that it doesn't know which of the overloads to call.

IMHO, using pow() in this case is pointless anyway. Why not just times the variable together??

Code:

`total += remainder * remainder;`
• 04-10-2005
Micko
Quote:

Originally Posted by wiz23
I followed your structure by I get an error say that:

total += pow(remainder, 2); has an "call of overloaded `pow(int&, int)' is ambiguous"

What does this mean?

Try casting to double:

pow (static_cast<double>remainder, 2.0)
• 04-10-2005
wiz23
Let me explain what I am doing, I needed to enter 7 digits as 1234567 and get the sum of squared digits so the answer should be 140. I have declare the 7 typed digits as int id; I also included
Code:

```         Quote:                                                                 #include <cmath> I have tried this code     int number = id;     int total = 0;         while(number > 0)     {     int remainder = number % 10;     total += remainder * remainder;     }     cout << total;                                                 ```
but when typed in 1234567, there is nothing to appear. I am stuck.
• 04-12-2005
TheColonial
you missed a line in what i gave you:

Code:

`number = number / 10;`
You'll get an infinite loop otherwise.

Cheers

PS. Many thanks to the person who "disapproved" of my post stating that "it wasn't psuedocode" ;) That's hilarious :) Please would be you be so kind as to point me at the psuedocode specification/standard?