how to make this code faster, but without extra imports:…
- User
how to make this code faster, but without extra imports:
//****************'******************* // Program: Dio.java // // Purpose: Make k = x3 + y3 + z3 work, with certain ranges // Made by Michael Ortola // Mr Friebe's Class // Finished 9/5/23 //*********************************** import java.util.Scanner; public class Dio_MAO { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // ET start - insert at start of algorithm of interest long et = System.nanoTime(); System.out.print("Enter range for k: "); int krange = scan.nextInt();
codeSystem.out.print("Enter span for x, y, z: "); int span = scan.nextInt(); System.out.println(""); int solution = 0; for (int k = 0; k <= krange; k++) { boolean solutionFound = false; for (int x = -span; x <= span && !solutionFound; x++) { for (int y = -span; y <= span && !solutionFound; y++) //negative span to positive span, is the range xyz can be pickekd { int z = (int) Math.cbrt(k - x*x*x - y*y*y); if (z*z*z == k - x*x*x - y*y*y) { System.out.println("k: " + k + ", x: " + x + ", y: " + y + ", z: " + z); solutionFound = true; // used for the the check later in the code to find not founds solution++; } } } if (!solutionFound) { System.out.println("k: " + k + " Not found!"); } } // ET end - insert at end of algorithm of interest et = System.nanoTime() - et; System.out.println("\nSolution(s) found for " + solution + " values of k."); System.out.printf("\nElapsed time: %.2E nsecs", (double) et); - Assistant