Guest User

Untitled

a guest
Feb 13th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. public class SpecialPythagoreanTriplet {
  2.  
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. int t = scan.nextInt();
  6. int n=0, i=0, j=0, a=0, b=0, c=0;
  7.  
  8. while(t!=0) {
  9. n = scan.nextInt();
  10. a=0; b=0; c=0;
  11. for(i=1; i<n; i++)
  12. for(j=(i+1); j<=n; j++) {
  13. //These two loops run to take a pair of numbers. j=(i+1) to avoid repetitions in the future.
  14. if(isSquare((i*i) + (j*j))) //check if a number is a square
  15. if(((int)Math.sqrt((i*i)+(j*j)) + i + j) == n) {
  16. a = i;
  17. b = j;
  18. c = (int)Math.sqrt((i*i)+(j*j));
  19. }
  20. }
  21. if(c==0)
  22. System.out.println(-1);
  23. else
  24. System.out.println(a*b*c);
  25. t--;
  26. }
  27. scan.close();
  28. }
  29.  
  30. static boolean isSquare(double t) {
  31. int a = (int)Math.sqrt(t);
  32. if(a*a == t)
  33. return true;
  34. else
  35. return false;
  36. }
  37. }
  38.  
  39. import java.io.*;
  40. import java.math.*;
  41.  
  42. public class Solution {
  43.  
  44. public static void main(String[] args) {
  45. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  46. try{
  47.  
  48. //precomputation
  49. int save[] = new int[3001];
  50.  
  51. for(int i=1; i<3001; i++){
  52. int iSqr = i*i;
  53. for(int j=i+1; j<3001; j++){
  54.  
  55. double x = Math.sqrt(iSqr + j*j);
  56. if(x == Math.floor(x)){
  57. int sum = i+j+(int)x;
  58. int product = i*j*(int)x;
  59. if((sum < 3001) && (product > save[sum]))
  60. save[sum] = product;
  61. }
  62. }
  63. }
  64.  
  65. int t = Integer.parseInt(stdin.readLine());
  66.  
  67. while(t-- > 0){
  68. int n = Integer.parseInt(stdin.readLine());
  69. if(save[n] == 0)
  70. System.out.println(-1);
  71. else
  72. System.out.println(save[n]);
  73. }
  74. }
  75. catch(Exception e){
  76.  
  77. }
  78. }
  79. }
Add Comment
Please, Sign In to add comment