Simplified fraction pattern

Does anyone have a pattern to check for a simplified fraction? Needs to be OK with integers too. Checking before I noodle around trying to build one.

p=patterns

frac=p.fraction(p.number,p.number).parse(this.latex)
num=frac.numerator.numericValue
den=frac.denominator.numericValue

check= p.integer.matches(this.latex) or (p.fraction(p.number,p.number).matches(this.latex) and numericValue(`\gcf(${num},${den})`)=1)

suffix: when not(this.submitted) "" when check "βœ…" otherwise "❌"
2 Likes

I like how terse this is

Awesome. :saluting_face:
What about including a simplified mixed number too? :heart_eyes:

Brilliant, Daniel!

As usual

UPDATED:

p=patterns

frac=p.fraction(p.number,p.number).parse(this.latex)
num=frac.numerator.numericValue
den=frac.denominator.numericValue
fracCheck=p.fraction(p.number,p.number).matches(this.latex) and numericValue(`\gcf(${num},${den})`)=1

mf=p.mixedNumber.parse(this.latex)
whole=mf.whole
mNum=mf.numerator
mDen=mf.denominator
mixCheck=numericValue(`\floor(${whole})`)=whole and numericValue(`\gcf(${mNum},${mDen})`)=1 and p.mixedNumber.matches(this.latex)

check= (p.integer.matches(this.latex) and not(p.mixedNumber.matches(this.latex))) or fracCheck or mixCheck

suffix: when not(this.submitted) "" when check "βœ…" otherwise "❌"

Note: There was some weirdness where an integer written in mixed number form (e.g. 3 3/3)was accepted as an integer, so had to add for the integer form that it was not a mixed number. (@Jay ?) I assume it’s using some calculation like I did for the whole number portion of mixCheck.

1 Like