NameSilo

Simple recursive function problem

Spaceship Spaceship
Watch
Impact
19
Hey
I am trying to figure out how to do this
I need to make a recursive function which does
b^e = b*b^e-1

so far..i have a function that will just compute b^e

This is in C by the way...but do it in PHP or any other language...i just need to know how to do this..i can translate it into C :)

heres the code for just b^e

Code:
#include<stdio.h>
int intPow(int b,int e); /*Function prototype*/
int main(){
        int b,e;
        printf("Please enter two integers: ");
        scanf("%d%d",&b,&e);
        printf("%d to the power of %d is %d\n",b,e,intPow(b,e));
}
int intPow(int b,int e){
        int power = 1;
        for(int i=1;i<=e;i++){
                power *= b;
        }
        return power;
}

Thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
What are you actually trying to do here? Are you trying to find the values of b and e programmatically?

If I remember my maths correctly (and I am possibly wrong due to precedence etc) b is actually 1 (and I suspect e might be).
 
0
•••
no
b and e are two integers that a user will input
so for example..if the user inputs
b = 2
e = 4

the program will do 2^4

but i have to do it without using the "power" function already provided in the C library...and i have to use recursive function in a way:
2^4 = 2*2^3

in the program above..it does 2^4 ...i have to break it and call that program again from the inside [recursive]
but the problem here is that i dont know what the stop condition should be, since i only break it once..
 
0
•••
so basically you need to do a recursive function that that in your instance will do 2*2*2*2. If that is the case it would be something like the following in PHP:-

PHP:
<?php
$a = 2;
$b = 3;

function power($number, $power, $original=null)
{
	if (is_null($original))
	{
		$original = $number;
	}
	if ($power>1)
	{
		$number = $original * $number;
		$output = power($number, $power-1, $original);
	}
	else 
	{
		$output = $number;
	}
	return $output;
}
echo power($a, $b);
?>

I'm sure you could translate that into c no problem at all as it will be extremely similar. If this is not quite what you wanted let me know but it pretty much seems to be.
 
0
•••
Wouldn't it make more sense to code the app in C and just use cgi to run it for performance reasons?
 
0
•••
I gave him a php version so that he could translate it into c. If you look through his posts he does suggest that. Having working code in another language makes it easier to translate it into another language as the concepts are the same (in fact in this case a lot of the syntax and constructs will be the same as well)
 
0
•••
Isn't there a C function like Math.pow() or Int.pow()?

Jason
 
0
•••
yes there is but he has been asked to do it without using any c built in functions.
 
0
•••
Yeah, pow(x,y) is defined in math.h
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back