Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
Created February 13, 2017 08:56
Show Gist options
  • Save M-ZubairAhmed/5f5631a63999640474bb040d649050e8 to your computer and use it in GitHub Desktop.
Save M-ZubairAhmed/5f5631a63999640474bb040d649050e8 to your computer and use it in GitHub Desktop.
Checks if a number can be written as the sum of two cubes in two different ways: n = a³+b³ = c³+d³.
/*Number with dual Cube Sums
TwoCubeSums
which checks if a given number can be written as the sum of two cubes in two different ways: n = a³+b³ = c³+d³. All the numbers a, b, c and d should be different and greater than 0.
e.g. 1729 = 9³+10³ = 1³+12³.
*/
public class CubeSumsClass {
public static boolean hasTwoCubeSums(int n){
int checker = 0;
for (int i = 0; i <=(int)Math.pow(n,1.0/3.0); i++) {
for (int j = 0; j <=i ; j++) {
if ((int)((Math.pow(i,3.0))+(Math.pow(j,3.0))) == n){
checker ++;
if (checker == 1){System.out.println(checker+"st"+" Set of numbers are "+i+","+"j")}
else{System.out.println(checker+"nd"+" Set of numbers are "+i+","+"j")}
}
}
}
if (checker >= 2){return true;}
else {return false;}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment