Skip to content

Commit ff5d89e

Browse files
authored
Merge pull request codeharborhub#3682 from Ishitamukherjee2004/new-branch
Nth Root of M of gfg problem is added
2 parents 9c8f6c5 + e6dbf26 commit ff5d89e

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
id: nth-root-of-m
3+
title: Nth root of M
4+
sidebar_label: Nth-root-of-M
5+
tags:
6+
- Mathematical
7+
- Algorithm
8+
description: "This tutorial covers the solution to the Nth root of M problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
You are given 2 numbers `(n, m)`; the task is to find `n√m` (`nth` root of `m`).
12+
13+
14+
## Examples
15+
16+
**Example 1:**
17+
18+
```
19+
Input: n = 2, m = 9
20+
Output: 3
21+
Explanation: 3^2 = 9
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: n = 3, m = 9
28+
Output: -1
29+
Explanation: 3rd root of 9 is not
30+
integer.
31+
```
32+
33+
## Your Task
34+
35+
You don't need to read or print anyhting. Your task is to complete the function NthRoot() which takes n and m as input parameter and returns the nth root of m. If the root is not integer then returns -1.
36+
37+
38+
## Constraints
39+
40+
* `1 <= n <= 30`
41+
* `1 <= m <= 10^9`
42+
43+
## Problem Explanation
44+
You are given 2 numbers (n , m); the task is to find n√m (nth root of m).
45+
46+
## Code Implementation
47+
48+
<Tabs>
49+
<TabItem value="Python" label="Python" default>
50+
<SolutionAuthor name="@arunimad6yuq"/>
51+
52+
```py
53+
import math
54+
55+
n = 3
56+
m = 27
57+
result = m ** (1/n)
58+
print(result)
59+
60+
```
61+
62+
</TabItem>
63+
<TabItem value="C++" label="C++">
64+
<SolutionAuthor name="@YourUsername"/>
65+
66+
```cpp
67+
#include <cmath>
68+
#include <iostream>
69+
70+
int main() {
71+
int n = 3;
72+
int m = 27;
73+
double result = pow(m, 1.0 / n);
74+
std::cout << result << std::endl;
75+
return 0;
76+
}
77+
78+
```
79+
80+
</TabItem>
81+
82+
<TabItem value="Javascript" label="Javascript" default>
83+
<SolutionAuthor name="@Ishitamukherjee2004"/>
84+
85+
```javascript
86+
87+
function nthRoot(n, m) {
88+
return Math.pow(m, 1/n);
89+
}
90+
91+
92+
93+
```
94+
95+
</TabItem>
96+
97+
<TabItem value="Typescript" label="Typescript" default>
98+
<SolutionAuthor name="@Ishitamukherjee2004"/>
99+
100+
```typescript
101+
function nthRoot(n: number, m: number): number {
102+
return Math.pow(m, 1/n);
103+
}
104+
105+
106+
```
107+
108+
</TabItem>
109+
110+
<TabItem value="Java" label="Java" default>
111+
<SolutionAuthor name="@Ishitamukherjee2004"/>
112+
113+
```java
114+
public class Main {
115+
public static void main(String[] args) {
116+
int n = 3;
117+
int m = 27;
118+
double result = Math.pow(m, 1.0 / n);
119+
System.out.println(result);
120+
}
121+
}
122+
123+
124+
```
125+
126+
</TabItem>
127+
</Tabs>
128+
129+
130+
## Time Complexity
131+
132+
* The iterative approach has a time complexity of $O(1)$.
133+
134+
## Space Complexity
135+
136+
* The space complexity is $O(1)$ since we are using only a fixed amount of extra space.

0 commit comments

Comments
 (0)