2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5900f3b11000cf542c50fec4
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: 'Problem 69: Totient maximum'
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302181
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: problem-69-totient-maximum
							 
						 
					
						
							
								
									
										
										
										
											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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Euler's Totient function, ${\phi}(n)$ (sometimes called the phi function), is used to determine the number of numbers less than `n`  which are relatively prime to `n` . For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, ${\phi}(9) = 6$.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< div  style = 'margin-left: 4em;' >  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								| $n$ | $\text{Relatively Prime}$ | $\displaystyle{\phi}(n)$ | $\displaystyle\frac{n}{{\phi}(n)}$ |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| --- | ------------------------- | ------------------------ | ---------------------------------- |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 2   | 1                         | 1                        | 2                                  |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 3   | 1,2                       | 2                        | 1.5                                |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 4   | 1,3                       | 2                        | 2                                  |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 5   | 1,2,3,4                   | 4                        | 1.25                               |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 6   | 1,5                       | 2                        | 3                                  |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 7   | 1,2,3,4,5,6               | 6                        | 1.1666...                          |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 8   | 1,3,5,7                   | 4                        | 2                                  |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 9   | 1,2,4,5,7,8               | 6                        | 1.5                                |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| 10  | 1,3,7,9                   | 4                        | 2.5                                |
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It can be seen that `n`  = 6 produces a maximum $\displaystyle\frac{n}{{\phi}(n)}$ for `n`  ≤ 10.
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Find the value of `n`  ≤ `limit`  for which $\displaystyle\frac{n}{{\phi(n)}}$ is a maximum.
							 
						 
					
						
							
								
									
										
										
										
											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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`totientMaximum(10)`  should return a number. 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(typeof totientMaximum(10) === 'number');
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`totientMaximum(10)`  should return `6` . 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.strictEqual(totientMaximum(10), 6);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`totientMaximum(10000)`  should return `2310` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(totientMaximum(10000), 2310);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`totientMaximum(500000)`  should return `30030` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(totientMaximum(500000), 30030);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`totientMaximum(1000000)`  should return `510510` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(totientMaximum(1000000), 510510);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function totientMaximum(limit) {
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								totientMaximum(10);
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2020-08-13 17:24:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2021-07-15 13:04:11 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function totientMaximum(limit) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  function getSievePrimes(max) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const primesMap = new Array(max).fill(true);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    primesMap[0] = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    primesMap[1] = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const primes = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (let i = 2; i <  max ;  i  =  i  +  2 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (primesMap[i]) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        primes.push(i);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        for (let j = i * i; j <  max ;  j  =  j  +  i )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          primesMap[j] = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (i === 2) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        i = 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return primes;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const MAX_PRIME = 50;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const primes = getSievePrimes(MAX_PRIME);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let result = 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  for (let i = 0; result * primes[i] <  limit ;  i + + )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    result *= primes[i];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return result;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```