refactor updating prime powers

This commit is contained in:
filifa 2025-10-07 18:06:24 -04:00
parent 365b396db1
commit df317e0837
1 changed files with 11 additions and 5 deletions

View File

@ -16,7 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package sieve
func primeOmegaUpdateMultiples(sieve []uint, p uint, n uint, multiplicity bool) {
func primeOmegaUpdateMultiples(sieve []uint, p uint, n uint) {
for q := p; ; q *= p {
// omega(a*b) = omega(a) + omega(b) if gcd(a,b) = 1
for i := 2 * q; i < n; i += q {
@ -28,10 +28,12 @@ func primeOmegaUpdateMultiples(sieve []uint, p uint, n uint, multiplicity bool)
if p*q >= n {
break
}
if multiplicity {
sieve[p*q] = 1 + sieve[q]
}
}
func updatePowers(sieve []uint, p uint, n uint) {
for q := p; p*q < n; q *= p {
sieve[p*q] = 1 + sieve[q]
}
}
@ -54,7 +56,11 @@ func PrimeOmega(n uint, multiplicity bool, buflen uint) chan uint {
}
sieve[i] = 1
primeOmegaUpdateMultiples(sieve, i, n, multiplicity)
if multiplicity {
updatePowers(sieve, i, n)
}
primeOmegaUpdateMultiples(sieve, i, n)
ch <- sieve[i]
}
}()