Skip to content

Commit

Permalink
Remove mod primecount
Browse files Browse the repository at this point in the history
  • Loading branch information
maitbayev committed Jun 21, 2020
1 parent 30b4f48 commit 8105c89
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "primecount"
version = "0.1.0"
version = "0.1.1"
authors = ["Madiyar Aitbayev ([email protected])"]
edition = "2018"
license-file = "LICENSE"
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
# Rust bindings for primecount
# primecount-rs

primecount-rs is a library that provides APIs for counting the primes below an integer x ≤ 1031
using highly optimized implementations of the combinatorial
[prime counting algorithms](https://en.wikipedia.org/wiki/Prime-counting_function#Algorithms_for_evaluating_%CF%80(x)).

It is a rust wrapper around an awesome [kimwalisch/primecount](https://github.com/kimwalisch/primecount) library.

## API

```rust
use primecount;

fn main() {
println!("Primes below 1000 = {}", primecount::pi(1000));
println!("Numbers below 1000 that are not divisible by any of the first 100 primes (a.k.a. Legendre-sum) = {}", primecount::phi(1000, 100));
println!("10th prime = {}", primecount::nth_prime(10));
}
```

## Contribute

Expand Down
65 changes: 31 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,53 @@ extern "C" {
fn primecount_nth_prime(n: i64) -> i64;
}

pub mod primecount {
use super::*;

/// Count the number of primes <= x using Xavier Gourdon's
/// algorithm. Uses all CPU cores by default.
/// Returns -1 if an error occurs.
///
/// Run time: O(x^(2/3) / (log x)^2)
/// Memory usage: O(x^(1/3) /// (log x)^3)
pub fn pi(x: i64) -> i64 {
unsafe { primecount_pi(x) }
}

/// Partial sieve function (a.k.a. Legendre-sum).
/// phi(x, a) counts the numbers <= x that are not divisible
/// by any of the first a primes.
/// Returns -1 if an error occurs.
pub fn phi(x: i64, a: i64) -> i64 {
unsafe { primecount_phi(x, a) }
}
/// Count the number of primes <= x using Xavier Gourdon's
/// algorithm. Uses all CPU cores by default.
/// Returns -1 if an error occurs.
///
/// Run time: O(x^(2/3) / (log x)^2)
/// Memory usage: O(x^(1/3) * (log x)^3)
pub fn pi(x: i64) -> i64 {
unsafe { primecount_pi(x) }
}

/// Find the nth prime using a combination of the prime counting
/// function and the sieve of Eratosthenes.
/// @pre n <= 216289611853439384
/// Returns -1 if an error occurs.
///
/// Run time: O(x^(2/3) / (log x)^2)
/// Memory usage: O(x^(1/2))
pub fn nth_prime(n: i64) -> i64 {
unsafe { primecount_nth_prime(n) }
}
/// Partial sieve function (a.k.a. Legendre-sum).
/// phi(x, a) counts the numbers <= x that are not divisible
/// by any of the first a primes.
/// Returns -1 if an error occurs.
pub fn phi(x: i64, a: i64) -> i64 {
unsafe { primecount_phi(x, a) }
}

// TODO(madiyar): Add i128 version of APIs.
/// Find the nth prime using a combination of the prime counting
/// function and the sieve of Eratosthenes.
/// @pre n <= 216289611853439384
/// Returns -1 if an error occurs.
///
/// Run time: O(x^(2/3) / (log x)^2)
/// Memory usage: O(x^(1/2))
pub fn nth_prime(n: i64) -> i64 {
unsafe { primecount_nth_prime(n) }
}

// TODO(madiyar): Add i128 version of APIs.

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_primecount_pi() {
assert_eq!(primecount::pi(20), 8);
assert_eq!(pi(20), 8);
}

#[test]
fn test_primecount_phi() {
assert_eq!(primecount::phi(10i64.pow(12), 78498i64), 37607833521)
assert_eq!(phi(10i64.pow(12), 78498i64), 37607833521)
}

#[test]
fn test_primecount_nth_prime() {
assert_eq!(primecount::nth_prime(455052511), 9999999967);
assert_eq!(nth_prime(455052511), 9999999967);
}
}

0 comments on commit 8105c89

Please sign in to comment.