-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from MikeBeloborodov/7_kyu_difference_of_squares
difference of squares
- Loading branch information
Showing
5 changed files
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
### Difference Of Squares | ||
|
||
https://www.codewars.com/kata/558f9f51e85b46e9fa000025 | ||
|
||
![description](./description.jpg "Description") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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,5 @@ | ||
import { differenceOfSquares } from "./difference_of_squares"; | ||
|
||
describe("test cases", () => { | ||
it("should return 170", () => expect(differenceOfSquares(5)).toBe(170)); | ||
}); |
This file contains 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,16 @@ | ||
export const differenceOfSquares = (n: number) => { | ||
const squareOfTheSum = Math.pow( | ||
Array(n) | ||
.fill(1) | ||
.map((_, index) => index + 1) | ||
.reduce((sum, next) => sum + next, 0), | ||
2, | ||
); | ||
|
||
const sumOfSquares = Array(n) | ||
.fill(1) | ||
.map((_, index) => Math.pow(index + 1, 2)) | ||
.reduce((sum, next) => sum + next, 0); | ||
|
||
return squareOfTheSum - sumOfSquares; | ||
}; |
This file contains 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