Skip to content

Instantly share code, notes, and snippets.

@bloxgate
Created March 8, 2015 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bloxgate/6d3eaf22f227be5bc20e to your computer and use it in GitHub Desktop.
Save bloxgate/6d3eaf22f227be5bc20e to your computer and use it in GitHub Desktop.
Cube Expression as a Sequence of Consecutive Odd Numbers
import java.util.Scanner;
public class cubeSums {
private final static double thirds = 1.0/3.0;
static String oString = "";
public static boolean isCube(double c){
double powerVal = Math.pow(c, thirds);
if(Math.round(c % powerVal) == 0 && c != 3){
return true;
}else{
return false;
}
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
while(input.hasNextDouble()){
double number = input.nextDouble();
oString = "";
if(number == 1){ oString = "1 = 1"; } else {
if(isCube(number)){
oString += (int) number + " = ";
double cRoot = Math.round(Math.pow(number, thirds));
int sNum = (int) ((cRoot * cRoot) - cRoot) + 1;
oString += sNum + " + ";
for(int i = 1; i <= cRoot - 1; i++){
if(i == cRoot - 1){
oString += (sNum + (2 * i));
}else{
oString += (sNum + (2 * i)) + " + ";
}
}
}else{
oString = "NOT CUBE";
}
}
System.out.println(oString + "\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment