| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: May 2005 Location: Ontario Canada
Posts: 3,088
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | simple recursive function problem 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 ![]() ????: NamePros.com http://www.namepros.com/programming/437171-simple-recursive-function-problem.html 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;
}
__________________ |
| |
| | #2 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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).
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | THREAD STARTER #3 (permalink) |
| Senior Member Join Date: May 2005 Location: Ontario Canada
Posts: 3,088
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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..
__________________ |
| |
| | #4 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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:- ????: NamePros.com http://www.namepros.com/showthread.php?t=437171 ????: NamePros.com http://www.namepros.com/showthread.php?t=437171 PHP Code:
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | #6 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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)
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |
| | #7 (permalink) |
| Senior Member Join Date: Oct 2006 Location: NJ
Posts: 1,152
![]() ![]() ![]() | Isn't there a C function like Math.pow() or Int.pow()? Jason
__________________ Web Development |
| |
| | #8 (permalink) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | yes there is but he has been asked to do it without using any c built in functions.
__________________ Manage your portfolio using my new Domain Portfolio Management script. Securing Your Domain Name From Theft |
| |