An integer is even subset of another integer An integer m is defined to be an even subset of another integer n if every even factor of m is also a factor of n.


18 is an even subset of 12 because the even factors of 18 are 2 and 6 and these are both factors of 12.
But 18 is not an even subset of 32 because 6 is not a factor of 32.


I wrote the following code to check for even subsets.

public class EvenSubset {
public static void main(String args[]) {
    System.out.println("The result is: " + isEvenSubset(18, 32));
}
public static boolean isEvenSubset(int m, int n) {
    boolean status = false;
    int firstNumber = 0;
    int secondNumber = 0;
    for(int i = 2; i &lt; m ; i++) {
        if(( m % i == 0) &amp;&amp; (i % 2 == 0)) {
            firstNumber = i;
            for(int j  = 2;  j &lt; n ; j++) {
                if( (n % j == 0) &amp;&amp; (j % 2 == 0)){
                    secondNumber = j;
                    if(firstNumber == secondNumber) {
                        status = true;
                        break;
                    } else {
                        status = false;

                    }
                }

        }
       if(!status){
           status = false;
       } 

    }

}
    return status;
}
 }
I thought that this coding style looked familiar… and indeed it is.  As I previously remarked, flag variables suck.  You shouldn't need a variable like status.

You also don't need firstNumber and secondNumber.  They are just the same as i and j, respectively.

In fact, the whole algorithm can be implemented more simply, just translating the problem description directly into code.  Iterate through the possible even factors of m.  If you find a candidate that is a factor of m but isn't a factor of n, then you can conclude that m is not an even subset of n.

public static boolean isEvenSubset(int m, int n) {
    for (int evenFactor = 2; evenFactor &lt; m; evenFactor += 2) {
        if ((m % evenFactor == 0) &amp;&amp; (n % evenFactor != 0)) {
            return false;
        }
    }
    return true;
}


However, there is an optimization that we can do, since factors always occur in pairs.  There is also a quick test we can do to immediately detect an obvious result, if m is odd.

public static boolean isEvenSubset(int m, int n) {
    // Optional optimization: m has no even factors
    if (m % 2 != 0) return true;

    int sqrtM = (int)(Math.sqrt(m) + 1);
    for (int evenFactor = 2; evenFactor &lt;= sqrtM; evenFactor += 2) {
        if (m % evenFactor == 0) {
            if (n % evenFactor != 0) {
                return false;
            }
            int otherFactor = m / evenFactor;
            if ((otherFactor % 2 == 0) &amp;&amp; (n % otherFactor != 0)) {
                return false;
            }
        }
    }
    return true;
}