Skip to main content
Back to problems
#3602
Easy Algorithms

Hexadecimal and hexatrigesimal conversion

Math String
80.7% acceptance
Feb 25, 2026
49
0
You are given an integer n. Return the concatenation of the hexadecimal representation of n2 and the hexatrigesimal representation of n3. A hexadecimal number is defined as a base-16 numeral system that uses the digits 0 – 9 and the uppercase letters A - F to represent values from 0 to 15. A hexatrigesimal number is defined as a base-36 numeral system that uses the digits 0 – 9 and the uppercase letters A - Z to represent values from 0 to 35.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn concat_hex36(n: i32) -> String {
    let n = n as i64;
    let n2 = n * n;
    let n3 = n * n * n;
    let hex = Self::to_base(n2, 16);
    let hex36 = Self::to_base(n3, 36);
    hex + &hex36
  }

  fn to_base(mut num: i64, base: i64) -> String {
    if num == 0 { return "0".to_string(); }
    let digits: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let mut result = Vec::new();
    while num > 0 {
      result.push(digits[(num % base) as usize] as char);
      num /= base;
    }
    result.iter().rev().collect()
  }
}