devela::num

Function prime_number_theorem

Source
pub fn prime_number_theorem(n: u128) -> u128 
Available on crate features std or _float_f64 only.
Expand description

The prime number theorem formula.

Returns the approximate count of primes less than the given n.

$$ \large \pi(x) \sim \frac{x}{\ln(x)} $$

§Examples

use devela::num::prime_number_theorem as pi;

// Showing the % difference against the real amount, if known.
// Note how precision increases in direct relationship to the power.
assert_eq![pi(u8::MAX.into()), 46]; // 14.81% < 54
assert_eq![pi(u16::MAX.into()), 5909]; // 9.67% < 6542

#[cfg(feature = "std")] // too slow otherwise
{
    assert_eq![pi(u32::MAX.into()), 193635251]; // 4.74% < 203280221
    assert_eq![pi(u64::MAX.into()), 415828534307635072]; // 2.30% < 425656284035217743
    assert_eq![pi(2u128.pow(92)), 77650867634561160386183168]; // 1.59% < 78908656317357166866404346
    assert_eq![pi(u128::MAX.into()), 3835341275459348115779911081237938176]; // ?% < ?
}