Given an integer num, return a string of its base 7 representation.

Example 1:

Input: num = 100 Output: "202"

Example 2:

Input: num = -7 Output: "-10"

Constraints:

  • -107 <= num <= 107

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: num = 100
    character(len=:), allocatable :: str

    str = base_7_repr(num)
    write (unit=*, fmt='(A)') trim(str)

contains

    function base_7_repr(n) result(res)
        implicit none
        integer, intent(in) :: n
        character(len=:), allocatable :: res

        character(len=30), parameter :: digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        integer :: i, sign

        if (n == 0) then
            allocate (character(len=1) :: res)
            res = '0'
            return
        end if

        sign = 1
        if (n < 0) then
            sign = -1
        end if

        i = abs(n)
        do while (i /= 0)
            allocate (character(len=len(res)+1) :: res)
            res = digits(mod(i, 7)+1) // res
            i = i / 7
        end do

        if (sign < 0) then
            allocate (character(len=len(res)+1) :: res)
            res = '-' // res
        end if

    end function base_7_repr

end program main
Compiled
Executed
Correct
[SOURCE]
module base_7
    implicit none
    private
    public :: to_base_7
contains
    function to_base_7(num) result(str)
        integer, intent(in) :: num
        character(len=20) :: str

        if (num == 0) then
            str = '0'
        else
            str = ''
            do while (num /= 0)
                str = char(ichar('0') + mod(num, 7)) // str
                num = num / 7
            end do
        end if
    end function to_base_7
end module base_7

program main
    use base_7
    implicit none
    integer :: num

    num = 100
    print *, to_base_7(num)

    num = -7
    print *, to_base_7(num)
end program main
[/SOURCE]
[TESTS]
num = 100
call assert(to_base_7(num) == "202")

num = -7
call assert(to_base_7(num) == "-10")
[/TESTS]
🌐 Data from online sources
def convert_to_base7(num: int) -> str:
    if num == 0:
        return "0"
    result = ""
    negative = num < 0
    if negative:
        num *= -1
    while num > 0:
        result = str(num % 7) + result
        num //= 7
    return "-" + result if negative else result

The algorithm first checks if the input number is 0, and if so, it simply returns the base-7 representation as "0". If the number is negative, it keeps track of the sign and converts the number to positive. Then, it iteratively divides the number by 7 (integer division) and prepends the remainder (modulus) to the result string. Finally, if the input number was negative, it adds the minus sign back to the result string. The same algorithm is implemented in C++, Java, Python, and JavaScript with minor syntax adjustments.

🌐 Data from online sources
std::string convertToBase7(int num) {
    if (num == 0) return "0";
    std::string result = "";
    bool negative = num < 0;
    if (negative) num *= -1;
    while (num > 0) {
        result = std::to_string(num % 7) + result;
        num /= 7;
    }
    return negative ? "-" + result : result;
}

The algorithm first checks if the input number is 0, and if so, it simply returns the base-7 representation as "0". If the number is negative, it keeps track of the sign and converts the number to positive. Then, it iteratively divides the number by 7 (integer division) and prepends the remainder (modulus) to the result string. Finally, if the input number was negative, it adds the minus sign back to the result string. The same algorithm is implemented in C++, Java, Python, and JavaScript with minor syntax adjustments.