diff --git a/Project-Euler/Problem069.js b/Project-Euler/Problem069.js new file mode 100644 index 0000000000..4837741f29 --- /dev/null +++ b/Project-Euler/Problem069.js @@ -0,0 +1,24 @@ +/** +// https://projecteuler.net/problem=69 + +Euler's totient function, phi(n) [sometimes called the phi function], is defined +as the number of positive integers not exceeding n which are relatively prime to n. +For example, as 1, 2, 4, 7, 8, are all less than or equal to nine +and relatively prime to nine, phi(9)=6. + +It can be seen that n=6 produces a maximum n/phi(n) for n <= 10 + +Find the value of n <= 1000000 for which n/phi(n) is a maximum. + + */ +export const answer = (maxVal) => { + //To minimize n/phi(n), phi(n) must have the most unique prime factors, n must be equal to the product of primes each to the first power, so it equals to 2*3*5*7*... + var productSoFar = 1; + var primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,57]; + for (var idx=0;idx maxVal){ + return productSoFar; + } + productSoFar *= primes[idx]; + } +} diff --git a/Project-Euler/test/Problem069.test.js b/Project-Euler/test/Problem069.test.js new file mode 100644 index 0000000000..1e7dded415 --- /dev/null +++ b/Project-Euler/test/Problem069.test.js @@ -0,0 +1,12 @@ +import { answer } from '../Problem069.js' + +describe('maximizing n/phi(n)', () => { + // Project Euler Condition Check + test('Maximum value for n <= 1000', () => { + expect(answer(1000)).toBe(210) + }) + // Project Euler Second Value for Condition Check + test('Maximum value for n <= 123456789', () => { + expect(answer(123456789)).toBe(9699690) + }) +})

AltStyle によって変換されたページ (->オリジナル) /