Advertisement
Guest User

Untitled

a guest
Jul 11th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. public static final BigInteger _3=BigInteger.valueOf(3);
  2.  
  3. public static BigInteger cbrt(BigInteger num)
  4. {
  5. BigInteger root=BigInteger.ZERO.setBit(num.bitLength()/3),temp;
  6. do {
  7. temp=root;
  8. root=temp.add(temp).add(num.divide(temp.multiply(temp))).divide(_3);
  9. } while(!root.equals(temp));
  10. }
  11.  
  12. public static final BigDecimal _3=BigDecimal.valueOf(3);
  13. public static final int UP=BigDecimal.ROUND_HALF_UP;
  14.  
  15. public static BigInteger cbrt(BigInteger num)
  16. {
  17. BigDecimal numD=new BigDecimal(num),temp;
  18. BigInteger root=BigInteger.ZERO.setBit(num.bitLength()/3);
  19. do {
  20. temp=new BigDecimal(root);
  21. root=temp.add(temp).add(numD.divide(temp.multiply(temp),UP)).divide(_3,UP).toBigInteger();
  22. } while(!root=temp.toBigInteger());
  23. }
  24.  
  25. public static final BigInteger _3=BigInteger.valueOf(3);
  26.  
  27. public static BigInteger cbrt(BigInteger num)
  28. {
  29. BigInteger root=BigInteger.ZERO.setBit(num.bitLength()/3),temp;
  30. do {
  31. temp=root;
  32. root=temp.add(temp).add(num.divide(temp.multiply(temp))).divide(_3);
  33. } while(root.compareTo(temp)>0);
  34. }
  35.  
  36. public static final BigInteger _3=BigInteger.valueOf(3);
  37.  
  38. public static BigInteger cbrt(BigInteger n)
  39. {
  40. BigInteger root=BigInteger.ZERO.setBit(n.bitLength()/3),t1,t2,t3;
  41. t1=t2=t3=BigInteger.ZERO;
  42. do {
  43. t3=t2;t2=t1;t1=root;
  44. root=t1.add(t1).add(n.divide(t1.multiply(t1))).divide(_3);
  45. } while(!root.equals(t1.add(t2).add(t3).divide(_3)));
  46. return root;
  47. }
  48.  
  49. private static final BigInteger THREE = BigInteger.valueOf(3);
  50.  
  51. private static BigInteger cubeRoot(BigInteger n) {
  52. // Using Newton's method, we approximate the cube root
  53. // of n by the sequence:
  54. // x_{i + 1} = frac{1}{3} left( frac{n}{x_i^2} + 2 x_i right).
  55. // See http://en.wikipedia.org/wiki/Cube_root#Numerical_methods.
  56. //
  57. // Implementation based on Section 1.7.1 of
  58. // "A Course in Computational Algebraic Number Theory"
  59. // by Henri Cohen.
  60. BigInteger x = BigInteger.ZERO.setBit(n.bitLength() / 3 + 1);
  61. BigInteger y = BigInteger.ZERO;
  62.  
  63. while (true) {
  64. y = x.shiftLeft(1).add(n.divide(x.multiply(x))).divide(THREE);
  65. if (y.compareTo(x) >= 0) {
  66. break;
  67. }
  68.  
  69. x = y;
  70. }
  71.  
  72. return x;
  73. }
  74.  
  75. for (int i = 1; i < Integer.MAX_VALUE; i++) {
  76. // Report progress.
  77. if (i % 1000000 == 0) {
  78. System.out.printf("%d%n", i);
  79. }
  80.  
  81. BigInteger n = BigInteger.valueOf(i);
  82. BigInteger m = cubeRoot(n);
  83.  
  84. BigInteger lower = m.pow(3);
  85. BigInteger upper = m.add(BigInteger.ONE).pow(3);
  86.  
  87. if (lower.compareTo(n) <= 0 && n.compareTo(upper) < 0) {
  88. continue;
  89. }
  90.  
  91. System.err.printf("Error for input %s: Got %s%n", n, m);
  92. System.err.printf("Expected m^3 <= %s < (m + 1)^3%n", n);
  93. System.err.printf("But m^3 = %s, (m + 1)^3 = %s%n", lower, upper);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement