-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Open
Project euler69 #1510
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7c4c42c
Create Problem069.js
rtang09 13bc93d
Update Problem069.js
rtang09 da0dc18
Update Problem069.js
rtang09 d571050
Update Problem069.js
rtang09 23f34a3
Create Problem069.test.js
rtang09 02a1341
Update Problem069.js
rtang09 9f2c6c3
Update Problem069.js
rtang09 b7240ae
Update Problem069.test.js
rtang09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.