In [1]:
# Part A
function toDecimal(integer, base)
  pos = 1
  result = 0
  while integer >= 10 ^ (pos-1)
    value = Int(round(integer%10^pos/10^(pos-1), RoundDown))
    if value < base
      result = result + value * base ^ (pos-1)
    else
      throw(ArgumentError("value higher than base"))
    end
    pos = pos+1
  end
  return result
end


Out[1]:
toDecimal (generic function with 1 method)

In [2]:
# Part B
println("Part B results")
println("5240 base 6 is in decimals ", toDecimal(5240, 6))
println("12012 base 3 is in decimals ", toDecimal(12012, 3))
println("888 base 9 is in decimals ", toDecimal(888, 9))


Part B results
5240 base 6 is in decimals 1176
12012 base 3 is in decimals 140
888 base 9 is in decimals 728


In [3]:
# Part C
function countPos(decimal, base)
  pos = 0
  while decimal / base ^ pos > 1
    pos = pos+1
  end
  return pos
end


Out[3]:
countPos (generic function with 1 method)

In [4]:
# Part C
println("Part C results")
for i = 2:9
  println("displaying 1111 in base ", i, " needs ", countPos(1111, i), " places")
end


Part C results
displaying 1111 in base 2 needs 11 places
displaying 1111 in base 3 needs 7 places
displaying 1111 in base 4 needs 6 places
displaying 1111 in base 5 needs 5 places
displaying 1111 in base 6 needs 4 places
displaying 1111 in base 7 needs 4 places
displaying 1111 in base 8 needs 4 places
displaying 1111 in base 9 needs 4 places

In [ ]: