Fix bug caused by treating term as an expression

Fix #58868

Also allow expression getGCD function to work with numbers.