Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Project euler69 #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rtang09 wants to merge 8 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from rtang09:ProjectEuler69
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Project-Euler/Problem069.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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<primes.length;idx++){
if (productSoFar*primes[idx] > maxVal){
return productSoFar;
}
productSoFar *= primes[idx];
}
}
12 changes: 12 additions & 0 deletions Project-Euler/test/Problem069.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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 によって変換されたページ (->オリジナル) /