algorithm - Understanding the code for finding the permutation of numbers x and y,(less than n) such that x*y is a perfect square and y -x is maximum (x < y) -


i read function in programming blog aforementioned task,but i'm unable understand . (also x*y should maximum).also preference maximizing x*y more y-x .

       long long ans = 0;         int x,y;      for( = 2;i <= n / i;++i){         int k = n / (i * i);         int y = k *i*i;         int x = k * (i - 1) * (i - 1);          if((long long)x * y > ans){             ans = (long long)x * y;             x = x;             y = y;         }     } 

  1. i <= n / i ensures values x , y less n, derived square of i, inside loop.
    (k derived n , i. , x , y in turn derived k , i.)
  2. int k = n / (i * i);, int y = k * * i;, int x = k * (i - 1) * (i - 1); ensures value x * y perfect square.
    (since x * y = k * k * * * (i - 1) * (i - 1). i.e x * y perfect square of k * * (i - 1)).
    also, x less y because x derives (i - 1) whereas y i.
  3. the 3 statements in if loop @ end keep track of pair having largest x * y.

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -