Created
March 8, 2015 01:38
-
-
Save bloxgate/6d3eaf22f227be5bc20e to your computer and use it in GitHub Desktop.
Cube Expression as a Sequence of Consecutive Odd Numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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