lamagna_CAS_ch_3.ipynb
Download Notebook
In [41]:
struct MyBigInt
    sign::Symbol
    base::Int
    digits::Vector{Int64}
end

p1 = MyBigInt(:+, 1000, Vector([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0]))
MyBigInt(:+, 1000, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0])
Figure 1: ?(caption)
In [44]:
# example value in Julia's Base.BigInt
n::BigInt = 1234567890 

# some functions to test my implementation
function to_base_big(n::MyBigInt)
    ret::BigInt = 0
    for i in 1:length(n.digits)
        ret += BigInt(n.digits[i]) * n.base ^ (i - 1) 
    end
    return ret
end

function from_base_big(n::BigInt; base::Int64 = 1000)
    ret::MyBigInt = MyBigInt(:+, base, [])
    while n > 0
        push!(ret.digits, mod(n, ret.base))
        n = div(n, ret.base)
    end
    return ret
end

# round trip test
to_base_big(from_base_big(n))
1234567890
Figure 2: ?(caption)
In [38]:
1234567890
In [39]:
div(n, 1000)
1234567
In [40]:
1234567890
In [16]:
from_base_big(n)
MyBigInt(:+, 1000, [890, 567, 234, 1])
In [12]:
ret::BigInt = 0
for i in 1:length(n.digits)
    ret += BigInt(n.digits[end - i]) * n.base * (i - 1) 
end
ErrorException: type BigInt has no field digits
In [10]:
for i in 10:-1:1
    println(i)
end
10
9
8
7
6
5
4
3
2
1