Sum of divisors

Report a typo

For a given range from a to b both inclusive, count sum of numbers that are divisible by n or m. Use streams to solve the problem.

For example, for range from 10 to 20 and n = 2, m = 3, there are 7 numbers that are divisible by n or m: 10, 12, 14, 15, 16, 18, 20. The sum of these numbers equals 105.

Sample Input 1:

10 20 2 3

Sample Output 1:

105
Write a program in Java 17
import java.util.Scanner;
import java.util.stream.*;

public class Main {

/**
* Calculates the sum of numbers in the range from a to b both inclusive
* that are only divisible by n or m.
*
* @param a > 0
* @param b > 0
* @param n > 0
* @param m > 0
*
* @return sum of numbers from the range that are only divisible by n or m
*/
public static int sum(int a, int b, int n, int m) {
// write your code here
}

// Don't change the code below
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int n = scanner.nextInt();
int m = scanner.nextInt();

System.out.println(sum(a, b, n, m));
}
}
___

Create a free account to access the full topic