
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 −
Example
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))
Input
17
Output
122
- Related Articles
- Base 3 to integer in Python
- Complement of Base 10 Integer in Python
- Convert the string of any base to integer in JavaScript
- Python program to reverse bits of a positive integer number?
- Program to convert Linked list representing binary number to decimal integer in Python
- Roman to Integer in Python
- Convert one base number system to another base system in MySQL
- How to convert a string of any base to an integer in JavaScript?
- Program to find number of moves to win deleting repeated integer game in Python
- Binary list to integer in Python
- Integer to English Words in Python
- Convert Tuple to integer in Python
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- JAVA Program To Convert Roman Number to Integer Number
- Python Pandas - Get the Integer number of levels in this MultiIndex

Advertisements