- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Integer to Base 3 Number in Python
Suppose we have a number n, we have to find the base 3 equivalent of this number as string.
So, if the input is like 17, then the output will be 122.
To solve this, we will follow these steps −
- if n<0:
- sign := -1
- otherwise sign := blank string
- n := |n|
- if n <3, then
- return n as string
- s := blank string
- while n is not same as 0, do
- s := string of (n mod 3) concatenate s
- n := quotient of (n / 3)
- return sign concatenate s
Let us see the following implementation to get better understanding −
class Solution: def solve(self, n): sign = '-' if n<0 else '' n = abs(n) if n < 3: return str(n) s = '' while n != 0: s = str(n%3) + s n = n//3 return sign+s ob = Solution() print(ob.solve(17))
- Base 3 to integer in Python
- Complement of Base 10 Integer in Python
- How do I check if raw input is integer in Python 3?
- Convert one base number system to another base system in MySQL
- Python program to reverse bits of a positive integer number?
- Roman to Integer in Python
- 3-digit Osiris number in C?
- Program to convert Linked list representing binary number to decimal integer in Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Convert decimal integer to hexadecimal number in Java
- C++ Pandigital Number in a Given Base
- Binary list to integer in Python