Created
January 8, 2015 16:35
-
-
Save AndrewWang1993/0bd44a030ffc2b920a5e to your computer and use it in GitHub Desktop.
素数排序(神奇的排序方式)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
class PrimeCompare implements ICompare { | |
@Override | |
public boolean compare(String longString, String shortString) { | |
BigInteger product = new BigInteger("1"); | |
for (int i = 0; i < longString.length(); i++) { | |
int index = longString.charAt(i) - 'A'; | |
product = product.multiply(BigInteger.valueOf(primeTable[index])); | |
} | |
for (int j = 0; j < shortString.length(); j++) { | |
int index = shortString.charAt(j) - 'A'; | |
BigInteger prime = BigInteger.valueOf(primeTable[index]); | |
if (!(product.mod(prime).equals(BigInteger.ZERO))) { | |
return false; | |
} | |
} | |
return true; | |
} | |
private long[] primeTable = new long[]{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment