NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page simple recursive function problem

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 02-25-2008, 11:34 PM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes

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;
}
Thanks
unknowngiver is offline  
Old 02-26-2008, 01:15 AM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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
Peter is offline  
Old 02-26-2008, 02:18 AM THREAD STARTER               #3 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
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..
unknowngiver is offline  
Old 02-26-2008, 01:02 PM   #4 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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:
<?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.
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 02-26-2008, 02:23 PM   #5 (permalink)
NamePros Regular
 
monaco's Avatar
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 689
monaco will become famous soon enough
 



Wouldn't it make more sense to code the app in C and just use cgi to run it for performance reasons?
__________________
My Website | My Blog
monaco is offline  
Old 02-26-2008, 02:53 PM   #6 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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
Peter is offline  
Old 02-26-2008, 03:25 PM   #7 (permalink)
Senior Member
 
RegisterRants's Avatar
Join Date: Oct 2006
Location: NJ
Posts: 1,152
RegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura aboutRegisterRants has a spectacular aura about
 




Isn't there a C function like Math.pow() or Int.pow()?

Jason
__________________
Web Development
RegisterRants is offline  
Old 02-26-2008, 03:42 PM   #8 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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
Peter is offline  
Old 02-26-2008, 03:42 PM   #9 (permalink)
NamePros Regular
 
monaco's Avatar
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 689
monaco will become famous soon enough
 



Yeah, pow(x,y) is defined in math.h
__________________
My Website | My Blog
monaco is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 07:43 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger