File tree Expand file tree Collapse file tree 4 files changed +42
-0
lines changed Expand file tree Collapse file tree 4 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
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 `
Original file line number Diff line number Diff line change
1
+ interface Kata {
2
+ static int differenceOfSquares (int n ) {
3
+ return (3 * n + 2 ) * (n * n * n - n ) / 12 ;
4
+ }
5
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 137
137
- [ Dice Rotation] ( dice-rotation " 5ff2093d375dca00170057bc ")
138
138
- [ Difference between two collections] ( difference-between-two-collections " 594093784aafb857f0000122 ")
139
139
- [ Difference between years. (Level 1)] ( difference-between-years-level-1 " 588f5a38ec641b411200005b ")
140
+ - [ Difference Of Squares] ( difference-of-squares " 558f9f51e85b46e9fa000025 ")
140
141
- [ Digital cypher vol 2] ( digital-cypher-vol-2 " 592edfda5be407b9640000b2 ")
141
142
- [ Digital cypher] ( digital-cypher " 592e830e043b99888600002d ")
142
143
- [ Digitize] ( digitize " 5417423f9e2e6c2f040002ae ")
You can’t perform that action at this time.
0 commit comments