Guest User

Untitled

a guest
Dec 31st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Gradient{
  4.  
  5. private static long c;
  6. private static long i_poss;
  7. private static long upper_bound_real = 0;
  8. private static long bound_change = 0;
  9. private static long highest_lower = 0;
  10. private static long i;
  11. private static long d;
  12. private static long n;
  13. private static long o;
  14. private static long x;
  15. private static long j;
  16. private static long real_a;
  17. private static long real_b;
  18. private static long real_i;
  19. private static long real_j;
  20. private static double multiplier;
  21. private static long test = 0;
  22. private static boolean changed = false;
  23.  
  24. public static void main(String[] args){
  25. Scanner input = new Scanner(System.in);
  26. System.out.println("What is your c value?");
  27. c = input.nextLong();
  28. //System.out.println("What is the real a value (the smaller of the two)?");
  29. //real_a = input.nextLong();
  30. //System.out.println("What is the real b value?");
  31. //real_b = input.nextLong();
  32. //real_j = (real_b - real_a)/2;
  33. //real_i = real_a + real_j;
  34. i_poss_create();
  35. //multiplier = (double)real_i / (double)i_poss;
  36. upper_bound_real = (long) (Math.ceil((double)((c-8)/6))) + 5;
  37. long mid_index = (long) (Math.ceil(((upper_bound_real)/2)));
  38.  
  39. System.out.println("Highest possible i = " + i_poss);
  40. //System.out.println("Multiplier = " + multiplier);
  41.  
  42. long correct_i = binary_search(mid_index, upper_bound_real, 0);
  43. long a = correct_i - j;
  44. long b = correct_i + j;
  45. System.out.println("For c = " + c + ", i = " + correct_i);
  46. System.out.println("For c = " + c + ", a = " + a + " and b = " + b);
  47. }
  48.  
  49. public static void i_poss_create(){
  50. long iMax = (long) (Math.ceil((double)((c-8)/6))) + 5;
  51. i_poss = iMax;
  52. }
  53.  
  54. public static long binary_search(long mid_index, long upper_bound, long lower_bound){
  55. while(test != c){
  56. if(upper_bound < lower_bound || ((upper_bound-mid_index == 1) && (mid_index - lower_bound == 1))){
  57. //return i_poss;
  58.  
  59. /*
  60. long thing = (long)((double)upper_bound_real*0.04248);
  61. j = (long)Math.floor((long)Math.sqrt((double)((thing*thing)-c)));
  62. System.out.println("Cop out: returning " + thing + ", j = " + j);
  63. System.out.println("i*i = " + (thing*thing) + ", c = " + c);
  64. return thing;*/
  65.  
  66. lower_bound = upper_bound+3;
  67. upper_bound = upper_bound-4;
  68. if(c < 1000){
  69. long temp = upper_bound;
  70. upper_bound = lower_bound;
  71. lower_bound = temp;
  72. }
  73.  
  74. }
  75. System.out.println("Upper = " + upper_bound + ", middle = " + mid_index + ", lower = " + lower_bound);
  76. changed = false;
  77. i = mid_index;
  78. d = (long) Math.floor(Math.sqrt((double) c));
  79. n = i - d;
  80. o = i * i;
  81. if(o < c){
  82. //then it needs to check everywhere above this point
  83. highest_lower = mid_index;
  84. lower_bound = mid_index+1;
  85. mid_index = lower_bound + ((upper_bound - lower_bound)/2);
  86. changed = true;
  87. }
  88. x = (long) (Math.floor(Math.sqrt((double)(o-c)))) - n;
  89. j = x + n;
  90. test = (i-j) * (i+j);
  91. System.out.println("test = " + test + "\ti = " + i + "\tc = " + c + "\thighest lower = " + highest_lower);
  92. if(test != c){
  93. //then it needs to check everywhere below this point
  94. if(!(changed)){
  95. upper_bound = mid_index;
  96. mid_index = upper_bound - ((upper_bound - lower_bound)/2);
  97. if(mid_index == upper_bound){
  98. bound_change++;
  99. mid_index = upper_bound_real - bound_change;
  100. upper_bound = upper_bound_real;
  101. }
  102. }
  103. } else {
  104. System.out.println("i = " + i + ", lower_bound = " + lower_bound);
  105. return i;
  106. }
  107. }
  108. return i;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment