Skip to content

Instantly share code, notes, and snippets.

@kazurof
Last active November 7, 2015 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazurof/f206bfd77053f3b4ce97 to your computer and use it in GitHub Desktop.
Save kazurof/f206bfd77053f3b4ce97 to your computer and use it in GitHub Desktop.
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"));
}
}
@kazurof
Copy link
Author

kazurof commented Nov 7, 2015

val == 1L だった時の対応は、if 文書くしかないのかなー。ふむ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment