Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up CoefficientsQadic for long results #4186

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions lib/integer.gi
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ end);
InstallMethod( CoefficientsQadic, "for two integers",
true, [ IsInt, IsInt ], 0,
function( i, q )
local v;
local i1, res, l, qq, i2;
if q <= 1 then
Error("2nd argument of CoefficientsQadic should be greater than 1\n");
fi;
Expand All @@ -435,12 +435,34 @@ function( i, q )
TryNextMethod();
fi;
# represent the integer <i> as <q>-adic number
v := [];
while i > 0 do
Add( v, RemInt( i, q ) );
i := QuoInt( i, q );
od;
return v;
if i = 0 then
return [];
elif i < q then
return [i];
elif i < q^2 then
i1 := QuoInt(i, q);
return [i - i1*q, i1];
elif Log2Int(q)*100 > Log2Int(i) then
# straight forward loop for result length < 100
res := [];
while i > 0 do
i1 := QuoInt(i, q);
Add(res, i - i1*q);
i := i1;
od;
else
# divide and conquer method for large i
l := QuoInt(LogInt(i, q), 2)+1;
qq := q^l;
i2 := QuoInt(i,qq);
i1 := i - i2*qq;
res := CoefficientsQadic(i1, q);
while Length(res) < l do
Add(res, 0);
od;
Append(res, CoefficientsQadic(i2, q));
fi;
return res;
end);


Expand Down
10 changes: 10 additions & 0 deletions tst/testinstall/integer.tst
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,15 @@ Error, You cannot loop over the integer 5 did you mean the range [1..5]
gap> for x in 5 do od;
Error, You cannot loop over the integer 5 did you mean the range [1..5]

#
gap> CoefficientsQadic(0,3);
[ ]
gap> CoefficientsQadic(2,3);
[ 2 ]
gap> CoefficientsQadic(3^5*2^7,3);
[ 0, 0, 0, 0, 0, 2, 0, 2, 1, 1 ]
gap> CoefficientsQadic(3^5*2^7,2);
[ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1 ]

#
gap> STOP_TEST("integers.tst", 1);