0

So I have fixed my program (from this question) a bit with some help, but still for some example input numbers the result at the end isn't correct.

The program receives two numbers as input, and has to find how many numbers between them are divisible by 2, 3 or 5.

import java.util.*;
public class Multiples {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);


        long x = sc.nextLong();   // first input number

        long y = sc.nextLong();   // second input number


        long num = 0;    
        long z = (y-x);     // the numbers between y and x

        long a = z/2;   // numbers divisible by 2
        long b = z/3;   // numbers divisible by 3
        long c = z/5;   // numbers divisible by 5

        long d = z/(2*3);     // intersections
        long e = z/(2*5);
        long f = z/(3*5);
        long g = z/(2*3*5);

        num = a+b+c-d-e-f+g;  // we add up the multitudes of a and b and c and we subtract the intersections of d and e and f and add up the intersection of g 

        System.out.println(num);

    }
}   

the thing is my program works for some examples and for others the program fails completely ..

Community
  • 1
  • 1
user3475581
  • 129
  • 2
  • 2
  • 10
  • 1
    What does it work for and what does it _fail completely_ for? Give us example output. – mattias Nov 08 '14 at 11:00
  • 1
    What is the actual purpose of this program?Give the details.Give examples.Be more specific and clear. – Saptak Bhattacharya Nov 08 '14 at 11:02
  • What do you mean by "fails completely"? I expect there would be some inaccuracies you should account for. For example, if y=100 and x=50, a would be 25, but there are actually 26 numbers divisible by 2 in this range. You should figure out how to change the values of a,b,c,d,e,f,g (probably at most adding or subtracting one from the result) depending on whether the numbers at the edges (y and x) are divisible by the relevant number. – Eran Nov 08 '14 at 11:04

1 Answers1

0

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;
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • Thank you, it helped me a lot, the program using your method works perfectly with all the random examples I could imagined, especially for me, because I am beginner, and using something new in the process. – user3475581 Nov 08 '14 at 18:14