patternjavaMinor
An integer is even subset of another integer
Viewed 0 times
evensubsetintegeranother
Problem
An integer
I wrote the following code to check for even subsets.
m is defined to be an even subset of another integer n if every even factor of m is also a factor of n.18is an even subset of12because the even factors of18are2and6and these are both factors of12.
- But
18is not an even subset of32because6is not a factor of32.
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 < m ; i++) {
if(( m % i == 0) && (i % 2 == 0)) {
firstNumber = i;
for(int j = 2; j < n ; j++) {
if( (n % j == 0) && (j % 2 == 0)){
secondNumber = j;
if(firstNumber == secondNumber) {
status = true;
break;
} else {
status = false;
}
}
}
if(!status){
status = false;
}
}
}
return status;
}
}Solution
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
You also don't need
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
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
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 < m; evenFactor += 2) {
if ((m % evenFactor == 0) && (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 <= sqrtM; evenFactor += 2) {
if (m % evenFactor == 0) {
if (n % evenFactor != 0) {
return false;
}
int otherFactor = m / evenFactor;
if ((otherFactor % 2 == 0) && (n % otherFactor != 0)) {
return false;
}
}
}
return true;
}Code Snippets
public static boolean isEvenSubset(int m, int n) {
for (int evenFactor = 2; evenFactor < m; evenFactor += 2) {
if ((m % evenFactor == 0) && (n % evenFactor != 0)) {
return false;
}
}
return true;
}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 <= sqrtM; evenFactor += 2) {
if (m % evenFactor == 0) {
if (n % evenFactor != 0) {
return false;
}
int otherFactor = m / evenFactor;
if ((otherFactor % 2 == 0) && (n % otherFactor != 0)) {
return false;
}
}
}
return true;
}Context
StackExchange Code Review Q#117567, answer score: 7
Revisions (0)
No revisions yet.