2018-09-30 22:01:58 +00:00
---
id: 5900f3b41000cf542c50fec7
title: 'Problem 72: Counting fractions'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302185
2021-01-13 02:31:00 +00:00
dashedName: problem-72-counting-fractions
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2020-02-28 12:39:47 +00:00
2021-06-01 06:50:51 +00:00
Consider the fraction, $\frac{n}{d}$, where `n` and `d` are positive integers. If `n` < `d` and highest common factor, ${HCF}(n, d) = 1$, it is called a reduced proper fraction.
2020-02-28 12:39:47 +00:00
2020-11-27 18:02:05 +00:00
If we list the set of reduced proper fractions for `d` ≤ 8 in ascending order of size, we get:
2020-02-28 12:39:47 +00:00
2021-06-01 06:50:51 +00:00
$$\frac{1}{8}, \frac{1}{7}, \frac{1}{6}, \frac{1}{5}, \frac{1}{4}, \frac{2}{7}, \frac{1}{3}, \frac{3}{8}, \frac{2}{5}, \frac{3}{7}, \frac{1}{2}, \frac{4}{7}, \frac{3}{5}, \frac{5}{8}, \frac{2}{3}, \frac{5}{7}, \frac{3}{4}, \frac{4}{5}, \frac{5}{6}, \frac{6}{7}, \frac{7}{8}$$
2020-02-28 12:39:47 +00:00
2021-06-01 06:50:51 +00:00
It can be seen that there are `21` elements in this set.
2020-02-28 12:39:47 +00:00
2021-06-01 06:50:51 +00:00
How many elements would be contained in the set of reduced proper fractions for `d` ≤ `limit` ?
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-06-01 06:50:51 +00:00
`countingFractions(8)` should return a number.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-01 06:50:51 +00:00
assert(typeof countingFractions(8) === 'number');
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-06-01 06:50:51 +00:00
`countingFractions(8)` should return `21` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-01 06:50:51 +00:00
assert.strictEqual(countingFractions(8), 21);
```
`countingFractions(20000)` should return `121590395` .
```js
assert.strictEqual(countingFractions(20000), 121590395);
```
`countingFractions(500000)` should return `75991039675` .
```js
assert.strictEqual(countingFractions(500000), 75991039675);
```
`countingFractions(1000000)` should return `303963552391` .
```js
assert.strictEqual(countingFractions(1000000), 303963552391);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2021-06-01 06:50:51 +00:00
function countingFractions(limit) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-06-01 06:50:51 +00:00
countingFractions(8);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2021-06-01 06:50:51 +00:00
function countingFractions(limit) {
const phi = {};
let count = 0;
for (let i = 2; i < = limit; i++) {
if (!phi[i]) {
phi[i] = i;
}
if (phi[i] === i) {
for (let j = i; j < = limit; j += i) {
if (!phi[j]) {
phi[j] = j;
}
phi[j] = (phi[j] / i) * (i - 1);
}
}
count += phi[i];
}
return count;
}
2018-09-30 22:01:58 +00:00
```