Skip to content

Commit b0413e5

Browse files
authored
Add new algorithm (is_K_power_of_N)
1 parent e4e486b commit b0413e5

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

bit_manipulation/is_K_power_of_N.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,65 @@
1+
The error message `Syntax Error @ 1:1. tokenizer error: '`' is not a valid character in this position\` indicates that the Python parser is encountering an invalid character right at the beginning of the file.
2+
3+
The character causing the issue is the backtick symbol (`` ` ``) which seems to have been included at the very start of the code block. This is not a valid character to start a Python statement.
4+
5+
Here is the corrected code. It is identical to the previous version, but without the leading backtick, making it syntactically correct.
6+
7+
```python
8+
"""
9+
Author : Your Name
10+
Date : August 1, 2025
11+
12+
Task:
13+
Given two positive integers, n and k, determine if k is a power of n.
14+
15+
Implementation notes: Use a loop to repeatedly divide k by n.
16+
For a number k to be a power of n, it must be possible to reduce k
17+
to 1 by repeatedly dividing by n without any remainder.
18+
For example, 8 is a power of 2 because:
19+
8 / 2 = 4
20+
4 / 2 = 2
21+
2 / 2 = 1
22+
The final result is 1.
23+
"""
24+
25+
126
def is_k_power_of_n(n: int, k: int) -> bool:
227
"""
3-
Given two positive integers, n and k, this function determines if k
4-
is a power of n. A number k is a power of n if k = n^x for some
5-
non-negative integer x.
28+
Return True if k is a power of n or False otherwise.
629
730
>>> is_k_power_of_n(2, 8)
831
True
9-
>>> is_k_power_of_n(3, 10)
10-
False
32+
>>> is_k_power_of_n(3, 9)
33+
True
1134
>>> is_k_power_of_n(5, 5)
1235
True
13-
>>> is_k_power_of_n(10, 1000)
36+
>>> is_k_power_of_n(2, 1)
1437
True
38+
>>> is_k_power_of_n(3, 10)
39+
False
40+
>>> is_k_power_of_n(2, 6)
41+
False
1542
>>> is_k_power_of_n(4, 2)
1643
False
1744
>>> is_k_power_of_n(1, 1)
1845
True
1946
>>> is_k_power_of_n(1, 5)
2047
False
21-
>>> is_k_power_of_n(0, 16)
48+
>>> is_k_power_of_n(-2, 8)
2249
Traceback (most recent call last):
2350
...
2451
ValueError: Both n and k must be positive integers
2552
>>> is_k_power_of_n(2, -8)
2653
Traceback (most recent call last):
2754
...
2855
ValueError: Both n and k must be positive integers
29-
>>> is_k_power_of_n(2, 'a')
56+
>>> is_k_power_of_n(2.5, 8)
3057
Traceback (most recent call last):
3158
...
3259
TypeError: n and k must be integers
3360
"""
3461
if not isinstance(n, int) or not isinstance(k, int):
3562
raise TypeError("n and k must be integers")
36-
3763
if n <= 0 or k <= 0:
3864
raise ValueError("Both n and k must be positive integers")
3965

0 commit comments

Comments
 (0)