Skip to content

Instantly share code, notes, and snippets.

@js51
Created February 22, 2020 00:50
Show Gist options
  • Select an option

  • Save js51/56fc6d5742bc52a8045f63e247309a21 to your computer and use it in GitHub Desktop.

Select an option

Save js51/56fc6d5742bc52a8045f63e247309a21 to your computer and use it in GitHub Desktop.
Convert an integer to a string representation in base b
def string(i, b):
"""represent any integer i as a base b string where 2<=b<=10"""
import math
start = b**int(math.log(i,b))
def _string(i,b,val):
digit = int(i/val)
return str(digit) + (_string(i-digit*val, b, val/b) if i-digit != 0 else "")
return _string(i,b,start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment