Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Gradient{
- private static long c;
- private static long i_poss;
- private static long upper_bound_real = 0;
- private static long bound_change = 0;
- private static long highest_lower = 0;
- private static long i;
- private static long d;
- private static long n;
- private static long o;
- private static long x;
- private static long j;
- private static long real_a;
- private static long real_b;
- private static long real_i;
- private static long real_j;
- private static double multiplier;
- private static long test = 0;
- private static boolean changed = false;
- public static void main(String[] args){
- Scanner input = new Scanner(System.in);
- System.out.println("What is your c value?");
- c = input.nextLong();
- //System.out.println("What is the real a value (the smaller of the two)?");
- //real_a = input.nextLong();
- //System.out.println("What is the real b value?");
- //real_b = input.nextLong();
- //real_j = (real_b - real_a)/2;
- //real_i = real_a + real_j;
- i_poss_create();
- //multiplier = (double)real_i / (double)i_poss;
- upper_bound_real = (long) (Math.ceil((double)((c-8)/6))) + 5;
- long mid_index = (long) (Math.ceil(((upper_bound_real)/2)));
- System.out.println("Highest possible i = " + i_poss);
- //System.out.println("Multiplier = " + multiplier);
- long correct_i = binary_search(mid_index, upper_bound_real, 0);
- long a = correct_i - j;
- long b = correct_i + j;
- System.out.println("For c = " + c + ", i = " + correct_i);
- System.out.println("For c = " + c + ", a = " + a + " and b = " + b);
- }
- public static void i_poss_create(){
- long iMax = (long) (Math.ceil((double)((c-8)/6))) + 5;
- i_poss = iMax;
- }
- public static long binary_search(long mid_index, long upper_bound, long lower_bound){
- while(test != c){
- if(upper_bound < lower_bound || ((upper_bound-mid_index == 1) && (mid_index - lower_bound == 1))){
- //return i_poss;
- /*
- long thing = (long)((double)upper_bound_real*0.04248);
- j = (long)Math.floor((long)Math.sqrt((double)((thing*thing)-c)));
- System.out.println("Cop out: returning " + thing + ", j = " + j);
- System.out.println("i*i = " + (thing*thing) + ", c = " + c);
- return thing;*/
- lower_bound = upper_bound+3;
- upper_bound = upper_bound-4;
- if(c < 1000){
- long temp = upper_bound;
- upper_bound = lower_bound;
- lower_bound = temp;
- }
- }
- System.out.println("Upper = " + upper_bound + ", middle = " + mid_index + ", lower = " + lower_bound);
- changed = false;
- i = mid_index;
- d = (long) Math.floor(Math.sqrt((double) c));
- n = i - d;
- o = i * i;
- if(o < c){
- //then it needs to check everywhere above this point
- highest_lower = mid_index;
- lower_bound = mid_index+1;
- mid_index = lower_bound + ((upper_bound - lower_bound)/2);
- changed = true;
- }
- x = (long) (Math.floor(Math.sqrt((double)(o-c)))) - n;
- j = x + n;
- test = (i-j) * (i+j);
- System.out.println("test = " + test + "\ti = " + i + "\tc = " + c + "\thighest lower = " + highest_lower);
- if(test != c){
- //then it needs to check everywhere below this point
- if(!(changed)){
- upper_bound = mid_index;
- mid_index = upper_bound - ((upper_bound - lower_bound)/2);
- if(mid_index == upper_bound){
- bound_change++;
- mid_index = upper_bound_real - bound_change;
- upper_bound = upper_bound_real;
- }
- }
- } else {
- System.out.println("i = " + i + ", lower_bound = " + lower_bound);
- return i;
- }
- }
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment