fork download
  1. //Brayan Jay Bohol
  2.  
  3. // Requires run from the other IDE since
  4. // Codiva have small time given for timeout.
  5. // *The output always have exit code: 143
  6. // when using Codiva.
  7.  
  8. // The output works if a < 12 but a = 16 based
  9. // on the given question.
  10. // *The question ask about T(16) where T(a) is
  11. // the sum of 2025 iterations.
  12.  
  13. public class Main {
  14. public static String compute(int N) { // Recursion Mode
  15. long total_1 = 0, total_2 = 0; // Initialize total to 0
  16. long total_1_b = 0, total_2_b = 0;
  17. long a, b;
  18. StringBuilder result = new StringBuilder();
  19.  
  20. for (b = 1; b < Math.pow(10, N / 2); b++) {
  21. int n = String.valueOf(b).length();
  22. double v = 4 * b * (1 - Math.pow(10, n)) + Math.pow(10, 2 * n);
  23. if (v > 0) {
  24. v = Math.sqrt(v);
  25.  
  26. if (v == (int) v) { // Check if v is a perfect square
  27. v = (int) v;
  28.  
  29. a = (int) ((Math.pow(10, n) - 2 * b - v) / 2);
  30. if (a != 0) {
  31. // Add formatted (a, b) to result
  32. if (result.length() > 0)
  33. System.out.println();
  34.  
  35. total_1 += (a * Math.pow(10, n));
  36. total_1_b += b;
  37. }
  38.  
  39. a = (int) ((Math.pow(10, n) - 2 * b + v) / 2);
  40. // Add formatted (a, b) to result
  41. if (result.length() > 0)
  42. System.out.println();
  43.  
  44. total_2 += (a * Math.pow(10, n));
  45. total_2_b += b;
  46. }
  47. }
  48. }
  49.  
  50. // Return the formatted result with the sum
  51. long total_a = total_1 + total_2;
  52. long total_b = total_1_b + total_2_b;
  53. long total = total_a + total_b;
  54. return result.append("\nT(" + N +") = ").append( total ).toString();
  55.  
  56. }
  57.  
  58. public static void main(String[] args) {
  59. int a = 16; // Let n = a
  60. System.out.println(compute(a));
  61.  
  62. }
  63. }
  64.  
Success #stdin #stdout 10.53s 125924KB
stdin
Standard input is empty
stdout
T(16) = 72673459417881349