- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Given an integer 'n', check if it is a power of 3, and return True, otherwise False.
When it is required to check if a given variable is of power 3, a method named ‘check_power_of_3’ is defined that takes an integer as parameter. The modulus operator and the ‘//’ operator is used to check for the same and return True or False depending on the output.
Example
Below is a demonstration of the same
def check_power_of_3(my_val): if (my_val == 0): return False while (my_val != 1): if (my_val % 3 != 0): return False my_val = my_val // 3 return True my_num = 81 print("The number to be checked is : ") print(my_num) if(check_power_of_3(my_num)): print(my_num, 'is a power of 3..') else: print(my_num, 'is not a power of 3..')
Output
The number to be checked is : 81 81 is a power of 3..
Explanation
A method named ‘check_power_of_3’ is defined that takes the number as a parameter.
If this value is 0, False is returned.
If it is not equal to 1, then the modulus operator is used with this integer to check if it returns 0, if not, it returns False.
Otherwise, the value is operated with 3 using the ‘//’ operator.
Outside the method, the number is defined and is displayed on the console.
The method is called by passing this number as a parameter.
The relevant output is displayed on the console.
- Related Articles
- Python - Given an integer 'n', check if it is a power of 3, and return True, otherwise False.
- Python - Given an integer 'n', check if it is a power of 4, and return True, otherwise False.
- Magnesium is a non-combustible substance. (True or False and if it is false give reason).
- Check if given number is a power of d where d is a power of 2 in Python
- If a number is divisible by 3 it must be divisible by 9. (True/False)
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Find whether a given integer is a power of 3 or not in C++
- Python - Given an integer list, find the third maximum number if it exists
- How do I check if raw input is integer in Python 3?
- Check if any permutation of N equals any power of K in Python
- Check if n is divisible by power of 2 without using arithmetic operators in Python
- Return true or false in a MySQL select if another field contains a string?
- For subtraction, we add the additive inverse of the integer that is being subtracted, to the other integer is it true or false?
- Check if bitwise AND of any subset is power of two in Python
- $n^2 - 1$ is divisible by 8, if $n$ is(A) an integer(B) a natural number(C) an odd integer(D) an even integer
