Guest User

Untitled

a guest
Jan 1st, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. public class lowest_higher{
  5. private static BigInteger zero = BigInteger.ZERO;
  6. private static BigInteger one = BigInteger.ONE;
  7. private static BigInteger two = BigInteger.valueOf(2);
  8. private static BigInteger three = BigInteger.valueOf(3);
  9. private static BigInteger four = BigInteger.valueOf(4);
  10. private static BigInteger five = BigInteger.valueOf(5);
  11. private static BigInteger six = BigInteger.valueOf(6);
  12. private static BigInteger eight = BigInteger.valueOf(8);
  13. private static int count = 0;
  14. private static boolean decided = false;
  15. private static BigInteger landed = zero;
  16. private static BigInteger a = zero;
  17. private static BigInteger b;
  18. private static BigInteger c;
  19. private static BigInteger upper_bound_real;
  20. private static BigInteger i;
  21. private static BigInteger d;
  22. private static BigInteger n;
  23. private static BigInteger o;
  24. private static BigInteger x;
  25. private static BigInteger j;
  26. private static BigInteger test = zero;
  27. private static BigInteger old_middle = zero;
  28.  
  29. public static void main(String[] args) {
  30. Scanner input = new Scanner(System.in);
  31. String str;
  32. System.out.print("c=");
  33. str = input.nextLine();
  34. c = new BigInteger(str);
  35. d = sqrt(c);
  36. upper_bound_real = c.subtract(eight).divide(six).add(three);
  37. BigInteger mid_index = upper_bound_real.divide(two);
  38. i = binary_search(mid_index, upper_bound_real, zero);
  39.  
  40. System.out.println("The lowest possible i that doesn't create an imaginary number is " + lowest_higher);
  41. o = i.pow(2);
  42. BigInteger jSquared = o.subtract(c);
  43. x = (sqrt(jSquared)).subtract(n);
  44. j = x.add(n);
  45. a = i.subtract(j);
  46. b = i.add(j);
  47. test = a.multiply(b);
  48. if(test.equals(c)){
  49. System.out.println("It is the correct i: c = " + c + ", test = " + test);
  50. } else {
  51. System.out.println("It is an incorrect i: c = " + c + ", test = " + test);
  52. }
  53. }
  54.  
  55. public static BigInteger binary_search(BigInteger mid_index, BigInteger upper_bound, BigInteger lower_bound) {
  56. while (!(decided)) {
  57. System.out.println("Upper = " + upper_bound +
  58. ", middle = " + mid_index +
  59. ", lower = " + lower_bound);
  60.  
  61. //The middle either produces an imaginary number or doesn't.
  62. //If it does, it's too small.
  63. //If it doesn't, it's too big.
  64. n = mid_index.subtract(d);
  65. o = mid_index.pow(2);
  66. old_middle = mid_index;
  67. if(lessThan(o, c)){
  68. lower_bound = mid_index.add(one);
  69. mid_index = lower_bound.add((upper_bound.subtract(lower_bound)).divide(two));
  70. } else {
  71. upper_bound = mid_index.subtract(one);
  72. mid_index = lower_bound.add((upper_bound.subtract(lower_bound)).divide(two));
  73. }
  74. if(upper_bound.equals(lower_bound)){
  75. landed = upper_bound;
  76. decided = true;
  77. }
  78. //if lower_bound > upper_bound, landed = old middle
  79. if(greaterThan(lower_bound, upper_bound)){
  80. landed = old_middle;
  81. decided = true;
  82. }
  83. }
  84.  
  85. //middle is either the highest lower (creates an imaginary number) or the lowest higher (doesn't)
  86. //for the sake of this program we want the lowest higher, because it could be the i for our c
  87. if(lessThan(landed.pow(2), c)){
  88. return landed.add(one);
  89. }
  90. return landed;
  91. }
  92.  
  93. public static BigInteger sqrt(BigInteger i) {
  94. BigInteger zero = BigInteger.ZERO;
  95. BigInteger one = BigInteger.ONE;
  96. BigInteger n = zero;
  97. BigInteger p = zero;
  98.  
  99. if (i.equals(zero)) {
  100. return zero;
  101. } else if (i.equals(one)) {
  102. return one;
  103. }
  104.  
  105. BigInteger high = i.shiftRight(1);
  106. BigInteger low = zero;
  107.  
  108. //high > low + 1
  109. while (greaterThan(high, low.add(one))) {
  110. //n = (high + low) >> 1;
  111. n = (high.add(low)).shiftRight(1);
  112. p = n.multiply(n);
  113. int result = i.compareTo(p);
  114. if (result == -1) {
  115. high = n;
  116. } else if (result == 1) {
  117. low = n;
  118. } else {
  119. break;
  120. }
  121. }
  122.  
  123. if (i.equals(p)) {
  124. return n;
  125. } else {
  126. return low;
  127. }
  128. }
  129.  
  130. public static boolean greaterThan(BigInteger i, BigInteger i2) {
  131. int result = i.compareTo(i2);
  132.  
  133. return result > 0;
  134. }
  135.  
  136. public static boolean lessThan(BigInteger i, BigInteger i2) {
  137. int result = i.compareTo(i2);
  138.  
  139. return result < 0;
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment