### factorial n as a wildcard in Calculated question

Number of replies: 8### Re: factorial n as a wildcard in Calculated question

The function factorial is not actually recognized as a valid function to be used in the answer formula

See

http://docs.moodle.org/en/Calculated_question_type#Available_functions

Using more complete mathematical function libraries is on my TO-TO list.

Pierre

### Re: factorial n as a wildcard in Calculated question

How about that "more complete mathematical function library"?

I could also use the factorial function, as well as several other common functions.

I'm using 2.0.2 now (loving it) to teach AP Calculus.

### Re: factorial n as a wildcard in Calculated question

See the plugin extended calculated

http://moodle.org/mod/data/view.php?d=13&rid=1929&filter=1

Pierre

### Re: factorial n as a wildcard in Calculated question

Interesting. Will that work in Moodle 2.0? Is it stable?

### Re: factorial n as a wildcard in Calculated question

Will that work in Moodle 2.0?

As long as the maintainer Dmitry Pupinin create a new version.

Pierre

P.S. However it is on my long term todo list.

### Re: factorial n as a wildcard in Calculated question

Pierre, I suppose you mean:

The function factorial ( n ) is not actually recognized as a valid function to be used in the answer formula etc.

### Re: factorial n as a wildcard in Calculated question

This only shows that in old time (circa 2007) the ( n) was not converted to emoticon .

### Re: factorial n as a wildcard in Calculated question

Hi guys!

In 2011 i found how to include factorial for Moodle 2.0 by hands and stored it:

go to:

**\question\type\calculated\questiontype.php:**

insert function with code:

**public function factorial($n) {**** return ($n <= 1) ? 1 : $n * $this->factorial($n - 1);****}**

find a function **substitute_variables_and_eval** and edit it:

**public function substitute_variables_and_eval($str, $dataset) {**** $formula = $this->substitute_variables($str, $dataset);**** if ($error = qtype_calculated_find_formula_errors($formula)) {**** return $error;**** }**** // Calculate the correct answer**** if (empty($formula)) {**** $str = '';**** } else if ($formula === '*') {**** $str = '*';**** } else {**** //Igor Sazonov hack**** if(strstr($formula, 'factorial')) eval('$str = $this->'.$formula.';');**** else eval('$str = '.$formula.';');**** }**** return $str;**** }**

also edit function **qtype_calculated_find_formula_errors **cases:

...case 'tan': case 'tanh': /*here!*/case 'factorial':

go to **\question\type\calculated\question.php:**

edit **function get_formula_errors**:

...case 'tan': case 'tanh': /*here!*/case 'factorial':

add new function in this file:

**public function factorial($n) {**** return ($n <= 1) ? 1 : $n * $this->factorial($n - 1); **** }**

edit function **calculate_raw:**

**protected function calculate_raw($expression) {**** // This validation trick from http://php.net/manual/en/function.eval.php****/*HERE*/if(!strstr($expression, 'factorial')) {**** if (!@eval('return true; $result = ' . $expression . ';')) {**** throw new moodle_exception('illegalformulasyntax', 'qtype_calculated', '', $expression);**** }**** return eval('return ' . $expression . ';');**** } else return eval('return $this->' . $expression . ';');**** }**

so now you can use factorial function as sin, tan etc

It was a year ago in Moodle 2.0 but maybe it will helps you now =)