Guest User

Untitled

a guest
Dec 31st, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. import java.math.*;
  2. import java.util.Scanner;
  3.  
  4. //import static jan.Const.oneDigit;
  5.  
  6. public class Gradient2 {
  7.  
  8. //constants
  9. private static BigInteger zero = BigInteger.ZERO;
  10. private static BigInteger one = BigInteger.ONE;
  11. private static BigInteger two = BigInteger.valueOf(2);
  12. private static BigInteger three = BigInteger.valueOf(3);
  13. private static BigInteger four = BigInteger.valueOf(4);
  14. private static BigInteger five = BigInteger.valueOf(5);
  15. private static BigInteger six = BigInteger.valueOf(6);
  16. private static BigInteger eight = BigInteger.valueOf(8);
  17. private static BigInteger one_thousand = BigInteger.valueOf(1000);
  18. private static BigInteger highest_lower = zero;
  19. private static BigInteger c;
  20. private static BigInteger i_poss;
  21. private static BigInteger upper_bound_real = zero;
  22. private static BigInteger bound_change = zero;
  23. private static BigInteger i;
  24. private static BigInteger d;
  25. private static BigInteger n;
  26. private static BigInteger o;
  27. private static BigInteger x;
  28. private static BigInteger j;
  29. private static BigInteger correct_i;
  30. private static BigInteger test = zero;
  31.  
  32. private static boolean changed = false;
  33.  
  34. /** An easy way to implement Math.ceil is just to add one, because integer truncation
  35. * is effectively flooring the number.
  36. */
  37. public static void main(String[] args){
  38. Scanner input = new Scanner(System.in);
  39. String str;
  40.  
  41. System.out.println("What is your c value?");
  42. str = input.nextLine();
  43. c = new BigInteger(str);
  44. d = sqrt(c);
  45.  
  46. i_poss = c.subtract(eight).divide(six).add(five);
  47. upper_bound_real = i_poss;
  48.  
  49. //BigInteger mid_index = one;
  50. BigInteger mid_index = upper_bound_real.divide(two);
  51.  
  52. correct_i = binary_search(mid_index, upper_bound_real, zero);
  53. BigInteger a = correct_i.subtract(j);
  54. BigInteger b = correct_i.add(j);
  55. System.out.println("For c = " + c + ", i = " + i);
  56. System.out.println("For c = " + c + ", a = " + a + " and b = " + b);
  57. }
  58.  
  59. public static BigInteger binary_search(BigInteger mid_index,
  60. BigInteger upper_bound,
  61. BigInteger lower_bound){
  62. while (!test.equals(c)) {
  63. if (lessThan(upper_bound, lower_bound) || (((upper_bound.subtract(mid_index).equals(1)) && (mid_index.subtract(lower_bound).equals(1))))) {
  64. System.out.println("It runs here");
  65. lower_bound = upper_bound.add(three);
  66. upper_bound = upper_bound.subtract(four);
  67. if(lessThan(c, one_thousand)){
  68. BigInteger temp = upper_bound;
  69. upper_bound = lower_bound;
  70. lower_bound = temp;
  71. }
  72. }
  73.  
  74. System.out.println("Upper = " + upper_bound +
  75. ", middle = " + mid_index +
  76. ", lower = " + lower_bound);
  77.  
  78. changed = false;
  79. i = mid_index;
  80. n = i.subtract(d);
  81. o = i.multiply(i);
  82.  
  83. if (lessThan(o, c)) {
  84. //then it needs to check everywhere above this point
  85. highest_lower = mid_index;
  86. lower_bound = mid_index.add(one);
  87. mid_index = lower_bound.add((upper_bound.subtract(lower_bound)).divide(two));
  88. changed = true;
  89. }
  90.  
  91. x = (sqrt(o.subtract(c))).subtract(n);
  92. j = x.add(n);
  93. test = (i.subtract(j)).multiply(i.add(j));
  94.  
  95. if (!test.equals(c)) {
  96. //then it needs to check everywhere below this point
  97.  
  98. if (!(changed)) {
  99. upper_bound = mid_index;
  100. mid_index = upper_bound.subtract((upper_bound.subtract(lower_bound)).divide(two));
  101.  
  102. if (mid_index.equals(upper_bound)){
  103. bound_change = bound_change.add(one);
  104. mid_index = upper_bound_real.subtract(bound_change);
  105. upper_bound = upper_bound_real;
  106. }
  107. }
  108. } else {
  109. return i;
  110. }
  111. }
  112. return i;
  113. }
  114.  
  115. public static boolean greaterThan(BigInteger i, BigInteger i2) {
  116. int result = i.compareTo(i2);
  117.  
  118. return result > 0;
  119. }
  120.  
  121. public static boolean lessThan(BigInteger i, BigInteger i2) {
  122. int result = i.compareTo(i2);
  123.  
  124. return result < 0;
  125. }
  126.  
  127. public static BigInteger sqrt(BigInteger i) {
  128. BigInteger zero = BigInteger.ZERO;
  129. BigInteger one = BigInteger.ONE;
  130. BigInteger n = zero;
  131. BigInteger p = zero;
  132.  
  133. if (i.equals(zero)) {
  134. return zero;
  135. }
  136.  
  137. BigInteger high = i.shiftRight(1);
  138. BigInteger low = zero;
  139.  
  140. //high > low + 1
  141. while (greaterThan(high, low.add(one))) {
  142. //n = (high + low) >> 1;
  143. n = (high.add(low)).shiftRight(1);
  144. p = n.multiply(n);
  145.  
  146. int result = i.compareTo(p);
  147. if (result == -1) {
  148. high = n;
  149. } else if (result == 1) {
  150. low = n;
  151. } else {
  152. break;
  153. }
  154. }
  155.  
  156. if (i.equals(p)) {
  157. return n;
  158. } else {
  159. return low;
  160. }
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment