Advertisement
satishfrontenddev5

Untitled

Jan 6th, 2024
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ou are given a X positive integer. You need to find the N-th root of X.
  3.  
  4. If the N-th root is in decimal you need to print its floor value e.g, floor(3.23) = 3.
  5.  
  6. Input format
  7. First line contains T i.e, the number of test cases.
  8.  
  9. Each test case has a single line that contains two positive integers, X and N.
  10.  
  11. Output format
  12. For each test case print the floor(N-th root of X) in a new line.
  13.  
  14. Sample Input 1
  15. 4
  16.  
  17. 4 2
  18.  
  19. 9 3
  20.  
  21. 11 2
  22.  
  23. 16 4
  24.  
  25. Sample Output 1
  26. 2
  27.  
  28. 2
  29.  
  30. 3
  31.  
  32. 2
  33.  
  34. Explanation
  35. For X = 4 and N = 2 => (4) ^ (1/2) = 2. And floor(2) = 2
  36.  
  37. For X = 9 and N = 3 => (9) ^ (1/3) = 2.08. And floor(2.08) = 2
  38.  
  39. For X = 11 and N = 2 => (11) ^ (1/2) = 3.316. And floor(3.316) = 3
  40.  
  41. For X = 16 and N = 4 => (16) ^ (1/4) = 2. And floor(2) = 2
  42.  
  43. Constraints
  44. 1 <= T <= 10 ^ 4
  45.  
  46. 1 <= X <= 10 ^ 9
  47.  
  48. 1 <= N <= 10 ^ 8
  49. */
  50. /**
  51.  * @param {number} x
  52.  * @param {number} n
  53.  * @return {number}
  54.  */
  55. function nthRoot(x, n) {
  56.     let ans;
  57.     let prevAns=1;
  58.     if(x==1)
  59.     return x;
  60.     if(n==1)
  61.     return x;
  62.     for(let m=1;m<x;m++){
  63.         let currentRoot=Math.pow(m,n);
  64.         if(currentRoot<=x){
  65.             prevAns=m;
  66.         }
  67.         else
  68.         break;
  69.        
  70.     }
  71.     return Math.floor(prevAns)
  72. }
  73.  
  74. function main() {
  75.     let t = parseInt(readLine(), 10)
  76.     while (t--){
  77.         const [x, n] = readIntArr()
  78.         const result = nthRoot(x, n);
  79.         console.log(result)
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement