File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
algorithms-modules/algorithms-numeric/src/test/java/com/baeldung/algorithms/consecutivenumbers Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .baeldung .algorithms .consecutivenumbers ;
2
+
3
+ import org .junit .jupiter .api .Test ;
4
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5
+
6
+ public class ConsecutiveNumbersUnitTest {
7
+
8
+ @ Test
9
+ void whenIsSumOfConsecutiveUsingBruteForce_thenReturnsTrue () {
10
+ int n = 15 ;
11
+
12
+ boolean isSumOfConsecutive = false ;
13
+ for (int k = 2 ; (k * (k - 1 )) / 2 < n ; k ++) {
14
+ int diff = n - k * (k - 1 ) / 2 ;
15
+ if (diff % k == 0 && diff / k > 0 ) {
16
+ isSumOfConsecutive = true ;
17
+ break ;
18
+ }
19
+ }
20
+
21
+ assertTrue (isSumOfConsecutive );
22
+ }
23
+
24
+ @ Test
25
+ void whenIsSumOfConsecutiveUsingBitwise_thenReturnsTrue () {
26
+ int n = 15 ;
27
+ boolean result = (n > 0 ) && ((n & (n - 1 )) != 0 );
28
+ assertTrue (result );
29
+ }
30
+
31
+ }
You can’t perform that action at this time.
0 commit comments