Here’s the Solution
X^3-Y^3 = (X-Y)(X^2 + XY + Y^2) = N
Then we should check all factors of N,assume we have a factor i
let X-Y=i,X^2 + XY + Y^2 = N/i
solving those equations together Will give us X and Y.
However the problem here is the bounds.i.e what is the limit for i
for i to be maximum then X-Y must be maximum.
then Y must be zero
at Y = 0
N=(X-Y)*(X^2+XY+Y^2)=X*(X^2)
then i must be <= cubicRoot(N)
Also since Y cannot be zero
then i must be < cubicRoot(N)
import java.util.Scanner; public class CubesExtreme { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) { long l = in.nextLong(); if (l == 0) break; long xx = -1; long yy = Integer.MAX_VALUE; for (long i = 1; i * i * i < l; i++) { if (l % i == 0) { // x=i+y // (i+y)^2 + (i+y)y + y^2 // i^2 + 3y^2 + 3iy long a = 3; long b = 3 * i; long c = i * i - (l / i); if (b * b >= 4 * a * c) { long r1 = (long) ((-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a)); long r2 = (long) ((-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a)); if (r1 > 0 && (i + r1) * (i + r1) * (i + r1) - r1 * r1 * r1 == l) { if (r1 < yy) { xx = i + r1; yy = r1; } } if (r2 > 0 && (i + r2) * (i + r2) * (i + r2) - r2 * r2 * r2 == l) { if (r2 < yy) { xx = i + r2; yy = r2; } } } } } if (xx > 0) System.out.println(xx + " " + yy); else System.out.println("No solution"); } } }
from line 16 to line 39 what’s going on i don’t undersand
thank u now i’ve solved this problem with one line in the loop 😀
congratz 🙂