fork download
  1. public class Main {
  2. public static void main(String[] args) {
  3. // Hardcoded test cases for easy execution
  4. int[] testCases = {2, 6, 24, 64, 1000000000};
  5.  
  6. for (int n : testCases) {
  7. System.out.print("n = " + n + " -> ");
  8. solve(n);
  9. }
  10. }
  11.  
  12. public static void solve(int n) {
  13. int a = -1;
  14.  
  15. /*
  16.   * LOGIC (a^3 <= n):
  17.   * We need 3 distinct factors where a < b < c.
  18.   * Since a, b, c are distinct, a * a * a MUST be strictly less than a * b * c (which equals n).
  19.   * Therefore, if a exists, it cannot be larger than the cube root of n.
  20.   * Using (i * i * i <= n) prevents searching uselessly large numbers.
  21.   */
  22. for (int i = 2; i * i * i <= n; i++) {
  23. if (n % i == 0) {
  24. a = i;
  25. break;
  26. }
  27. }
  28.  
  29. // If no valid first factor 'a' exists, we cannot form 3 distinct numbers
  30. if (a == -1) {
  31. System.out.println("NO");
  32. return;
  33. }
  34.  
  35. int rem = n / a;
  36. int b = -1;
  37.  
  38. // Find 'b' starting from (a + 1) to ensure b > a
  39. // It must be smaller than the square root of the remaining value (i * i <= rem)
  40. for (int i = a + 1; i * i <= rem; i++) {
  41. if (rem % i == 0) {
  42. b = i;
  43. break;
  44. }
  45. }
  46.  
  47. if (b == -1) {
  48. System.out.println("NO");
  49. return;
  50. }
  51.  
  52. // 'c' takes whatever is left over from the original number
  53. int c = rem / b;
  54.  
  55. // Final sanity check: ensure c is greater than b and distinct from a
  56. if (c > b && c != a) {
  57. System.out.println("YES (" + a + " * " + b + " * " + c + ")");
  58. } else {
  59. System.out.println("NO");
  60. }
  61. }
  62. }
  63.  
Success #stdin #stdout 0.18s 60040KB
stdin
Standard input is empty
stdout
n = 2 -> NO
n = 6 -> NO
n = 24 -> YES (2 * 3 * 4)
n = 64 -> YES (2 * 4 * 8)
n = 1000000000 -> YES (2 * 4 * 125000000)