w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Java - multiples program

First, you should note that your original question was inclusive. That is, if either x or y were themselves divisible by one of the numbers, they were included in the count.

This means you cannot use z = (y-x) because that actually excludes one of them. Suppose x is 14 and y is 16. The result would be 3 - the numbers 14,15 and 16 are all divisible by either 2,3 or 5. If you calculated 'y-x' you'd only get 2 to begin with.

However, this is not the only problem. The proble is that z/2, z/3 etc. do not give you the exact number of divisible numbers in the range. It's very important to consider what x and y actually were. For example, let's take two examples where the difference is 23.

Example 1 - x=11, y=33

  • We calculate z to be 33-11+1 (adding the one to be inclusive), which is 23.
  • Wanting to find the number of numbers divisible by 5 in this interval, we calculate z/5, which gives 4.
  • Indeed, the divisible numbers in that interval are 15,20,25,30. Four numbers, good!

Example 2 - x=9, y=31

  • We calculate z to be 31-9+1, which, again, is 23.
  • Once again, we calculate z/5 to be 4.
  • But this time, the numbers in that interval that are divisible by 5 are: 10,15,20,25,30. Five numbers, not 4!

Note that unlike what one of the comments above said, both numbers x and y in this example are not divisible by 5. So this can't be a way to find out when it's 4 and when it's 5.

So what do we do?

We don't calculate the difference between x and y. This loses information. What we do is different. We find the number of numbers divisible by 5 between 0 and x (not inclusive), and between zero and y (inclusive). The difference between these two numbers is exactly the number of numbers divisible by 5 between y and x. Let's demonstrate with the two examples from above.

Example 1 - x=11, y=33

  • What are all the numbers k, such that k > 0 and k < x, and also k is divisible by 5? The numbers are 5 and 10. That's 2 numbers.
  • What are all the numbers j, such that j > 0 and j ≤ y, and also j is divisible by 5? The numbers are 5, 10, 15, 20, 25,30. That is 6 numbers.
  • The numbers 5 and 10, the ones we found for x, don't interest us, as they not in our interval. So out of 6 numbers, we exclude the two, and we have 4 remaining: 15, 20, 25, 30.

Example 2 - x=9, y=31

  • Find the k numbers for 9. These include just the number 5. So we have 1 number.
  • Find the j numbers for 31. Again it's 6 numbers 5,10,15,20,25,30.
  • Subtract 1 (this time it's just 1) from 6. We have 5. And indeed, there are five numbers between 9 and 31 which are divisible by 5: 10,15,20,25,30.

So, how do we calculate it using java?

For any number n, the number of numbers divisible by 5 between 0 and itself, including itself if it's divisible, is exactly n/5 (integer division). Why? I'll leave that for you to consider, but basically it's because starting from 1, there are always four indivisibles and one divisible, four indivisibles, one divisible all the way up to n.

And to exclude itself, even if it is divisible, you do (n-1)/5. So if you have the number 10, and you don't want to consider it, take the number 9 and you get the correct number.

Of course, all of this discussion is true for any number, not just for five. So here is a small method that calculates how many divisible numbers are between x and y:

private static long findDivisibles( long x, long y, long divisor
) {
    return y / divisor - (x-1) / divisor;
}

Now you can correct your program:

long div2 = findDivisibles( x, y, 2 );
long div3 = findDivisibles( x, y, 3 );
long div5 = findDivisibles( x, y, 5 );
long div23 = findDivisibles( x, y, 2*3 );
long div35 = findDivisibles( x, y, 3*5 );
long div25 = findDivisibles( x, y, 2*5 );
long div235 = findDivisibles( x, y, 2*3*5 );

num = div2 + div3 + div5 - div23 - div35 - div25 + div235;




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.