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 to n-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 the sum variable.
    • The method returns the sum variable, which contains the sum of all the multiples of 3 and 5 up to n-1.

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, and m2. It calculates the sum of multiples of m1 and m2 up to max using the Inclusion-Exclusion principle.
    • The sum of multiples of m1 and m2 up to max is equal to the sum of multiples of m1 up to max plus the sum of multiples of m2 up to max minus the sum of multiples of m1 multiplied by m2 up to max (because the sum of multiples of m1 and m2 up to max counts multiples of m1 and m2 that are divisible by both m1 and m2 twice).
  • Method: sumMultiple

    • This method takes two parameters: max and m. It calculates the sum of multiples of m up to max.
    • It uses the formula m * (maxDivM * (maxDivM + 1)) / 2, where maxDivM is the maximum integer that divides max without leaving a remainder.
  • Method: sumMultiples (unused)

    • This method is a helper method used for testing. It calculates the sum of multiples of n1 and n2 up to max using a simple loop.
  • Method: sumMultiple (unused)

    • This method is a helper method used for testing. It calculates the sum of multiples of n up to max using a simple loop.

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