0

For a small assignment, I have to write a function which takes in one integer as an input and should return the multipled total of everything before that number. For example;

getTotal(9);

It should do and return the following:
9*8*7*6*5*4*3*2*1 = 3628800

Seems easy enough, right? Well. We've been imposed limitations on the tools we can and can't use. We cannot use while and for loops, we have to only supply one parameter into the function and we can only pass in actual values as parameters, not references. AND we cannot have global variables.

I thought a simple way of doing this would be to have a recursive function, but as I started doing that I found too many limitations to that method and perhaps because of my coding ability I wasn't able to follow it through because two counts will need to be retained:

One decrementing count which is 10, 9, 8, 7 and so on..., then another number which holds the previous multiplied amounts like 10*9 = 90 and as I said, only one parameter is allowed here.

Is there an obvious thing I'm missing here?

Edited by asif49: n/a

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by asif49
0

Brilliant. I think it might be since I'm new to recursive functions but I didn't know you could call a function whilst multiplying it. Thanks for that brilliant solution!

0

but can I just ask that when you do
if ($val == 1) return 1;

where does it return the 1 to?

Edited by asif49: n/a

0

It returns 1 to the last iteration (it multiplies the one before last total by 1) and also serves as an exit condition (does not call itself anymore). It is also there if you want to use it with walue 1 (getTotal(1)) so you don't enter an endless loop.

According to the above link 0! equals 1 so this condition should be also taken into acount. Also to prevent negative numbers you could add another condition. So the code should be at least as follows:

function getTotal($val) {

    if($val < 0) {

        return 'Value should be 0 or greater!';

    } elseif($val == 0 or $val == 1) {

        return 1;

    } else {

        return $val * getTotal($val - 1);
    }
}

echo '<br />TOTAL(9): ' . getTotal(9);

echo '<br />TOTAL(5): ' . getTotal(5);

echo '<br />TOTAL(1): ' . getTotal(1);

echo '<br />TOTAL(0): ' . getTotal(0);

echo '<br />TOTAL(-1): ' . getTotal(-1);

You got me thinking on this one. I am also quite new to the subject and have started diving into it since I need to develop some document structure management functionality where document has a tree structure.

0

No recursion necessary...

<?php

function getTotal( $integer )
{
	return array_product( range( 1, (int) $integer ) );
}

echo getTotal(9); //362880
0

Ah I understand now. And in this case, recursion is necessary since I am not allowed to use any functions like array_product or range otherwise I would of course have considered it and many similar much easier solutions. The first solution did the trick though, and second one added to it so thanks all =D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.