using variable values in mathematical expression pattern restrictions

10 views
Skip to first unread message

merryn....@gmail.com

unread,
Jun 7, 2022, 5:52:50 AM6/7/22
to Numbas Users
How do I add numbas variable values into pattern restrictions?

I want to get students to fully factorise an expression like (8x+12xy), i.e. 4x(2+3y)
To do this I am trying to use pattern restriction, where the pattern needs to have the numerical gcd . x out the front. The gcd is a randomly selected number held in a variable and I cannot figure out how to make this work.

In order to try to figure it out I set up a simpler problem: the answer should be 6 + k, where 6 is held in a number variable {tc}
I tried $n;c + k `where c={tc} and other variations but I haven't succeeded so far in getting the pattern match to work, and would appreciate some help.

Christian Lawson-Perfect

unread,
Jun 7, 2022, 9:02:44 AM6/7/22
to numbas...@googlegroups.com
I don't think you need to use the value of the gcd in the pattern.
If the greatest common factor has been extracted, then the coefficients inside the brackets must be coprime. Use the `where operator to add this condition.

Here's the pattern:

// a number times...
$n`? *
(
  // the constant term:
  //   either a number 'a',
  //   or 'a' is implicitly zero if no constant term is given
  ($n;a `| $z;a:0)
 
  +
 
  // the x term:
  //   either a number 'b' times x,
  //   or x on its own, and 'b' is implicitly 1
  ($n;b * x `| x;b:1)
)

// a and b must be coprime
`where

(gcd(a,b)=1)

--
You received this message because you are subscribed to the Google Groups "Numbas Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numbas-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/numbas-users/84da205b-6153-4e81-be56-c5b0e0b49591n%40googlegroups.com.

Merryn Horrocks

unread,
Jun 7, 2022, 8:28:52 PM6/7/22
to numbas...@googlegroups.com
Ok, many thanks Christian

You received this message because you are subscribed to a topic in the Google Groups "Numbas Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/numbas-users/-HP0U9CtT1k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to numbas-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/numbas-users/CAEMHSOgKHkG483MVZemsVxPfnDEsqSd-F4yN9aTOpr-iwNfeNw%40mail.gmail.com.

merryn....@gmail.com

unread,
Jun 7, 2022, 8:40:44 PM6/7/22
to Numbas Users
Hi Christian,
Thank you for your solution - it works a treat!
However, I have quickly run into another problem, namely, "factorise 6ab + 8bc" where 6 and 8 are randomly chosen, and a, b, and c are randomly chosen letters (so that the questions don't always use the same variables.) Again, the way I am thinking is to use the variable containing b in the pattern match. If so, how do I go about this, or is there a different smart way to do this? 

Thanks again,

Merryn

Reply all
Reply to author
Forward
0 new messages