🔢 Convert to Base 7 | Beats 100% in all Languages 🚀
IntuitionThe simplest way to convert a number to base 7 is by leveraging built-in conversion functions. Many languages have direct methods to convert a number t
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The simplest way to convert a number to base 7 is by leveraging built-in conversion functions. Many languages have direct methods to convert a number to a different base.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Use a built-in function (if available) to convert `num` to a `base 7` string.

2. If not available, manually implement base conversion by repeatedly dividing the number by `7` and collecting remainders.



# Complexity
- Time complexity: `O(log n)`
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Since the number is divided by 7 in each step

- Space complexity: `O(1)`
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
Constant space usage

# Code
```javascript []
/**
 * @param {number} num
 * @return {string}
 */
var convertToBase7 = function(num) {
    return num.toString(7);
};
```
``` Python3 []
import numpy as np

class Solution:
    def convertToBase7(self, num: int) -> str:
        return '-' + np.base_repr(-num, 7) if num < 0 else np.base_repr(num, 7)
```
``` C++ []
class Solution {
public:
    string convertToBase7(int num) {
        if (num == 0) return "0";
        string res;
        bool neg = num < 0;
        num = abs(num);
        while (num > 0) {
            res = to_string(num % 7) + res;
            num /= 7;
        }
        return neg ? "-" + res : res;
    }
};
```
``` Java []
class Solution {
    public String convertToBase7(int num) {
        return Integer.toString(num, 7);
    }
}
```
``` Go []
import "strconv"

func convertToBase7(num int) string {
    return strconv.FormatInt(int64(num), 7)
}
```
``` Swift []
class Solution {
    func convertToBase7(_ num: Int) -> String {
        return String(num, radix: 7)
    }
}
```
``` Dart []
class Solution {
  String convertToBase7(int num) {
    return num.toRadixString(7);
  }
}
``` Kotlin []
class Solution {
    fun convertToBase7(num: Int): String {
        return num.toString(7)
    }
}
``` Ruby []
# @param {Integer} num
# @return {String}
def convert_to_base7(num)
    num.to_s(7)
end
```
![leetcode.webp](https://assets.leetcode.com/users/images/d945006a-912d-463b-bb16-70f9ecf4f186_1742894557.4876678.webp)



['Math', 'Swift', 'C++', 'Go', 'Python3', 'JavaScript', 'Dart']