fork download
  1. import java.util.*;
  2.  
  3. public class Main
  4. {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int t = sc.nextInt();
  8. while (t-- > 0) {
  9. solve(sc);
  10. }
  11. sc.close();
  12. }
  13.  
  14. public static void solve(Scanner sc)
  15. {
  16. int x=0;int max=1;
  17. x=sc.nextInt();
  18. int maxGCDPlusY = 0;
  19. int maxY = 0;
  20. for (int y = 1; y < x; y++) {
  21. int gcdPlusY = gcd(x, y) + y;
  22. if (gcdPlusY > maxGCDPlusY) {
  23. maxGCDPlusY = gcdPlusY;
  24. maxY = y;
  25. }
  26. }
  27. System.out.println(maxY);
  28. }
  29. public static int gcd(int a, int b) {
  30. while (b != 0) {
  31. int temp = b;
  32. b = a % b;
  33. a = temp;
  34. }
  35. return a;
  36. }
  37. }
  38.  
  39.  
  40.  
Success #stdin #stdout 0.14s 54704KB
stdin
7
10
7
21
100
2
1000
6
stdout
5
6
14
50
1
500
3