/*
In this Magma script, we implement basic operations +, -, times, ... for non-negative integers
when we use the positional numeral system with b = 2^10 = 1024
Later, we could also try the base 2^16 = 65,536
We represent non-negative integers using sequences. For example, a sequence
[a_0, a_1, ..., a_{n-1}] (with 0 < = a_j < = b-1 ) represents
a_0 + a_1 b + a_2 b^2 + ... + a_{n-1} b^{n-1}
The function Intseq( ,b) and Seqint( ,b) are used to convert an nonnegative integer
to a sequence and back.
*/
b := 2^10;
//tested
MySum_prim := function(s,t)
/*
implementation of the addition (if we work with base = b);
here, we assume that s and t have the same length
*/
m := Max(#s, #t);
out := [ ];
carry := 0; //here, will store the "carry-over"
for j in [1..m] do
d := s[j] + t[j] + carry;
Append(~out, d mod b);
if d ge b then
carry := 1;
else
carry := 0;
end if;
end for;
if carry eq 1 then
Append(~out, 1);
end if;
return out;
end function;
//please try to test this function
MySum := function(s,t)
/*
implementation of the addition (if we work with base = b);
this one is more "advanced"; s and t may have different lengths
well... the code requires more work...
*/
m := Max(#s, #t);
if #s lt m then
s1 := s cat [0 : i in [1..(m - #s)]];
else
s1 := s;
end if;
if #t lt m then
t1 := t cat [0 : i in [1..(m - #t)]];
else
t1 := t;
end if;
out := [ ];
carry := 0; //here, will store the "carry-over"
for j in [1..m] do
d := s1[j] + t1[j] + carry;
Append(~out, d mod b);
if d ge b then
carry := 1;
else
carry := 0;
end if;
end for;
if carry eq 1 then
Append(~out, 1);
end if;
return out;
end function;