Last active
November 7, 2015 13:14
-
-
Save kazurof/f206bfd77053f3b4ce97 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
class Fraction { | |
/** | |
* 値を素因数分解して返します。 | |
* 素数を x で連結する表記です。 | |
* 10 => 2x2x5x5 | |
* */ | |
static String factorize(long val) { | |
if(val == 1L){ | |
return "1"; | |
} | |
List<String> result = new ArrayList<>(); | |
BigInteger bi = BigInteger.ONE; | |
while (bi.longValue() < val) { | |
long prime = bi.nextProbablePrime().longValue(); | |
while (val % prime == 0) { | |
result.add(String.valueOf(prime)); | |
val = val / prime; | |
} | |
bi = BigInteger.valueOf(prime); | |
} | |
return result.stream().collect(Collectors.joining("x")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
val == 1L だった時の対応は、if 文書くしかないのかなー。ふむ。