Skip to content

Commit 343f7d9

Browse files
* docs: kata description * feat: kata/difference-of-squares --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent a60c3be commit 343f7d9

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# [Difference Of Squares](https://www.codewars.com/kata/difference-of-squares "https://www.codewars.com/kata/558f9f51e85b46e9fa000025")
2+
3+
*Recreation of [Project Euler problem #6](https://projecteuler.net/problem=6)*
4+
5+
Find the difference between the square of the sum of the first `n` natural numbers `(1 <= n <= 100)` and the sum of their squares.
6+
7+
## Example
8+
9+
For example, when `n = 10`:
10+
11+
* The square of the sum of the numbers is:
12+
13+
(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)² = 55² = 3025
14+
15+
* The sum of the squares of the numbers is:
16+
17+
1² + 2² + 3² + 4² + 5² + 6² + 7² + 8² + 9² + 10² = 385
18+
19+
Hence the difference between square of the sum of the first ten natural numbers and the sum of the squares of those numbers is:
20+
`3025 - 385 = 2640`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Kata {
2+
static int differenceOfSquares(int n) {
3+
return (3 * n + 2) * (n * n * n - n) / 12;
4+
}
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class DifferenceOfSquaresTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
5, 170
10+
10, 2640
11+
100, 25164150
12+
""")
13+
void sample(int n, int expected) {
14+
assertEquals(expected, Kata.differenceOfSquares(n));
15+
}
16+
}

kata/7-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
- [Dice Rotation](dice-rotation "5ff2093d375dca00170057bc")
138138
- [Difference between two collections](difference-between-two-collections "594093784aafb857f0000122")
139139
- [Difference between years. (Level 1)](difference-between-years-level-1 "588f5a38ec641b411200005b")
140+
- [Difference Of Squares](difference-of-squares "558f9f51e85b46e9fa000025")
140141
- [Digital cypher vol 2](digital-cypher-vol-2 "592edfda5be407b9640000b2")
141142
- [Digital cypher](digital-cypher "592e830e043b99888600002d")
142143
- [Digitize](digitize "5417423f9e2e6c2f040002ae")

0 commit comments

Comments
 (0)