[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 02-25-2008, 10:34 PM   #1 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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

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, 12:15 AM   #2 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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, 01:18 AM   #3 (permalink)
Senior Member
 
Join Date: May 2005
Location: Ontario Canada
Posts: 2,928
1,675.13 NP$ (Donate)

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 behold


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, 12:02 PM   #4 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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:-

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, 01:23 PM   #5 (permalink)
NamePros Regular
 
monaco's Avatar
 
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 695
314.80 NP$ (Donate)

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, 01:53 PM   #6 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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, 02:25 PM   #7 (permalink)
Senior Member
 
RegisterRants's Avatar
 
Join Date: Oct 2006
Posts: 1,144
257.37 NP$ (Donate)

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, 02:42 PM   #8 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

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, 02:42 PM   #9 (permalink)
NamePros Regular
 
monaco's Avatar
 
Join Date: Jul 2005
Location: Tucson, AZ
Posts: 695
314.80 NP$ (Donate)

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 06:17 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85