Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static final BigInteger _3=BigInteger.valueOf(3);
- public static BigInteger cbrt(BigInteger num)
- {
- BigInteger root=BigInteger.ZERO.setBit(num.bitLength()/3),temp;
- do {
- temp=root;
- root=temp.add(temp).add(num.divide(temp.multiply(temp))).divide(_3);
- } while(!root.equals(temp));
- }
- public static final BigDecimal _3=BigDecimal.valueOf(3);
- public static final int UP=BigDecimal.ROUND_HALF_UP;
- public static BigInteger cbrt(BigInteger num)
- {
- BigDecimal numD=new BigDecimal(num),temp;
- BigInteger root=BigInteger.ZERO.setBit(num.bitLength()/3);
- do {
- temp=new BigDecimal(root);
- root=temp.add(temp).add(numD.divide(temp.multiply(temp),UP)).divide(_3,UP).toBigInteger();
- } while(!root=temp.toBigInteger());
- }
- public static final BigInteger _3=BigInteger.valueOf(3);
- public static BigInteger cbrt(BigInteger num)
- {
- BigInteger root=BigInteger.ZERO.setBit(num.bitLength()/3),temp;
- do {
- temp=root;
- root=temp.add(temp).add(num.divide(temp.multiply(temp))).divide(_3);
- } while(root.compareTo(temp)>0);
- }
- public static final BigInteger _3=BigInteger.valueOf(3);
- public static BigInteger cbrt(BigInteger n)
- {
- BigInteger root=BigInteger.ZERO.setBit(n.bitLength()/3),t1,t2,t3;
- t1=t2=t3=BigInteger.ZERO;
- do {
- t3=t2;t2=t1;t1=root;
- root=t1.add(t1).add(n.divide(t1.multiply(t1))).divide(_3);
- } while(!root.equals(t1.add(t2).add(t3).divide(_3)));
- return root;
- }
- private static final BigInteger THREE = BigInteger.valueOf(3);
- private static BigInteger cubeRoot(BigInteger n) {
- // Using Newton's method, we approximate the cube root
- // of n by the sequence:
- // x_{i + 1} = frac{1}{3} left( frac{n}{x_i^2} + 2 x_i right).
- // See http://en.wikipedia.org/wiki/Cube_root#Numerical_methods.
- //
- // Implementation based on Section 1.7.1 of
- // "A Course in Computational Algebraic Number Theory"
- // by Henri Cohen.
- BigInteger x = BigInteger.ZERO.setBit(n.bitLength() / 3 + 1);
- BigInteger y = BigInteger.ZERO;
- while (true) {
- y = x.shiftLeft(1).add(n.divide(x.multiply(x))).divide(THREE);
- if (y.compareTo(x) >= 0) {
- break;
- }
- x = y;
- }
- return x;
- }
- for (int i = 1; i < Integer.MAX_VALUE; i++) {
- // Report progress.
- if (i % 1000000 == 0) {
- System.out.printf("%d%n", i);
- }
- BigInteger n = BigInteger.valueOf(i);
- BigInteger m = cubeRoot(n);
- BigInteger lower = m.pow(3);
- BigInteger upper = m.add(BigInteger.ONE).pow(3);
- if (lower.compareTo(n) <= 0 && n.compareTo(upper) < 0) {
- continue;
- }
- System.err.printf("Error for input %s: Got %s%n", n, m);
- System.err.printf("Expected m^3 <= %s < (m + 1)^3%n", n);
- System.err.printf("But m^3 = %s, (m + 1)^3 = %s%n", lower, upper);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement