File tree Expand file tree Collapse file tree 6 files changed +96
-1
lines changed
Expand file tree Collapse file tree 6 files changed +96
-1
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ public static int sumOfDigits(long number) {
1212 number = Math .abs (number );
1313 int sum = 0 ;
1414 while (number != 0 ) {
15- sum += number % 10 ;
15+ sum = ( int ) ( sum + number % 10 ) ;
1616 number /= 10 ;
1717 }
1818 return sum ;
Original file line number Diff line number Diff line change 1+ package com .examplehub .maths ;
2+
3+ public class SumOfDivisor {
4+
5+ /**
6+ * Calculate sum of divisors of a number, including itself.
7+ *
8+ * @param number the number to be calculated
9+ * @return sum of divisors.
10+ */
11+ public static int sumOfDivisorInclude (int number ) {
12+ int sum = 0 ;
13+ for (int i = 1 ; i <= number ; i ++) {
14+ if (number % i == 0 ) {
15+ sum += i ;
16+ }
17+ }
18+ return sum ;
19+ }
20+
21+ /**
22+ * Calculate sum of divisors of a number, excluding itself.
23+ *
24+ * @param number the number to be calculated
25+ * @return sum of divisors.
26+ */
27+ public static int sumOfDivisorExclude (int number ) {
28+ int sum = 0 ;
29+ for (int i = 1 ; i < number ; i ++) {
30+ if (number % i == 0 ) {
31+ sum += i ;
32+ }
33+ }
34+ return sum ;
35+ }
36+ }
Original file line number Diff line number Diff line change 33import com .examplehub .maths .Factorial ;
44import com .examplehub .maths .SumOfDigits ;
55
6+ /**
7+ * https://projecteuler.net/problem=20
8+ */
69public class Problem20 {
710 public static int solution1 (int number ) {
811 return SumOfDigits .sumOfDigits (Factorial .factorial (number ));
Original file line number Diff line number Diff line change 1+ package com .examplehub .projecteuler ;
2+
3+ import com .examplehub .maths .SumOfDivisor ;
4+
5+ /**
6+ * https://projecteuler.net/problem=21
7+ */
8+ public class Problem21 {
9+ public static int solution1 (int n ) {
10+ int sum = 0 ;
11+ for (int i = 1 ; i < n ; i ++) {
12+ int sumOfDivisor = SumOfDivisor .sumOfDivisorExclude (i );
13+ if (SumOfDivisor .sumOfDivisorExclude (sumOfDivisor ) == i && sumOfDivisor != i ) {
14+ sum += i ;
15+ }
16+ }
17+ return sum ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .examplehub .maths ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static org .junit .jupiter .api .Assertions .*;
6+
7+ class SumOfDivisorTest {
8+ @ Test
9+ void testSumOfDivisorInclude () {
10+ assertEquals (0 , SumOfDivisor .sumOfDivisorInclude (0 ));
11+ assertEquals (1 , SumOfDivisor .sumOfDivisorInclude (1 ));
12+ assertEquals (3 , SumOfDivisor .sumOfDivisorInclude (2 ));
13+ assertEquals (18 , SumOfDivisor .sumOfDivisorInclude (10 ));
14+ }
15+
16+ @ Test
17+ void testSumOfDivisorExclude () {
18+ assertEquals (0 , SumOfDivisor .sumOfDivisorExclude (0 ));
19+ assertEquals (0 , SumOfDivisor .sumOfDivisorExclude (1 ));
20+ assertEquals (1 , SumOfDivisor .sumOfDivisorExclude (2 ));
21+ assertEquals (8 , SumOfDivisor .sumOfDivisorExclude (10 ));
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package com .examplehub .projecteuler ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static org .junit .jupiter .api .Assertions .*;
6+
7+ class Problem21Test {
8+
9+ @ Test
10+ void test () {
11+ assertEquals (504 , Problem21 .solution1 (1000 ));
12+ assertEquals (31626 , Problem21 .solution1 (10000 ));
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments