Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static int readNumPositive()
- {
- int num;
- num = -1;
- boolean isIncorrect;
- Scanner scan = new Scanner(System.in);
- do {
- isIncorrect = false;
- try
- {
- num = Integer.parseInt(scan.next());
- }
- catch (NumberFormatException e)
- {
- System.err.print("Symbols have been entered or exceeding permissible
- limits. Enter a valid value: ");
- isIncorrect = true;
- }
- if ((!isIncorrect) && (num < 0))
- {
- System.err.print("A number less than two was entered. Enter a valid
- value: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return num;
- }
- public static int countRoot(int num) {
- return (int)Math.round(Math.cbrt((double)num));
- }
- public static int showCombinations(int MAX, int num) {
- int x;
- int y;
- int z;
- int k;
- k = 0;
- MAX++;
- for (x = 0; x < MAX; x++) {
- for (y = 0; y < MAX; y++) {
- for (z = 0; z < MAX; z++) {
- if ((x * x * x) + (y * y * y) + (z * z * z) == num) {
- System.out.println(x + " " + y + " " + z);
- k++;
- }
- }
- }
- }
- return k;
- }
- static void showResult(int k) {
- if (k != 0) {
- System.out.print("Finite number of combinations: " + k);
- }
- else {
- System.out.print("There are no combinations.");
- }
- }
- static void whatDoesTheProgramDo() {
- System.out.println("The program indicates all triples of numbers X, Y, Z,
- that satisfy the condition: X^3 + Y^3 + Z^3 = Num.");
- }
- public static void main(String[]args) {
- int num;
- int x;
- int y;
- int z;
- int k;
- int MAX;
- whatDoesTheProgramDo();
- k = 0;
- System.out.print("Enter the number: ");
- Scanner scan = new Scanner(System.in);
- num = readNumPositive();
- scan.close();
- MAX = countRoot(num);
- k = showCombinations(MAX, num);
- showResult(k);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement