How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Java programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Java programming language
Table of Contents
Problem Statement
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n. Show output for n = 1000. This is is the same as Project Euler problem 1. Extra credit: do this efficiently for n = 1e20 or higher.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Java programming language
Source Code 1:
- Class:
SumMultiples
- Method:
getSum
- This method takes a parameter
n
and calculates the sum of all the multiples of 3 and 5 up ton-1
. - It uses a loop to iterate through numbers from 3 to
n-1
and checks if the current number is divisible by 3 or 5. If it is, the number is added to thesum
variable. - The method returns the
sum
variable, which contains the sum of all the multiples of 3 and 5 up ton-1
.
- This method takes a parameter
Source Code 2:
-
Class:
SumMultiples
-
Method:
main
- This method is the entry point of the program.
- It uses a
for
loop to iterate over a range of limits (powers of 10). - For each limit, it calculates the sum of multiples of 3 and 5 up to that limit and prints the result.
-
Method:
sumMultiples
- This method takes three parameters:
max
,m1
, andm2
. It calculates the sum of multiples ofm1
andm2
up tomax
using the Inclusion-Exclusion principle. - The sum of multiples of
m1
andm2
up tomax
is equal to the sum of multiples ofm1
up tomax
plus the sum of multiples ofm2
up tomax
minus the sum of multiples ofm1
multiplied bym2
up tomax
(because the sum of multiples ofm1
andm2
up tomax
counts multiples ofm1
andm2
that are divisible by bothm1
andm2
twice).
- This method takes three parameters:
-
Method:
sumMultiple
- This method takes two parameters:
max
andm
. It calculates the sum of multiples ofm
up tomax
. - It uses the formula
m * (maxDivM * (maxDivM + 1)) / 2
, wheremaxDivM
is the maximum integer that dividesmax
without leaving a remainder.
- This method takes two parameters:
-
Method:
sumMultiples
(unused)- This method is a helper method used for testing. It calculates the sum of multiples of
n1
andn2
up tomax
using a simple loop.
- This method is a helper method used for testing. It calculates the sum of multiples of
-
Method:
sumMultiple
(unused)- This method is a helper method used for testing. It calculates the sum of multiples of
n
up tomax
using a simple loop.
- This method is a helper method used for testing. It calculates the sum of multiples of
Source code in the java programming language
class SumMultiples {
public static long getSum(long n) {
long sum = 0;
for (int i = 3; i < n; i++) {
if (i % 3 == 0 || i % 5 == 0) sum += i;
}
return sum;
}
public static void main(String[] args) {
System.out.println(getSum(1000));
}
}
import java.math.BigInteger;
public class SumMultiples {
public static void main(String[] args) {
BigInteger m1 = BigInteger.valueOf(3);
BigInteger m2 = BigInteger.valueOf(5);
for ( int i = 1 ; i <= 25 ; i++ ) {
BigInteger limit = BigInteger.valueOf(10).pow(i);
System.out.printf("Limit = 10^%d, answer = %s%n", i, sumMultiples(limit.subtract(BigInteger.ONE), m1, m2));
}
}
// Use Inclusion - Exclusion
private static BigInteger sumMultiples(BigInteger max, BigInteger n1, BigInteger n2) {
return sumMultiple(max, n1).add(sumMultiple(max, n2)).subtract(sumMultiple(max, n1.multiply(n2)));
}
private static BigInteger sumMultiple(BigInteger max, BigInteger m) {
BigInteger maxDivM = max.divide(m);
return m.multiply(maxDivM.multiply(maxDivM.add(BigInteger.ONE))).divide(BigInteger.valueOf(2));
}
// Used for testing
@SuppressWarnings("unused")
private static long sumMultiples(long max, long n1, long n2) {
return sumMultiple(max, n1) + sumMultiple(max, n2) - sumMultiple(max, n1 * n2);
}
private static long sumMultiple(long max, long n) {
long sum = 0;
for ( int i = 1 ; i <= max ; i++ ) {
if ( i % n == 0 ) {
sum += i;
}
}
return sum;
}
}
You may also check:How to resolve the algorithm Bulls and cows/Player step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm HTTPS step by step in the Tcl programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the XSLT programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Go programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Logo programming language