DevPath

← All problems

Convert to Base 7

Easy math · strings

Description

Return the base-7 representation of a non-negative integer as a string.

Constraints

0 <= n <= 10^7

Examples

Input: n = 100

Output: "202"

Hints (1)
  • Repeatedly take the remainder modulo 7.

Solutions

Repeated division. · Time O(log n) · Space O(1)

func convertToBase7(n int) string {
    if n == 0 {
        return "0"
    }
    result := ""
    for n > 0 {
        result = string(rune('0'+n%7)) + result
        n /= 7
    }
    return result
}

Create a free account to submit solutions, track progress and save problems.

Create account