Skip to content

Commit

Permalink
Removed num dependency and isqrt
Browse files Browse the repository at this point in the history
  • Loading branch information
wackywendell committed Apr 24, 2015
1 parent 48c666b commit 985f389
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 46 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "primes"
version = "0.1.6"
version = "0.1.7"
authors = ["Wendell Smith <wendellwsmith@gmail.com>"]
license="BSD-3-Clause"

Expand All @@ -16,7 +16,4 @@ readme="README.md"

documentation = "https://wackywendell.github.io/primes/"
homepage = "https://github.com/wackywendell/primes/tree/master"
repository = "https://github.com/wackywendell/primes/tree/master"

[dependencies]
num = "^0.1.24"
repository = "https://github.com/wackywendell/primes/tree/master"
64 changes: 23 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,14 @@ case, but slower in the long term as they do not use any caching of primes.
#[warn(missing_docs)]

extern crate test;
extern crate num;

use std::ops::Index;
use std::slice;
use std::cmp::Ordering::{Equal,Less,Greater};
use num::{Float,NumCast};

#[cfg(test)]
use test::Bencher;

/// Equivalent to floor(sqrt(n)), but takes an integer and returns an integer
fn sqrt_floor<T: NumCast>(n : T) -> T {
let n64 : f64 = NumCast::from(n).unwrap();
let rt = n64.sqrt().floor();
NumCast::from(rt).unwrap()
}

/// Equivalent to floor(sqrt(n)), but takes an integer and returns an integer
fn sqrt_ceil<T: NumCast>(n : T) -> T {
let n64 : f64 = NumCast::from(n).unwrap();
let rt = n64.sqrt().ceil();
NumCast::from(rt).unwrap()
}

/** A prime generator, using the Sieve of Eratosthenes.
Create with `let mut pset = PrimeSet::new()`, and then use `pset.iter()` to iterate over all primes.
Expand Down Expand Up @@ -188,7 +172,7 @@ impl PrimeSet {
if n % m == 0 {return false;};
if m*m > n {return true;};
}
panic!("This iterator should not be empty.");
unreachable!("This iterator should not be empty.");
}

/// Find the next largest prime from a number, if it is within the already-found list
Expand Down Expand Up @@ -315,30 +299,6 @@ pub fn is_prime(n : u64) -> bool {
firstfac(n) == n
}

#[test]
fn test_sqrts(){
assert_eq!(sqrt_ceil(0), 0);
assert_eq!(sqrt_floor(0), 0);

assert_eq!(sqrt_ceil(1), 1);
assert_eq!(sqrt_floor(1), 1);

let rts = [2u64,3,5,7,11,13,17,19,23, 8734, 809832, 7433154, 1 << 26 - 1];
for &rt in rts.iter() {
let sq = rt * rt;
println!("rt: {}, n: {}", rt, sq);

assert_eq!(sqrt_ceil(sq), rt);
assert_eq!(sqrt_floor(sq), rt);

assert_eq!(sqrt_ceil(sq-1), rt);
assert_eq!(sqrt_floor(sq-1), rt-1);

assert_eq!(sqrt_ceil(sq+1), rt+1);
assert_eq!(sqrt_floor(sq+1), rt);
}
}

#[test]
fn test_iter(){
let mut pset = PrimeSet::new();
Expand Down Expand Up @@ -395,6 +355,28 @@ fn test_primes(){
assert!(!is_prime(9));
assert!(pset.is_prime(5));
assert!(is_prime(5));

assert!(pset.is_prime(954377));
assert!(pset.is_prime(954379));
assert!(!pset.is_prime(954377*954379));

assert!(!is_prime(18409199*18409201));
assert!(pset.is_prime(18409199));
assert!(pset.is_prime(18409201));

assert!(!pset.is_prime(2147483643));
assert!(pset.is_prime(2147483647));
assert!(!pset.is_prime(2147483649));
assert!(!pset.is_prime(63061493));
assert!(!pset.is_prime(63061491));
assert!(pset.is_prime(63061489));
assert!(!pset.is_prime(63061487));
assert!(!pset.is_prime(63061485));
assert!(pset.is_prime(2147483647));
assert!(pset.is_prime(63061489));
assert!(!is_prime(63061489 * 2147483647));
assert!(!is_prime(63061489 * 63061489));
// assert!(!is_prime(2147483647 * 2147483647)); // Runs very long
}

#[test]
Expand Down

0 comments on commit 985f389

Please sign in to comment.