← Back to challenges

Proper and Improper Fractions

JavaScriptHardlogicmathnumberslanguage_fundamentals

Instructions

Mubashir was reading about Proper and Improper Fractions on Wikipedia. He concluded that if n is the numerator and d is the denominator of a given fraction, the fraction can be called as Proper Fraction if and only if GCD(n,d)==1.

For example 5/16 is a proper fraction, while 6/16 is an improper fraction, as both 6 and 16 are divisible by 2, thus the fraction can be reduced to 3/8.

Create a function that takes a denominator number d and returns the total number of proper fractions which can be built using d as a denominator.

See below example for given denominator d = 15:

properFractions(15) ➞ 8

// A total of 8 different proper fractions are possible between 0 and 1
// 1/15, 2/15, 4/15, 7/15, 8/15, 11/15, 13/15 and 14/15.

Examples

properFractions(1) ➞ 0

properFractions(2) ➞ 1

properFractions(5) ➞ 4

properFractions(25) ➞ 20

Notes

You can expect big numbers.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.