mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-12 07:30:30 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@ -17,7 +17,7 @@
|
||||
* @author ddaniel27
|
||||
*/
|
||||
|
||||
function problem28 (dim) {
|
||||
function problem28(dim) {
|
||||
if (dim % 2 === 0) {
|
||||
throw new Error('Dimension must be odd')
|
||||
}
|
||||
@ -28,24 +28,24 @@ function problem28 (dim) {
|
||||
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 simplifying, 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
|
||||
* 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 simplifying, 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user