fork(2) download
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3. import java.util.stream.IntStream;
  4.  
  5. class Ideone
  6. {
  7. private static BigInteger[] primes =
  8. IntStream.of(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,103,107,109,113,127,131,137,139,149,151,157,163,167,173).mapToObj(BigInteger::valueOf).toArray(BigInteger[]::new);
  9.  
  10.  
  11. public static void main(String[] args)
  12. {
  13. try (Scanner in = new Scanner(System.in)) {
  14. while (in.hasNextLong()) {
  15. solve(in.nextLong());
  16. }
  17. }
  18. }
  19.  
  20.  
  21. static void solve(long m)
  22. {
  23. BigInteger ans = m <= 0 ? BigInteger.ONE : solve(m, 1, BigInteger.ONE, null, 0, 64);
  24. System.out.printf("%d -> %d%n", m, ans);
  25. }
  26.  
  27.  
  28. static BigInteger solve(long m, long solutions, BigInteger n, BigInteger ans, int f, int fn)
  29. {
  30. BigInteger nn = n;
  31. for (int i = 1; i <= fn; i++) {
  32. nn = nn.multiply(primes[f]);
  33. if (ans != null && nn.compareTo(ans) > 0) return ans;
  34.  
  35. long ns = solutions * (i * 2 + 1) - i;
  36. if (ns > m) {
  37. return ans == null || nn.compareTo(ans) < 0 ? nn : ans;
  38. }
  39.  
  40. ans = solve(m, ns, nn, ans, f + 1, i);
  41. }
  42. return ans;
  43. }
  44. }
  45.  
Success #stdin #stdout 0.43s 2184192KB
stdin
2
5
10
100
1000
10000
250000000000
stdout
2 -> 4
5 -> 12
10 -> 24
100 -> 1260
1000 -> 180180
10000 -> 116396280
250000000000 -> 4633408464970568987374681757947200