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]:
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))
In [3]:
# Part C
function countPos(decimal, base)
pos = 0
while decimal / base ^ pos > 1
pos = pos+1
end
return pos
end
Out[3]:
In [4]:
# Part C
println("Part C results")
for i = 2:9
println("displaying 1111 in base ", i, " needs ", countPos(1111, i), " places")
end
In [ ]: