Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Discrete calculations should not be done using floating-point math. You should be suspicious of any results contaminated by floating-point math. The way to compute the cube of an integer is n * n * n. For larger powers, do repeated squaring do repeated squaring, or use BigInteger.pow() (which is slower, but immune to overflow).

Discrete calculations should not be done using floating-point math. You should be suspicious of any results contaminated by floating-point math. The way to compute the cube of an integer is n * n * n. For larger powers, do repeated squaring, or use BigInteger.pow() (which is slower, but immune to overflow).

Discrete calculations should not be done using floating-point math. You should be suspicious of any results contaminated by floating-point math. The way to compute the cube of an integer is n * n * n. For larger powers, do repeated squaring, or use BigInteger.pow() (which is slower, but immune to overflow).

BigInteger immune to overflow
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Discrete calculations should not be done using floating-point math. You should be suspicious of any results contaminated by floating-point math. The way to compute the cube of an integer is n * n * n. For larger powers, do repeated squaring, or use BigInteger.pow() (which is probably slower, but immune to overflow).

Discrete calculations should not be done using floating-point math. You should be suspicious of any results contaminated by floating-point math. The way to compute the cube of an integer is n * n * n. For larger powers, do repeated squaring, or use BigInteger.pow() (which is probably slower).

Discrete calculations should not be done using floating-point math. You should be suspicious of any results contaminated by floating-point math. The way to compute the cube of an integer is n * n * n. For larger powers, do repeated squaring, or use BigInteger.pow() (which is slower, but immune to overflow).

Replaced Math.pow()
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
import java.util.ArrayList;
public class Cubes {
 public static void main(String[] args) {
 new Cubes().find();
 }
 private void find()
 {
 ArrayList<Integer> sum = new ArrayList<Integer>();
 ArrayList<Integer> x = new ArrayList<Integer>();
 ArrayList<Integer> y = new ArrayList<Integer>();
 int a, b, c, d;
 sum.add((int)(Math.pow(2,3.0 * 2 * 2)+Math.pow + (1,3.0) * 1 * 1));
 x.add(1);
 y.add(2);
 int i = 4;
 while(true)
 {
 for(int j = 1; j < i/2 +1; j++)
 {
 int k=i-j;
 int cubeSum = (int)(Math.pow(k,3.0 * k * k)+Math.pow + (j,3.0) * j * j);
 for (int l = 0; l < sum.size(); l++)
 {
 a = (int)x.get(l);
 b = (int)y.get(l);
 if(cubeSum == sum.get(l))
 {
 if (a != j && a != k && b != j && b != k)
 {
 c=j;
 d=k;
 System.out.println("The first positive integer "
 + "expressible as the sum\nof 2 different positive"
 + " cubes in 2 different ways\nis "
 + cubeSum + " = " +a+"^3 + "+b+"^3 = "
 + c+"^3 + "+d+"^3");
 return;
 }
 }
 }
 sum.add(cubeSum);
 x.add(j);
 y.add(k);
 }
 i++;
 }
 }
}
import java.util.ArrayList;
public class Cubes {
 public static void main(String[] args) {
 new Cubes().find();
 }
 private void find()
 {
 ArrayList<Integer> sum = new ArrayList<Integer>();
 ArrayList<Integer> x = new ArrayList<Integer>();
 ArrayList<Integer> y = new ArrayList<Integer>();
 int a, b, c, d;
 sum.add((int)(Math.pow(2,3.0)+Math.pow(1,3.0)));
 x.add(1);
 y.add(2);
 int i = 4;
 while(true)
 {
 for(int j = 1; j < i/2 +1; j++)
 {
 int k=i-j;
 int cubeSum = (int)(Math.pow(k,3.0)+Math.pow(j,3.0));
 for (int l = 0; l < sum.size(); l++)
 {
 a = (int)x.get(l);
 b = (int)y.get(l);
 if(cubeSum == sum.get(l))
 {
 if (a != j && a != k && b != j && b != k)
 {
 c=j;
 d=k;
 System.out.println("The first positive integer "
 + "expressible as the sum\nof 2 different positive"
 + " cubes in 2 different ways\nis "
 + cubeSum + " = " +a+"^3 + "+b+"^3 = "
 + c+"^3 + "+d+"^3");
 return;
 }
 }
 }
 sum.add(cubeSum);
 x.add(j);
 y.add(k);
 }
 i++;
 }
 }
}
import java.util.ArrayList;
public class Cubes {
 public static void main(String[] args) {
 new Cubes().find();
 }
 private void find()
 {
 ArrayList<Integer> sum = new ArrayList<Integer>();
 ArrayList<Integer> x = new ArrayList<Integer>();
 ArrayList<Integer> y = new ArrayList<Integer>();
 int a, b, c, d;
 sum.add((2 * 2 * 2) + (1 * 1 * 1));
 x.add(1);
 y.add(2);
 int i = 4;
 while(true)
 {
 for(int j = 1; j < i/2 +1; j++)
 {
 int k=i-j;
 int cubeSum = (k * k * k) + (j * j * j);
 for (int l = 0; l < sum.size(); l++)
 {
 a = (int)x.get(l);
 b = (int)y.get(l);
 if(cubeSum == sum.get(l))
 {
 if (a != j && a != k && b != j && b != k)
 {
 c=j;
 d=k;
 System.out.println("The first positive integer "
 + "expressible as the sum\nof 2 different positive"
 + " cubes in 2 different ways\nis "
 + cubeSum + " = " +a+"^3 + "+b+"^3 = "
 + c+"^3 + "+d+"^3");
 return;
 }
 }
 }
 sum.add(cubeSum);
 x.add(j);
 y.add(k);
 }
 i++;
 }
 }
}
Avoid repetitiveness in the for-loop
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
Follow my own advice: use autoboxing
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
Note about verbosity
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /