Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bit_manipulation/is_K_power_of_N.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This program checks if k is a power of n.

Check failure on line 1 in bit_manipulation/is_K_power_of_N.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

bit_manipulation/is_K_power_of_N.py:1:1: N999 Invalid module name: 'is_K_power_of_N'


def is_k_power_of_n(n, k):
"""
Checks if k can be reduced to 1 by repeatedly dividing it by n.

>>> 2
>>> 8
True

>>> 1092
>>> 20,21,99,97,93,72,24,99,35,25,59,616
True

>>>12
>>>100
False
"""
if n == 1:
return k == 1
if n <= 0 or k <= 0:
return False
while k % n == 0:
k //= n
return k == 1


n = int(input("Enter the base (n): "))
k = int(input("Enter the number to check (k): "))
print(is_k_power_of_n(n, k))
Loading