Replace heuristic sqrt with log2-based estimate
This commit is contained in:
1 parent
eeeff42be6
commit
5a9e103bd5
2 files changed
+26
-32
No files matched your search
@@ -311,31 +311,28 @@ Cleanup:
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong bit = 1UL << 62;
|
int log2 = BitOperations.Log2(n);
|
||||||
while (bit > n)
|
ulong estimate = 1UL << ((log2 + 1) >> 1);
|
||||||
|
ulong quotient = n / estimate;
|
||||||
|
|
||||||
|
while (estimate > quotient)
|
||||||
{
|
{
|
||||||
bit >>= 2;
|
estimate = (estimate + quotient) >> 1;
|
||||||
|
quotient = n / estimate;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong result = 0UL;
|
while (true)
|
||||||
ulong remainder = n;
|
|
||||||
while (bit != 0)
|
|
||||||
{
|
{
|
||||||
ulong candidate = result + bit;
|
ulong nextEstimate = estimate + 1UL;
|
||||||
if (remainder >= candidate)
|
if (nextEstimate > n / nextEstimate)
|
||||||
{
|
{
|
||||||
remainder -= candidate;
|
break;
|
||||||
result = (result >> 1) + bit;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result >>= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bit >>= 2;
|
estimate = nextEstimate;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return estimate;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ReadOnlySpan<uint> GetCombinedDivisors(byte nMod10)
|
private static ReadOnlySpan<uint> GetCombinedDivisors(byte nMod10)
|
||||||
|
|||||||
@@ -436,31 +436,28 @@ Cleanup:
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong bit = 1UL << 62;
|
int log2 = BitOperations.Log2(n);
|
||||||
while (bit > n)
|
ulong estimate = 1UL << ((log2 + 1) >> 1);
|
||||||
|
ulong quotient = n / estimate;
|
||||||
|
|
||||||
|
while (estimate > quotient)
|
||||||
{
|
{
|
||||||
bit >>= 2;
|
estimate = (estimate + quotient) >> 1;
|
||||||
|
quotient = n / estimate;
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong result = 0UL;
|
while (true)
|
||||||
ulong remainder = n;
|
|
||||||
while (bit != 0)
|
|
||||||
{
|
{
|
||||||
ulong candidate = result + bit;
|
ulong nextEstimate = estimate + 1UL;
|
||||||
if (remainder >= candidate)
|
if (nextEstimate > n / nextEstimate)
|
||||||
{
|
{
|
||||||
remainder -= candidate;
|
break;
|
||||||
result = (result >> 1) + bit;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result >>= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bit >>= 2;
|
estimate = nextEstimate;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return estimate;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ReadOnlySpan<byte> GetGroupBEndingOrder(byte nMod10) => nMod10 switch
|
private static ReadOnlySpan<byte> GetGroupBEndingOrder(byte nMod10) => nMod10 switch
|
||||||
|
|||||||
Reference in new issue
Block a user