I have a program that reads two real numbers and then it prints out all the numbers between these two, that are divisible by 2 or 3 or 5. The program works fine, but when a user enters two really large numbers (for example, 1122222123333 and 214123324434434) the program takes a very long time to calculate the result. I would like to somehow fix the program, so that even for large numbers the result would be printed out instantly.
here is my code so far :
import java.util.Scanner;
public class Numbers
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
long x = sc.nextLong(); // first number input
long y = sc.nextLong(); // second number input
long num = 0; // new variable num -- means all the numbers between these to given input numbers
long count = 0; // loop counter - how many numbers are divided by 2 or 3 or 5
for (num = x; x <= num && num <= y; num++) {
if (num % 2 == 0 | num % 3 == 0 | num % 5 == 0) {
count = count + 1; // increasing the counter by 1, so that every time the loop repeats, the counter increases...
}
}
System.out.println(count); // prints out how many numbers are divided by 2 or 3 or 5 ...
}
}