Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created March 14, 2014 05:20
Show Gist options
  • Select an option

  • Save rayjcwu/9542517 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9542517 to your computer and use it in GitHub Desktop.
find the smallest x such that x, 2x, 3x, 4x, 5x, 6x are anagram
import java.util.HashMap;
import java.util.Map;
public class GuessNumber {
public static void main(String[] argv) {
GuessNumber gn = new GuessNumber();
gn.entry();
}
public void entry() {
int x = 1;
while (true) {
Map<Integer, Integer> original = getCount(x);
boolean found = true;
for (int i = 2; i <= 6; ++i) {
if (!original.equals(getCount(i * x))) {
found = false;
break;
}
}
if (found) {
System.out.println(x);
break;
} else {
x++;
}
}
}
public Map<Integer, Integer> getCount(int x) {
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
while (x > 0) {
int d = x % 10;
if (countMap.containsKey(d)) {
countMap.put(d, countMap.get(d) + 1);
} else {
countMap.put(d, 1);
}
x /= 10;
}
return countMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment