1个不稳定版本

0.1.2 2021年11月9日
0.1.1 2021年11月9日
0.1.0 2021年11月9日

#1351 in 数学

MIT/Apache

110KB
921

Prime Data

这个库最初是为了简单处理素数和在快速且内存高效的方式中存储素数而设计的。

当你将这个crate导入到你的项目中时,你将默认获得以下工具

假设你想知道100到200之间所有的素数

let primes = prime_data::PrimeData::generate(100..=200);

有了这个,你可以用它做一些很酷的事情

// You can iterate over all those prime numbers
for prime in primes.iter_all() {
    println!("{} is a prime number!", prime);
}

// Therefore, you can collect them into a vector
println!("{:?}", primes.iter_all().collect::<Vec<_>>());

// You don't have to iterate through all of them. Maybe you just want primes within some range
// Naturally, you can collect those into a vector too
for prime in primes.iter(161..=179) {
    println!("{p} is a prime number and 161 <= {p} <= 179", p = prime);
}

// If you just want to know how many there are
println!("There are {} primes between 100 and 200.", primes.count_primes());

// ...within a range
println!("There are {} primes between 161 and 179.", primes.count_primes_in_range(161..=179));

然而,有一些方便的公共方法,可以抽象出生成数据的需求

// You can verify if a number is prime
println!("2027 is {}", if prime_data::is_prime(2_027) { "prime!" } else { "not prime!" });

// You can count the amount of primes from 1 to some bound
println!("There are {} primes from 1 to 1024", prime_data::count_primes(1024));

功能

通过在依赖项中包含功能,你可以获得更多功能

"因子"

因子功能包括一个结构和用于分解数字的公共方法。

// from some prime data...
let data = prime_data::PrimeData::generate(0..=12);

// you can factorize numbers
let factors = data.factorize(120);

// from that, you can retrieve the original number
println!("The prime factorization of {} is:", factors.as_u64());
// retrieve the prime factors as tuples, where each tuple is of the form
// (prime, amount) meaning prime.pow(amount)
println!("{:?}", factors.as_tuples());

// or alternatively, you can retrieve a list of all factors
let all_factors = factors.all_factors();
println!("All factors are: {:?}", all_factors);
println!("120 has {} factors in total.", all_factors.len());

// you can also convert a u64 into its factorization
let factorized_44 = prime_data::Factorization::from(44);
println!("The factors of 44 are {:?}", factorized_44.all_factors());

// finally, if you only need to list a number's factors once,
// you can use the public method:
println!("The factors of 490 are {:?}", prime_data::all_factors_of(490));

无运行时依赖

功能