Calculating exponents without library functions

D

David

How can you write a program in C++ by using loop to control the calculation
without the use of library function pwr .For example 3 to the power of 4 (ie
3x3x3x3). Any help will be appreciated
 
M

Mike Wahler

David said:
How can you write a program in C++ by using loop to control the calculation
without the use of library function pwr .For example 3 to the power of 4 (ie
3x3x3x3). Any help will be appreciated

#include <iostream>

unsigned int pwr(unsigned int base, unsigned int exp)
{
unsigned int result = 1;

while(exp--)
result *= base;

return result;
}

int main()
{
std::cout << pwr(3, 4) << '\n';
return 0;
}


-Mike
 
D

David

Thanks a lot Mike for the solution

I am trying to do the the calculation without the use of built in pwr
function.I have to use the for or while loop with probably the counter

Thanks once again



David
 
M

Mike Wahler

David said:
Thanks a lot Mike for the solution

I am trying to do the the calculation without the use of built in pwr
function.

There is no built-in function called 'pwr()' (however there
is one called 'pow()' declared by <cmath>

'pwr()' is the name of the function I wrote in my post.

I have to use the for or while loop with probably the counter

The function I posted uses a while loop with a counter.
(the 'counter' is the exponent parameter).

BTW please don't top post.

-Mike
 
M

Matt

David said:
How can you write a program in C++ by using loop to control the calculation
without the use of library function pwr .For example 3 to the power of 4 (ie
3x3x3x3). Any help will be appreciated

Try posting to comp.lang.c++.homework.
 
M

Mabden

Matt said:
Try posting to comp.lang.c++.homework.

I would use a loop to control the calculation. For example 3 to the
power of 4 is the same as 3 x 3 x 3 x 3.


Do I get part of the A?
 
B

Bruce Clement

Matt said:
Try posting to comp.lang.c++.homework.

Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.


/***************************************************************************
main.cpp - description
-------------------
begin : Thu Apr 15 19:35:27 NZDT 2004
copyright : (C) 2004 by Bruce Clement
email : (e-mail address removed)

***************************************************************************/

/***************************************************************************
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any
* later version.
*
**********************************************************************/

#include <iostream>

double power( double x, int y )
{
double rslt = 1;
for( int i = y; 0 < i--; )
rslt = x * power( x, y - 1 );
return rslt;
}


int main(int argc, char *argv[])
{
std::cout << "Zap, Pow, Kapow ... 3^4 = " << power( 3.0, 4 ) <<
std::endl;

return 0;
}
 
R

Richard Herring

Bruce Clement said:
Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.


/***************************************************************************
main.cpp - description
-------------------
begin : Thu Apr 15 19:35:27 NZDT 2004
copyright : (C) 2004 by Bruce Clement
email : (e-mail address removed)
************************************************************************
***/

/***************************************************************************
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any
* later version.
*
**********************************************************************/

#include <iostream>

double power( double x, int y )
{
double rslt = 1;
for( int i = y; 0 < i--; )
rslt = x * power( x, y - 1 );

Couldn't you use a recursive function here instead of operator* ?
return rslt;
}


int main(int argc, char *argv[])
{
std::cout << "Zap, Pow, Kapow ... 3^4 = " << power( 3.0, 4 ) <<
std::endl;

return 0;
}
 
B

Bruce Clement

Richard said:
Bruce Clement said:
Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.
[...]

Couldn't you use a recursive function here instead of operator* ?
[...]

LOL, You're evil. I like that in a person. :)
 
R

rossum

Richard said:
Bruce Clement said:
Matt wrote:

David wrote:

How can you write a program in C++ by using loop to control the
calculation
without the use of library function pwr .For example 3 to the power
of 4 (ie
3x3x3x3). Any help will be appreciated

Try posting to comp.lang.c++.homework.


Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.
[...]

Couldn't you use a recursive function here instead of operator* ?
[...]

LOL, You're evil. I like that in a person. :)


Real programmers do it with the Peano Axioms

rossum


#include <iostream>
#include <cstdlib>

//-------------------------------------

int successor(int num)
{
int result;
result = ++num;
return result;
} // end successor()

//-------------------------------------

int add(int a, int b)
{
int result;
if (b == 0)
result = a;
else
result = successor(add(a, b - 1));
return result;
} // end add()

//-------------------------------------

int multiply(int a, int b)
{
int result;
if (b == 0)
result = 0;
else
result = add(a, multiply(a, b - 1));
return result;
} // end multiply()

//-------------------------------------

int power(int x, int y)
{
int result = 1;
// An interesting variation, thanks Bruce.
for (int i = y; 0 < i--; )
result = multiply(x, power(x, y - 1));
return result;
} // end power()

//-------------------------------------

int main(int argc, char **argv)
{
std::cout << "Three raised to the power four is "
<< power(3, 4)
<< std::endl;
return EXIT_SUCCESS;
} // end main()
 
R

Richard Herring

rossum said:
Richard said:
In message <[email protected]>, Bruce Clement

Matt wrote:

David wrote:

How can you write a program in C++ by using loop to control the
calculation
without the use of library function pwr .For example 3 to the power
of 4 (ie
3x3x3x3). Any help will be appreciated

Try posting to comp.lang.c++.homework.


Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.

[...]

Couldn't you use a recursive function here instead of operator* ?
[...]

LOL, You're evil. I like that in a person. :)


Real programmers do it with the Peano Axioms

[snip program]

Indeed. Now do it for _double_ arguments ;-)
 
V

velthuijsen

Couldn't you use a recursive function here instead of operator* ?
[...]

LOL, You're evil. I like that in a person. :)


Real programmers do it with the Peano Axioms

rossum

Nice program :)
But seeing that it takes:
65 calls to power
376 calls to multiply
6036 calls to add
5724 calls to Successor
do you have a version with less redundancy in it while preserving the
I'm the processor structure?
 
R

rossum

Couldn't you use a recursive function here instead of operator* ?

[...]

LOL, You're evil. I like that in a person. :)


Real programmers do it with the Peano Axioms

rossum

Nice program :)
But seeing that it takes:
65 calls to power
376 calls to multiply
6036 calls to add
5724 calls to Successor
do you have a version with less redundancy in it while preserving the
I'm the processor structure?

That version was designed to take recursion towards the extreme, it
was not designed for efficiency, or for doing the OP's homework.

If you want less redundancy then:
1 Remove Bruce's redundant loop from power()
2 Change all the single recursions into for loops

Thanks for the call counts, just think yourself lucky that I didn't
reproduce Bruce's redundant loop in all the other functions as well.

rossum
 
R

rossum

rossum said:
Richard Herring wrote:
In message <[email protected]>, Bruce Clement

Matt wrote:

David wrote:

How can you write a program in C++ by using loop to control the
calculation
without the use of library function pwr .For example 3 to the power
of 4 (ie
3x3x3x3). Any help will be appreciated

Try posting to comp.lang.c++.homework.


Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.


[...]

Couldn't you use a recursive function here instead of operator* ?

[...]

LOL, You're evil. I like that in a person. :)


Real programmers do it with the Peano Axioms

[snip program]

Indeed. Now do it for _double_ arguments ;-)

Only if you can give me an equivalent of the Peano Axioms (which only
cover integers) for real numbers. The problem is how to
mathematically define "successor" in the real number domain.

rossum
 
R

Richard Herring

rossum said:
rossum said:
On Fri, 16 Apr 2004 09:34:20 +1200, Bruce Clement

Richard Herring wrote:
In message <[email protected]>, Bruce Clement

Matt wrote:

David wrote:

How can you write a program in C++ by using loop to control the
calculation
without the use of library function pwr .For example 3 to the power
of 4 (ie
3x3x3x3). Any help will be appreciated

Try posting to comp.lang.c++.homework.


Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.


[...]

Couldn't you use a recursive function here instead of operator* ?

[...]

LOL, You're evil. I like that in a person. :)


Real programmers do it with the Peano Axioms

[snip program]

Indeed. Now do it for _double_ arguments ;-)

Only if you can give me an equivalent of the Peano Axioms (which only
cover integers) for real numbers. The problem is how to
mathematically define "successor" in the real number domain.
<shudder> Don't fall into that trap; what "double" denotes is nothing
like a real. It's merely a subset of the rationals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top