Guest User

Untitled

a guest
Sep 10th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. Link = https://www.hackerrank.com/challenges/sherlock-and-squares/problem
  2.  
  3. Solution = ( I only had to modify squares method)
  4.  
  5. import java.io.*;
  6. import java.math.*;
  7. import java.security.*;
  8. import java.text.*;
  9. import java.util.*;
  10. import java.util.concurrent.*;
  11. import java.util.regex.*;
  12.  
  13. public class Solution {
  14. static boolean isPerfect(int n)
  15. {
  16.  
  17.  
  18. if ((Math.sqrt(n) - Math.floor(Math.sqrt(n)))== 0)
  19. {
  20. return true;
  21. }
  22. else
  23. return false;
  24. }
  25.  
  26. // Complete the squares function below.
  27. static int squares(int a, int b) {
  28. int n=b;
  29. int count=0;
  30. int lessn;
  31. while(n>a)
  32. {
  33. //count++;
  34. if(isPerfect(n))
  35. {
  36. count++;
  37. n=(int)Math.sqrt(n)-1;
  38. }
  39. else
  40. {
  41. n=(int)Math.sqrt(n);
  42. n=(int)Math.floor(n);
  43.  
  44. }
  45. n=(int)Math.pow(n,2);
  46.  
  47. }
  48. return count;
  49.  
  50.  
  51.  
  52.  
  53. }
  54.  
  55. private static final Scanner scanner = new Scanner(System.in);
  56.  
  57. public static void main(String[] args) throws IOException {
  58. BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
  59.  
  60. int q = scanner.nextInt();
  61. scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  62.  
  63. for (int qItr = 0; qItr < q; qItr++) {
  64. String[] ab = scanner.nextLine().split(" ");
  65.  
  66. int a = Integer.parseInt(ab[0]);
  67.  
  68. int b = Integer.parseInt(ab[1]);
  69.  
  70. int result = squares(a, b);
  71.  
  72. bufferedWriter.write(String.valueOf(result));
  73. bufferedWriter.newLine();
  74. }
  75.  
  76. bufferedWriter.close();
  77.  
  78. scanner.close();
  79. }
  80. }
  81.  
Add Comment
Please, Sign In to add comment