Skip to content

Commit 3687447

Browse files
committed
Faster two squares
1 parent f00a116 commit 3687447

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

cp-algo/number_theory/two_squares.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace cp_algo::math {
3939
cnt[p]++;
4040
}
4141
// 1, -1, i, -i
42-
std::vector<gaussint> res = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
42+
std::vector<gaussint> res = {1};
4343
for(auto [p, c]: cnt) {
4444
std::vector<gaussint> nres;
4545
if(p % 4 == 3) {
@@ -67,8 +67,12 @@ namespace cp_algo::math {
6767
}
6868
std::vector<gaussint> nres;
6969
for(auto p: res) {
70-
if(p.real() >= 0 && p.imag() >= 0) {
71-
nres.push_back(p);
70+
while(p.real() < 0 || p.imag() < 0) {
71+
p *= gaussint(0, 1);
72+
}
73+
nres.push_back(p);
74+
if(!p.real() || !p.imag()) {
75+
nres.emplace_back(p.imag(), p.real());
7276
}
7377
}
7478
return nres;

0 commit comments

Comments
 (0)