Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-33-digit-cancelling-fractions.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

2.9 KiB

id, challengeType, title, forumTopicId
id challengeType title forumTopicId
5900f38d1000cf542c50fea0 5 Problem 33: Digit cancelling fractions 301987

Description

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.

Instructions

Tests

tests:
  - text: <code>digitCancellingFractions()</code> should return a number.
    testString: assert(typeof digitCancellingFractions() === 'number');
  - text: <code>digitCancellingFractions()</code> should return 100.
    testString: assert.strictEqual(digitCancellingFractions(), 100);

Challenge Seed

function digitCancellingFractions() {
  // Good luck!
  return true;
}

digitCancellingFractions();

Solution

function digitCancellingFractions() {
  function isCurious(numerator, denominator) {
    const fraction = numerator / denominator;
    const numString = numerator.toString();
    const denString = denominator.toString();

    if (numString[1] === '0' && denString[1] === '0') {
      // trivial
      return false;
    }
    for (let i = 0; i < 2; i++) {
      for (let j = 0; j < 2; j++) {
        if (numString[i] === denString[j]) {
          const newNum = parseInt(numString[1 - i], 10);
          const newDen = parseInt(denString[1 - j], 10);
          if (newNum / newDen === fraction) {
            return true;
          }
        }
      }
    }
    return false;
  }
  function findLargestDivisor(a, b) {
    let gcd = a > b ? b : a;
    while (gcd > 1) {
      if (a % gcd === 0 && b % gcd === 0) {
        return gcd;
      }
      gcd--;
    }
    return gcd;
  }

  function simplifyFraction(numerator, denominator) {
    const divisor = findLargestDivisor(numerator, denominator);
    return [numerator / divisor, denominator / divisor];
  }

  let multipleNumerator = 1;
  let multipleDenominator = 1;

  for (let denominator = 11; denominator < 100; denominator++) {
    for (let numerator = 10; numerator < denominator; numerator++) {
      if (isCurious(numerator, denominator)) {
        multipleNumerator *= numerator;
        multipleDenominator *= denominator;
      }
    }
  }

  return simplifyFraction(multipleNumerator, multipleDenominator)[1];
}