<commit_before>#![allow(dead_code)]
#![allow(unused_imports)]

#![feature(test)]
//extern crate rand;
extern crate test;

use test::Bencher;

#[test]
fn is_it_a_prime_number() {
    assert!(is_prime(5));
    assert!(is_prime_why_not(5));
}

#[test]
fn should_not_be_a_prime_number() {
    assert_eq!(false, is_prime(4));
    assert_eq!(false, is_prime_why_not(4));
}

#[test]
fn prime_factorization_test(){
    let mut vec = Vec::new();
    vec.push(2);
    vec.push(2);
    vec.push(2);
    vec.push(3);
    vec.push(7);
    assert_eq!(vec, prime_factorization(168));
    vec = Vec::new();
    vec.push(2);
    vec.push(3);
    vec.push(17);
    assert_eq!(vec, prime_factorization(102));
}

#[test]
fn get_next_prime_number_test() {
    assert_eq!(79, get_next_prime_number(73));
}

#[test]
fn get_twin_primes_test() {
    assert_eq!((5, 7), get_twin_primes(5));
}

#[test]
fn greatest_common_factor_test() {
    assert_eq!(6, greatest_common_factor(18,24));
}

#[test]
fn least_common_multiple_test() {
    assert_eq!(90, least_common_multiple(18,30));
}

#[test]
fn number_of_primes_test() {
    assert_eq!(489, number_of_primes(1,3500));
}

#[test]
fn number_of_factors_test() {
    assert_eq!(12, number_of_factors(72));
}

#[test]
fn mersenne_prime_test() {
    let mersenne_prime = mersenne_prime(5);
    assert_eq!(31, mersenne_prime);
}

#[test]
fn generate_primes_test() {
    let result = generate_primes(3500);
    assert_eq!(489, result.len());
}

//#[bench]
//fn bench_is_prime(b: &mut Bencher) {
//    b.iter(|| is_prime(rand::random::<i32>()));
//}

/// Tests for prime numbers.
///
/// # Examples
///
/// ```
/// use primapalooza::is_prime;
/// 
/// println!("{}", primapalooza::is_prime(5));
/// 
pub fn is_prime(x: i32) -> bool {
    if x < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut start: i64 = 2;
    while start <= (x as f64).sqrt() as i64 {
        if (x as i64) % start < 1 {
            return false;
        }
        start += 1;
    }
    return x > 1;
}

//#[bench]
//fn bench_is_prime_why_not(b: &mut Bencher) {
//      b.iter(|| 
//             for n in (0..10) {
//                test::black_box(is_prime_why_not(n));
//             });
//}

/// Tests for prime numbers and state why its not a prime number.
///
/// # Examples
///
/// ```
/// use primapalooza::is_prime_why_not;
/// 
/// println!("{}", primapalooza::is_prime_why_not(9));
/// 
pub fn is_prime_why_not(x: i32) -> bool {
    if x < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut start: i64 = 2;
    let mut return_value: bool = true;
    while start <= (x as f64).sqrt() as i64 {
        let m = (x as i64) % start; 
        if m < 1 {
            println!("{} is divisible by {}.  {} * {} = {}", x, start, start, ((x as i64) / start), x);
            return_value = false;
        }
        start += 1;
    }
    if return_value == true { 
        return_value = x > 1;
    }
    return return_value;
}

//#[bench]
//fn bench_get_next_prime_number(b: &mut Bencher) {
//    b.iter(|| 
//           for n in (1..10) {
//               test::black_box(get_next_prime_number(n));
//           });
//}

/// Returns the next available prime number.
///
/// # Examples
///
/// ```
/// use primapalooza::get_next_prime_number;
/// 
/// println!("{}", primapalooza::get_next_prime_number(9));
/// 
pub fn get_next_prime_number(n:i32) -> i32 {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut x: i32 = n;
    x += 1;
    while !is_prime(x) {
        x += 1;
    }
    return x;
}

/// Returns the twin primes.  A pair of prime numbers that differ by 2.
///
/// # Examples
///
/// ```
/// use primapalooza::get_twin_primes;
/// 
/// println!("{:?}", primapalooza::get_twin_primes(5));
/// 
pub fn get_twin_primes(n: i32) -> (i32, i32){
    if !is_prime(n) {
        panic!("prime number required as input");
    }
    return (n, get_next_prime_number(n));
}

//#[bench]
//fn bench_prime_factorization(b: &mut Bencher) {
//    b.iter(||
//           for n in (1..10) {
//                test::black_box(prime_factorization(n));
//           });
//}

/// Perform a prime factorization.
///
/// # Examples
///
/// ```
/// use primapalooza::prime_factorization;
/// 
/// println!("{:?}", primapalooza::prime_factorization(168));
/// 
pub fn prime_factorization(n:i32) -> Vec<i64> {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut x:i64 = n as i64;
    let mut return_value = Vec::new();
    let mut start:i64 = 2;
    while !is_prime(x as i32) {
        let m = x % start;
        if m < 1 {
            return_value.push(start);
            x = x / start;
        } else {
            let s = start as i32;
            start = get_next_prime_number(s) as i64;                                                                  }
    }
    return_value.push(x);
    return return_value;
}


/// Perform a greatest common factor(gcf) calculation.
///
/// # Examples
///
/// ```
/// use primapalooza::greatest_common_factor;
/// 
/// println!("{}", primapalooza::greatest_common_factor(18, 24));
/// 
pub fn greatest_common_factor(x:i32, y:i32) -> i32 {
    if x < 2 || y < 2 {
        panic!("requires a positive integers greater then 1");
    }
    let mut vec_x = prime_factorization(x);
    let mut vec_y = prime_factorization(y);
    vec_x.dedup();
    vec_y.dedup();
    let mut m:Vec<i32> = Vec::new();
    for p in vec_x.iter() {
        for q in vec_y.iter() {
            if p == q {
                m.push(*p as i32);
            }
        }
    }
    let mut return_value = 1;
    for k in &m {
        return_value = return_value * k;    
    }
    return return_value;
}


/// Perform a least common multiple(lcm) calculation.
///
/// # Examples
///
/// ```
/// use primapalooza::least_common_multiple;
/// 
/// println!("{}", primapalooza::least_common_multiple(18, 24));
/// 
pub fn least_common_multiple(x:i32, y:i32) -> i32 {
    if x < 2 || y < 2 {
        panic!("requires a positive integers greater then 1");
    }
    let gcf = greatest_common_factor(x, y);
    if x % gcf < 1 {
        (x/gcf) * y
    } else {
        (y/gcf) * x
    }
}


/// Number of prime numbers within a range.
///
/// # Examples
///
/// ```
/// use primapalooza::number_of_primes;
/// 
/// println!("{}", primapalooza::number_of_primes(1, 3500));
/// 
pub fn number_of_primes(x: i32, y: i32) -> i32 {
    if x < 1 {
        panic!("requires a positive integers");
    }
    if y < 2 {
        panic!("requires a positive integer greater than 1");
    }
    let mut a = x;
    if a == 1 {
        a += 1;
    }
    let mut z = 0;
    for n in a..y {
        if is_prime(n) {
            z += 1
        }
    }
    return z;
}

/// The number of factors in a number.
///
/// # Examples
///
/// ```
/// use primapalooza::number_of_factors;
/// 
/// println!("{}", primapalooza::number_of_factors(72));
/// 
pub fn number_of_factors(n: i32) -> i32 {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let prime_factors = prime_factorization(n);
    let mut factors = prime_factors.clone();
    factors.dedup();
    let mut return_value = 1;
    for f in factors.iter() {
        let mut count = 1;
        for pf in prime_factors.iter() {
            if pf == f {
                count += 1;
            }
        }
        return_value = return_value * count;
    }
    return return_value;
}

/// Generate a mersenne prime from a prime number.
///
/// # Examples
///
/// ```
/// use primapalooza::mersenne_prime;
/// 
/// let results = primapalooza::mersenne_prime(5);
pub fn mersenne_prime(n: i32) -> i32 {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let two:i32 = 2;
    let result:i32 = (two.pow(n as u32)) - 1;
    return result;
}

/// Generate a collection of prime numbers up to a particular limit.
///
/// # Examples<commit_msg>added perfect_number<commit_after>#![allow(dead_code)]
#![allow(unused_imports)]

#![feature(test)]
//extern crate rand;
extern crate test;

use test::Bencher;

#[test]
fn is_it_a_prime_number() {
    assert!(is_prime(5));
    assert!(is_prime_why_not(5));
}

#[test]
fn should_not_be_a_prime_number() {
    assert_eq!(false, is_prime(4));
    assert_eq!(false, is_prime_why_not(4));
}

#[test]
fn prime_factorization_test(){
    let mut vec = Vec::new();
    vec.push(2);
    vec.push(2);
    vec.push(2);
    vec.push(3);
    vec.push(7);
    assert_eq!(vec, prime_factorization(168));
    vec = Vec::new();
    vec.push(2);
    vec.push(3);
    vec.push(17);
    assert_eq!(vec, prime_factorization(102));
}

#[test]
fn get_next_prime_number_test() {
    assert_eq!(79, get_next_prime_number(73));
}

#[test]
fn get_twin_primes_test() {
    assert_eq!((5, 7), get_twin_primes(5));
}

#[test]
fn greatest_common_factor_test() {
    assert_eq!(6, greatest_common_factor(18,24));
}

#[test]
fn least_common_multiple_test() {
    assert_eq!(90, least_common_multiple(18,30));
}

#[test]
fn number_of_primes_test() {
    assert_eq!(489, number_of_primes(1,3500));
}

#[test]
fn number_of_factors_test() {
    assert_eq!(12, number_of_factors(72));
}

#[test]
fn mersenne_prime_test() {
    let mersenne_prime = mersenne_prime(5);
    assert_eq!(31, mersenne_prime);
}

#[test]
fn perfect_number_test() {
    assert!(perfect_number(5));
    assert_eq!(false, perfect_number(11));
}

#[test]
fn generate_primes_test() {
    let result = generate_primes(3500);
    assert_eq!(489, result.len());
}

//#[bench]
//fn bench_is_prime(b: &mut Bencher) {
//    b.iter(|| is_prime(rand::random::<i32>()));
//}

/// Tests for prime numbers.
///
/// # Examples
///
/// ```
/// use primapalooza::is_prime;
/// 
/// println!("{}", primapalooza::is_prime(5));
/// 
pub fn is_prime(x: i32) -> bool {
    if x < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut start: i64 = 2;
    while start <= (x as f64).sqrt() as i64 {
        if (x as i64) % start < 1 {
            return false;
        }
        start += 1;
    }
    return x > 1;
}

//#[bench]
//fn bench_is_prime_why_not(b: &mut Bencher) {
//      b.iter(|| 
//             for n in (0..10) {
//                test::black_box(is_prime_why_not(n));
//             });
//}

/// Tests for prime numbers and state why its not a prime number.
///
/// # Examples
///
/// ```
/// use primapalooza::is_prime_why_not;
/// 
/// println!("{}", primapalooza::is_prime_why_not(9));
/// 
pub fn is_prime_why_not(x: i32) -> bool {
    if x < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut start: i64 = 2;
    let mut return_value: bool = true;
    while start <= (x as f64).sqrt() as i64 {
        let m = (x as i64) % start; 
        if m < 1 {
            println!("{} is divisible by {}.  {} * {} = {}", x, start, start, ((x as i64) / start), x);
            return_value = false;
        }
        start += 1;
    }
    if return_value == true { 
        return_value = x > 1;
    }
    return return_value;
}

//#[bench]
//fn bench_get_next_prime_number(b: &mut Bencher) {
//    b.iter(|| 
//           for n in (1..10) {
//               test::black_box(get_next_prime_number(n));
//           });
//}

/// Returns the next available prime number.
///
/// # Examples
///
/// ```
/// use primapalooza::get_next_prime_number;
/// 
/// println!("{}", primapalooza::get_next_prime_number(9));
/// 
pub fn get_next_prime_number(n:i32) -> i32 {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut x: i32 = n;
    x += 1;
    while !is_prime(x) {
        x += 1;
    }
    return x;
}

/// Returns the twin primes.  A pair of prime numbers that differ by 2.
///
/// # Examples
///
/// ```
/// use primapalooza::get_twin_primes;
/// 
/// println!("{:?}", primapalooza::get_twin_primes(5));
/// 
pub fn get_twin_primes(n: i32) -> (i32, i32){
    if !is_prime(n) {
        panic!("prime number required as input");
    }
    return (n, get_next_prime_number(n));
}

//#[bench]
//fn bench_prime_factorization(b: &mut Bencher) {
//    b.iter(||
//           for n in (1..10) {
//                test::black_box(prime_factorization(n));
//           });
//}

/// Perform a prime factorization.
///
/// # Examples
///
/// ```
/// use primapalooza::prime_factorization;
/// 
/// println!("{:?}", primapalooza::prime_factorization(168));
/// 
pub fn prime_factorization(n:i32) -> Vec<i64> {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let mut x:i64 = n as i64;
    let mut return_value = Vec::new();
    let mut start:i64 = 2;
    while !is_prime(x as i32) {
        let m = x % start;
        if m < 1 {
            return_value.push(start);
            x = x / start;
        } else {
            let s = start as i32;
            start = get_next_prime_number(s) as i64;                                                                  }
    }
    return_value.push(x);
    return return_value;
}


/// Perform a greatest common factor(gcf) calculation.
///
/// # Examples
///
/// ```
/// use primapalooza::greatest_common_factor;
/// 
/// println!("{}", primapalooza::greatest_common_factor(18, 24));
/// 
pub fn greatest_common_factor(x:i32, y:i32) -> i32 {
    if x < 2 || y < 2 {
        panic!("requires a positive integers greater then 1");
    }
    let mut vec_x = prime_factorization(x);
    let mut vec_y = prime_factorization(y);
    vec_x.dedup();
    vec_y.dedup();
    let mut m:Vec<i32> = Vec::new();
    for p in vec_x.iter() {
        for q in vec_y.iter() {
            if p == q {
                m.push(*p as i32);
            }
        }
    }
    let mut return_value = 1;
    for k in &m {
        return_value = return_value * k;    
    }
    return return_value;
}


/// Perform a least common multiple(lcm) calculation.
///
/// # Examples
///
/// ```
/// use primapalooza::least_common_multiple;
/// 
/// println!("{}", primapalooza::least_common_multiple(18, 24));
/// 
pub fn least_common_multiple(x:i32, y:i32) -> i32 {
    if x < 2 || y < 2 {
        panic!("requires a positive integers greater then 1");
    }
    let gcf = greatest_common_factor(x, y);
    if x % gcf < 1 {
        (x/gcf) * y
    } else {
        (y/gcf) * x
    }
}


/// Number of prime numbers within a range.
///
/// # Examples
///
/// ```
/// use primapalooza::number_of_primes;
/// 
/// println!("{}", primapalooza::number_of_primes(1, 3500));
/// 
pub fn number_of_primes(x: i32, y: i32) -> i32 {
    if x < 1 {
        panic!("requires a positive integers");
    }
    if y < 2 {
        panic!("requires a positive integer greater than 1");
    }
    let mut a = x;
    if a == 1 {
        a += 1;
    }
    let mut z = 0;
    for n in a..y {
        if is_prime(n) {
            z += 1
        }
    }
    return z;
}

/// The number of factors in a number.
///
/// # Examples
///
/// ```
/// use primapalooza::number_of_factors;
/// 
/// println!("{}", primapalooza::number_of_factors(72));
/// 
pub fn number_of_factors(n: i32) -> i32 {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let prime_factors = prime_factorization(n);
    let mut factors = prime_factors.clone();
    factors.dedup();
    let mut return_value = 1;
    for f in factors.iter() {
        let mut count = 1;
        for pf in prime_factors.iter() {
            if pf == f {
                count += 1;
            }
        }
        return_value = return_value * count;
    }
    return return_value;
}

/// Generate a mersenne prime from a prime number.
///
/// # Examples
///
/// ```
/// use primapalooza::mersenne_prime;
/// 
/// let results = primapalooza::mersenne_prime(5);
pub fn mersenne_prime(n: i32) -> i32 {
    if n < 2 {
        panic!("requires a positive integer greater then 1");
    }
    let two:i32 = 2;
    let result:i32 = (two.pow(n as u32)) - 1;
    return result;
}

/// Is a number a perfect number.  The sum of its factors is the number itself.
///
/// # Examples
///
/// ```
/// use primapalooza::perfect_number;
/// 
/// println!("{}", primapalooza::perfect_number(5));
pub fn perfect_number(n:i32) -> bool {
    return is_prime(mersenne_prime(n));
}

/// Generate a collection of prime numbers up to a particular limit.
///
/// # Examples
{'fraction_non_alphanumeric': 0.1756748485033972, 'fraction_numerical': 0.027789679867784783, 'mean_word_length': 2.169965075669383, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3453064', 'n_tokens_mistral': 6195, 'n_tokens_neox': 5966, 'n_words': 1400}