2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5900f3b61000cf542c50fec8
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: 'Problem 73: Counting fractions in a range'
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302186
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: problem-73-counting-fractions-in-a-range
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If we list the set of reduced proper fractions for `d`  ≤ 8 in ascending order of size, we get:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$$\frac{1}{8}, \frac{1}{7}, \frac{1}{6}, \frac{1}{5}, \frac{1}{4}, \frac{2}{7}, \frac{1}{3}, \mathbf{\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}$$
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It can be seen that there are `3`  fractions between $\frac{1}{3}$ and $\frac{1}{2}$.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								How many fractions lie between $\frac{1}{3}$ and $\frac{1}{2}$ in the sorted set of reduced proper fractions for `d`  ≤ `limit` ?
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`countingFractionsInARange(8)`  should return a number. 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(typeof countingFractionsInARange(8) === 'number');
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`countingFractionsInARange(8)`  should return `3` . 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.strictEqual(countingFractionsInARange(8), 3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`countingFractionsInARange(1000)`  should return `50695` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(countingFractionsInARange(1000), 50695);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`countingFractionsInARange(6000)`  should return `1823861` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(countingFractionsInARange(6000), 1823861);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`countingFractionsInARange(12000)`  should return `7295372` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(countingFractionsInARange(12000), 7295372);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function countingFractionsInARange(limit) {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								countingFractionsInARange(8);
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function countingFractionsInARange(limit) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let result = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const stack = [[3, 2]];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while (stack.length > 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const [startDenominator, endDenominator] = stack.pop();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const curDenominator = startDenominator + endDenominator;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (curDenominator < = limit) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      result++;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      stack.push([startDenominator, curDenominator]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      stack.push([curDenominator, endDenominator]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return result;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```