Sieve of Eratosthenes

struct SieveOfEratosthenes {
    primes: Vec<u64>,
}

impl SieveOfEratosthenes {
    fn new(v: usize) -> Self {
        let mut is_prime = vec![true; v + 1];
        let mut primes = vec![];
        for i in 2..=v {
            if is_prime[i] {
                primes.push(i as u64);
                for j in ((i * i)..=v).step_by(i) {
                    is_prime[j] = false;
                }
            }
        }
        Self { primes }
    }

    fn factorize(&self, mut x: u64) -> Vec<(u64, u64)> {
        assert!(x > 1);
        let mut res = vec![];
        for &p in self.primes.iter() {
            let mut exp = 0;
            while x % p == 0 {
                exp += 1;
                x = x / p;
            }
            if exp > 0 {
                res.push((p, exp))
            }
            if p * p > x {
                break;
            }
        }
        if x > 1 {
            res.push((x, 1));
        }
        res
    }
}

ABC280D

Number/Sum/Product of Factors

Assume [{x = p^a q^b r^c}], then

nameformula
Number of factors of [{x}][{(a + 1)(b + 1)(c + 1)}]
Sum of factors of [{x}][{(p^(a + 1) - 1) / (p - 1) * (q^(b + 1) - 1) / (q - 1) * (r^(c + 1) - 1) / (r - 1)}]
Product of factors of [{x}][{ x^( 1/2 * "number of factors" ) = x^( 1/2 * ((a+1)(b+1)(c+1))) }]

Take the factors of 36 as example:

1  2  3  4  6
36 18 12 9

There are [{"number of factors" / 2 = 4.5}] pairs that have product 36.

ARC167B

Number of Primes Under x

power of 10:

xNumber of primes < x
10^225
10^3168
10^41,229
10^59,592
10^678,498
10^7664,579
10^85,7614,55
10^9508,475,34
10^10455,052,511
10^114,118,054,813
10^1237,607,912,018

power of 2:

xNumber of primes < x
2^854
2^10172
2^166,542
2^2082,025
2^3241,203,088,796

Number of Prime Factors

// cnt[i] = number of prime factors of i
let mut cnt = vec![0; n + 1];
for i in 2..=n {
    if cnt[i] == 0 {
        for j in (i..=n).step_by(i) {
            cnt[j] += 1;
        }
    }
}

Typical90-030