Base 7 - Problem
Base 7 Number System Conversion
Given an integer
In the base 7 number system, we use digits 0 through 6 to represent numbers. For example:
• Decimal 7 becomes
• Decimal 49 becomes
Your task is to implement the conversion algorithm that works for both positive and negative integers.
Given an integer
num, convert it to its base 7 representation and return it as a string.In the base 7 number system, we use digits 0 through 6 to represent numbers. For example:
• Decimal 7 becomes
"10" in base 7• Decimal 49 becomes
"100" in base 7Your task is to implement the conversion algorithm that works for both positive and negative integers.
Input & Output
example_1.py — Basic Positive Number
$
Input:
num = 100
›
Output:
"202"
💡 Note:
100 in base 10 equals 2×7² + 0×7¹ + 2×7⁰ = 98 + 0 + 2 = 100, so the base 7 representation is "202"
example_2.py — Negative Number
$
Input:
num = -7
›
Output:
"-10"
💡 Note:
7 in base 10 equals 1×7¹ + 0×7⁰ = 7, so -7 becomes "-10" in base 7
example_3.py — Zero Edge Case
$
Input:
num = 0
›
Output:
"0"
💡 Note:
Zero is represented as "0" in any base system
Constraints
- -107 ≤ num ≤ 107
- The input is guaranteed to fit in a 32-bit signed integer
Visualization
Tap to expand
Understanding the Visualization
1
Start with 100
We want to convert decimal 100 to base 7
2
Divide by 7
100 ÷ 7 = 14 remainder 2 (rightmost digit)
3
Continue dividing
14 ÷ 7 = 2 remainder 0 (middle digit)
4
Final division
2 ÷ 7 = 0 remainder 2 (leftmost digit)
5
Read remainders
Reading from bottom to top: 2-0-2 = "202"
Key Takeaway
🎯 Key Insight: The remainders from repeated division by 7, read in reverse order, give us the base-7 representation
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code