Skip to content

Instantly share code, notes, and snippets.

@vijaysharm
Created December 21, 2015 11:26
Show Gist options
  • Save vijaysharm/b0ee4dbfef9ec6cbc0ff to your computer and use it in GitHub Desktop.
Save vijaysharm/b0ee4dbfef9ec6cbc0ff to your computer and use it in GitHub Desktop.
public class Trello {
public static void main(String[] args){
divide_and_conquer(491454843L, "acdegilmnoprstuw", 5);
}
public static void divide_and_conquer(long target, String letters, int size) {
char[] test = new char[size];
for (int index = 0; index < size; index++)
test[index] = 'a';
int it = 1;
do {
String string = new String(test);
long hash = hash(string, letters);
long diff = target - hash;
System.out.println((it++) + ": " + string + " h:" + hash + " d:" + diff);
if (diff == 0) {
System.out.println("success!");
break;
}
for (int index = (test.length - 1); index >= 0; index--) {
double max = Math.pow(37, index);
if (diff >= max) {
int mod = test.length - 1 - index;
int letter = letters.indexOf(test[mod]) + 1;
test[mod] = letters.charAt(letter);
break;
}
}
} while(true);
}
private static long hash(String string, String letters) {
long h = 7;
for (int i = 0; i < string.length(); i++ ) {
h = (h * 37 + letters.indexOf(string.charAt(i)));
}
return h;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment