Formulas: Using gcd()

Formulas: Using gcd()

by Matthias Giger -
Number of replies: 34
Picture of Particularly helpful Moodlers

In the question type Formulas (plugin) how can I make use of functions like gcd()?

I tried the following:

a = {1:12:1};

b = {1:12:1};

c=gcd(a,b);

But it didn't work. I get an error: Try evalution error! 1: The function gcd is not supported

I'm using version 4.3 for Moodle 2.6+ (2016012400) in Moodle 3.0.4 (Build: 20160509).

Strangely enough, functions like fact() or max() work.

I'm asking because I want to build a question bank around fraction problems.

Average of ratings: -
In reply to Matthias Giger

Re: Formulas: Using gcd()

by Bernat Martinez -

Hi Mathhias,

I think I've just solved this bug. You only have to add these parameters in these files.

moodle/question/type/formulas/script/formatcheck.php
  • line 38 -39: var funclist = {'sin': 1, 'cos': 1, 'tan': 1, 'asin': 1, 'acos': 1, 'atan': 1, 'exp': 1,
            'log10': 1, 'ln': 1, 'sqrt': 1, 'abs': 1, 'ceil': 1, 'floor': 1, 'fact':1 , 'lcm': 2, 'gcd' : 2};

moodle/question/type/formulas/variables.php
  • line 1312: case 'atan2': case 'fmod': case 'pow': case 'ncr': case 'npr': case 'lcm': case 'gcd': 


Finally, purge all caches (Site administration -> Development -> Purge all caches)

Average of ratings: Useful (2)
In reply to Bernat Martinez

Re: Formulas: Using gcd()

by Matthias Giger -
Picture of Particularly helpful Moodlers

Dear Bernat


I just made the changes in my installation ... and it WORKS!

Just one small thing: The first file you have to change is not formatcheck.php it is formatcheck.js.


Having the gcd() function available makes things much easier. Until know I used a dataset to go around the fraction problem.


Thank you very much.

Matthias

Average of ratings: Useful (1)
In reply to Bernat Martinez

Re: Formulas: Using gcd()

by Godfrey Sovis -

Hi Sir Bernat,

Can you please check the accuracy of the algorithm of the code from line 65 to 71 in variables.php file?

function gcd($a,$b) {

    if($a < 0)         $a=0-$a;

    if($b < 0 )        $b=0-$b;

    if($a == 0 || $b == 0)    return 1;

    if($a == $b)              return a;                                          # Should this return 1 instead of a


    do{

        $rest = (int) $a % $b;

        $a=$b;

        $b=$rest;

    } while($rest >0);

return $a;


Thanks


Sovis

In reply to Godfrey Sovis

Re: Formulas: Using gcd()

by Godfrey Sovis -

Please ignore the above post. I mis-understood. Kindly remove this post.

Very sorry for the mistake!

Regards

Godfrey Sovis


In reply to Godfrey Sovis

Re: Formulas: Using gcd()

by Bernat Martinez -

Yes the algorithm is right, gcd(6,6) = 6

In reply to Bernat Martinez

Re: Formulas: Using gcd()

by Bernat Martinez -

I'm very sorry, Sovis. Now I understand your "old" question. 

You are right, there was a BIG  error in the algorithm, 

 if($a == $b)              return $a (not a)

Average of ratings: Useful (1)
In reply to Bernat Martinez

Re: Formulas: Using gcd()

by Godfrey Sovis -
Dear Bernat,

Thanks so much! I changed it and now it works fine!

Is there any way to select a pair of numbers randomly from three or four different lists?

I designed four questions; one with factors of 24, then factors of 48, 56, and 72.

(Please check my earlier post about these sets)

Now I want to put all the sets together and design one more question.

Still I could not figure a out how to do it?

Do you have any suggestion?

I will post all these questions, so that you could add them to your question sets. 

I will explain my approach in the main question text , so that teachers could understand how I did them.

Regards.

Godfrey Sovis

In reply to Godfrey Sovis

Re: Formulas: Using gcd()

by Bernat Martinez -

I will try to think of your algorithm, in the meanwhile I show you how we build fractions. 

It is done less systematically that yours, but it works. 

Each formula question has 4 fraction exercises, first one being the easiest one



In reply to Bernat Martinez

Re: Formulas: Sample questions on fractions

by Godfrey Sovis -

I have attached 6 questions on fractions for primary & lower secondary level. Bernat Martinez can add them to his collections if they are suitable. I have used 3 different logics to design them. 

In reply to Godfrey Sovis

Re: Formulas: Sample questions on fractions

by Bernat Martinez -

Thanks Sovis,

 I have added 2  template questions to the FRACTIONS  quizz .  They are very elaborated

To visit them use teacher/teacher

In reply to Godfrey Sovis

Re: Formulas: Sample questions on fractions

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Godfrey, thanks so much for the questions.

In reply to AL Rachels

Re: Formulas: Sample questions on fractions

by Godfrey Sovis -

The question 2 you have added to the quiz has some weaknesses. I am very sorry for that. I modified it and it is attached here.

I used the 'sort' function. This is the first time I am using it and it works. Do you think it will work smoothly?


In reply to Godfrey Sovis

Re: Formulas: Sample questions on fractions

by Bernat Martinez -

I have changed the question 

http://moodle.compasspro.eu/mod/quiz/attempt.php?attempt=619&page=1

It works fine. As far as I understand your aim is: 

- both fractions and result  to be proper fractions

- denominators to be different 

- there is some simplification work to be done

In reply to Bernat Martinez

Re: Formulas: Sample questions on fractions

by Godfrey Sovis -

Yes, my aim is to design questions on fractions to cater junior secondary and upper primary maths curriculum. We start with operations on proper fractions where simplifications are involved ending with an answer which is also either a proper fraction or a mixed number. It is important to increase the difficulty level gradually.

I have attached 3 questions using mixed numbers. Please check whether there are any errors.

In reply to Godfrey Sovis

Re: Formulas: Sample questions on fractions

by Bernat Martinez -

Each time they are more elaborated, they work fine

In reply to Bernat Martinez

Re: Formulas: Sample questions on Simple equations

by Godfrey Sovis -

Hi Bernat, First of all let me wish you Merry Christmas!

I want to design a question on Simple equations.

The question should pick one at random from a list. I used pick() function. It does not work as I expected.

I have attached the question here. Kindly have a look when you are free. Thanks.

Godfrey Sovis

In reply to Godfrey Sovis

Re: Formulas: Sample questions on Simple equations

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Godfrey,

Thanks for the sample. I think I have it working. The overall set of square brackets are not needed in qn and an.

In reply to AL Rachels

Re: Formulas: Sample questions on Simple equations

by Godfrey Sovis -

Oh! Dear AL Rachels, Thank you so much! I tries to design this question using variables. It gave me a hard time. So, I used this method, even though it is not very good. It rotated only through two questions. I never thought these outer square brackets were creating this problem. Thank you again and I wish you a Joyful, blessed Christmas.

In reply to Godfrey Sovis

Re: Formulas: Sample questions on Simple equations

by Bernat Martinez -

Hi Sovis, 

I think Al is correct, if you use brackets makes it a list so there is one element only to pick. 

In addition you have some unnecessary things there, for example placeholder is not needed,....

I have simplified and corrected your question, see attach

Please let me know if you  need any clarification

I wish you Merrry Christmas

In reply to Bernat Martinez

Re: Formulas: Sample questions on Simple equations

by Godfrey Sovis -

Dear Bernat, Thanks so much! You made it still more simple! Do you have any idea of using variables to design a question like this? But, think about it after Christmas! Merry Christmas!

In reply to Godfrey Sovis

Re: Formulas: Sample questions on Simple equations

by Bernat Martinez -

Sovis, I have created a linear equation question  with code description at Solving EQUATIONS with FORMULAS question  (teacher/teacher).

In addition we have updated to Moodle 3.2 with the BOOST theme, so we can confirm FORMULAS is working fine at M3.2 and that the new theme is superb.

BTW, I have used your "chk" trick in order no to get indefined solutions, thanks.

In reply to AL Rachels

Re: Formulas: Sample questions on fractions

by Godfrey Sovis -

Hi AL Rachels,

I had problems using the shuffle function. Do you think it works with a list of numbers? Do you have any sample of using that function?

In reply to Godfrey Sovis

Re: Formulas: Sample questions on fractions

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Godfrey,

Sorry, I got in a rush and misread the documentation. Shuffle seems to work only for a simple list of numbers such as S=shuffle([1,2,3,4,5,6]) but does NOT work for pairs of them, S=shuffle([2,22], [3,21], [4,20], [6,18]) like I thought it would.

In reply to Godfrey Sovis

Re: Formulas: Using gcd()

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Hi Godfrey,

Couldn't you use "shuffle" for each of the four lists? It's my understanding that each variable, c1, c2, c3, and c4 will each wind up instantiated with one permutation of the input list.

c1 = shuffle([2,22], [3,21], [4,20], [6,18], [8,16], [9,15], [10,14], [12,12]);

c2 = shuffle([...list 2

c3 = shuffle([...list 3

c4 = shuffle([...list 4
In reply to Bernat Martinez

Re: Formulas: Using gcd()

by Godfrey Sovis -

Hi Sir Bernat,

I want to design some questions on addition and subtraction of Fractions using only "Proper Fractions" exclusively for Primary level. I designed one sample which I have attached below. I am not very happy with it. There must be a better way of doing this. Do you have any suggestion?

Thanks and regards

Godfrey Sovis

In reply to Godfrey Sovis

Re: Formulas: Using gcd()

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Godfrey,

My first inclination was to try using your question with the fractions rendered with the MathJax or Tex filters. However, neither filter works 100% reliably in the formula type question. The fraction part renders correctly only once in a while, but the answer inputs {_0} and {_1} always render as o and 1.

Eventually I realized you said these are to be questions for Primary level, by which I hope you mean young kids, I tried just putting the parts of the Part's text into a five column table with slightly increased fonts size so it typically will look like this:

Editing view:


Preview after answering:


In reply to AL Rachels

Re: Formulas: Using gcd()

by Godfrey Sovis -

Hi  AL Rachels,

Thanks for formatting the question and the answer space very nicely. My main problem is the way I generated the question. It does not generate a wide variety of fractions that give answers as "Proper Fractions". The reason is that my logic of designing the question is not very good! I am looking for a better algorithm of designing a wide variety of proper fractions which add up to another proper fraction. 

I am going to try this method:

Numbers 24, 36, 48, (56), 64, and 72 have a lot of factors.

Take 24:

First take pairs that add up to 24:

[2, 22], [3,21], [4,20], [6,18], [8,16], [9.15], 10,14], [12,12]   

Take the first element (2) in [2,22] 

Now make pairs with the second elements of the rest of pairs.

[2,21], [2,20], [2,18], [2,16], [2,15], [2,14], [2,12]

Take the first pair [2,21] and generate the two fractions using them: 2/24 + 21/24 and simplify them to generate the question.

1/12 + 7/8. The answer will be always less than 1, hence, it will be a proper fraction. This way, we can generate more than 50 different questions only with the pairs of 24.

Likewise, we can take 36, 48, etc and generate fractions. This method will generate a large number of combination of fractions.

What do you think about this method? 

How ever, designing the question with all these conditions will be a bit challenging! If I succeed, I will post it to the forum. May be, it is better to design separate sets of questions with each set of numbers.


In reply to Matthias Giger

Re: Formulas: Problem with lcm()

by Godfrey Sovis -

I am getting a problem with lcm().

In my question, when there is a fraction with the same denominator lcm() function returns 0.

How can I resolve this?

I have attached a sample question on proper fractions.

When there are equal denominators, I had to change them for the moment.

All the variables and their assignments are explained in the Question text.

Thanks


In reply to Godfrey Sovis

Re: Formulas: Problem with lcm()

by Bernat Martinez -

Dear Sovis, you are doing a great job in finding an algorithm for creating fractions exercises. 

I have tested your question, it works fine,   sometimes it shows 0 in numerator, it is made on purpose?

In reply to Godfrey Sovis

Re: Formulas: Problem with lcm()

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Godfrey,

Could you use the for loop to make the question keep selecting a new d12 = d2/gcd(n2,24); until d11-d12 is NOT zero?

In reply to AL Rachels

Re: Formulas: Problem with lcm()

by Godfrey Sovis -

Hi AL Rachels,

It is a good idea!  What I did was to add 2 to one denominator. Now I will try your suggestion.

Thanks so much.

In reply to Godfrey Sovis

Re: Formulas: Problem with lcm()

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Godfrey,

It will be nice if it works. While ignoring advertisements during tv watching last night, I looked again at the example documentation for the formula question type and I'm not sure if the for loop has enough flexibility to do what I suggested.

Your solution seems to be nice and simple.

By the way, two times I have run into a situation where you cannot put in a correct answer. I could never get it to accept an answer of inf. Haven't had the chance to try all caps, INF yet.


In reply to AL Rachels

Re: Formulas: Problem with lcm()

by Godfrey Sovis -

I think it is due to the problem with lcm().

Did you change the coding in "..moodle/question/type/formulas/variables.php"

function gcd($a,$b) {

    if($a < 0)         $a=0-$a;

    if($b < 0 )        $b=0-$b;

    if($a == 0 || $b == 0)    return 1;

    if($a == $b)              return a;                                          # Bernat corrected it to be "return $a" not  return a

    do{

        $rest = (int) $a % $b;

        $a=$b;

        $b=$rest;

    } while($rest >0);

return $a;

It was returning 0. So, you will get INF error.


In reply to Godfrey Sovis

Re: Formulas: Problem with lcm()

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Godfrey,

Good catch. I read that one, got interrupted, then never went back and actually made the change.