website

source code for the ttmaths.uk website
Log | Files | Refs | README

mathsUtils.js (434B)


      1 export function getHCF(a, b) {
      2   if (b == 0) return a;
      3   if (a == 1 && b == 1) return 1;
      4   const hcf = getHCF(b, a % b);
      5   if (a * b < 0) return Math.abs(hcf);
      6   return hcf;
      7 }
      8 
      9 export function simplifyFraction(num, denom) {
     10   const hcf = getHCF(num, denom);
     11   return [num / hcf, denom / hcf];
     12 }
     13 
     14 export function trueMod(a, b) {
     15   // a mod b, but with expected behaviour for negatives.
     16   return a >= 0 ? a % b : b - ((-a % b) || b);
     17 }