-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
solution: Project Euler Problem 28 #1198
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,53 @@ | ||
/** | ||
* Problem 28 - Number spiral diagonals | ||
* | ||
* @see {@link https://projecteuler.net/problem=28} | ||
* | ||
* Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: | ||
* | ||
* 21 22 23 24 25 | ||
* 20 07 08 09 10 | ||
* 19 06 01 02 11 | ||
* 18 05 04 03 12 | ||
* 17 16 15 14 13 | ||
* | ||
* It can be verified that the sum of the numbers on the diagonals is 101. | ||
* What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? | ||
* | ||
* @author ddaniel27 | ||
*/ | ||
|
||
function problem28 (dim) { | ||
if (dim % 2 === 0) { | ||
throw new Error('Dimension must be odd') | ||
} | ||
if (dim < 1) { | ||
throw new Error('Dimension must be positive') | ||
} | ||
|
||
let result = 1 | ||
for (let i = 3; i <= dim; i += 2) { | ||
/** | ||
* Adding more dimensions to the matrix, we will find at the top-right corner the follow sequence: | ||
* 01, 09, 25, 49, 81, 121, 169, ... | ||
* So this can be expressed as: | ||
* i^2, where i is all odd numbers | ||
* | ||
* Also, we can know which numbers are in each corner dimension | ||
* Just develop the sequence counter clockwise from top-right corner like this: | ||
* First corner: i^2 | ||
* Second corner: i^2 - (i - 1) | The "i - 1" is the distance between corners in each dimension | ||
* Third corner: i^2 - 2 * (i - 1) | ||
* Fourth corner: i^2 - 3 * (i - 1) | ||
* | ||
* Doing the sum of each corner and simplifing, we found that the result for each dimension is: | ||
* sumDim = 4 * i^2 + 6 * (1 - i) | ||
* | ||
* In this case I skip the 1x1 dim matrix because is trivial, that's why I start in a 3x3 matrix | ||
*/ | ||
result += (4 * i * i) + 6 * (1 - i) // Calculate sum of each dimension corner | ||
} | ||
return result | ||
} | ||
|
||
export { problem28 } |
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,17 @@ | ||
import { problem28 } from '../Problem028.js' | ||
|
||
describe('checking number spiral diagonals', () => { | ||
it('should be invalid input if number is negative', () => { | ||
expect(() => problem28(-3)).toThrowError('Dimension must be positive') | ||
}) | ||
it('should be invalid input if number is not odd', () => { | ||
expect(() => problem28(4)).toThrowError('Dimension must be odd') | ||
}) | ||
test('if the number is equal to 5 result should be 101', () => { | ||
expect(problem28(5)).toBe(101) | ||
}) | ||
// Project Euler Condition Check | ||
test('if the number is equal to 1001 result should be 669171001', () => { | ||
expect(problem28(1001)).toBe(669171001) | ||
}) | ||
}) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a more helpful comment explaining how you arrived at this formula.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explanation added