Advertisement
Salman_CUET_18

Divisor differene

Aug 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. ll divisor_difference(ll n)
  5. {
  6.     ll diff, ans = n - 1;
  7.     int sqrtN = sqrt(n) + 1;
  8.     for(ll i = 1; i <= sqrtN; i++)
  9.     {
  10.         if(n % i == 0)
  11.         {
  12.             ll j = n / i;
  13.             diff = abs(i - j);
  14.             if(diff < ans)
  15.                 ans = diff;
  16.         }
  17.     }
  18.     return ans;
  19. }
  20. int main(void)
  21. {
  22.     int t;
  23.     ll n;
  24.     cin >> t;
  25.     for(int kase = 1; kase <= t; kase++)
  26.     {
  27.         cin >> n;
  28.         printf("Case #%d: %lld\n", kase, divisor_difference(n));
  29.     }
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement