Skip to content

Commit

Permalink
Add some other utility functions found in C solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 12, 2024
1 parent af7fce2 commit b55674e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/rust.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Library Code
:numbered:
:maxdepth: 1

rust/digits
rust/fibonacci
rust/primes

Problems Solved
Expand Down
12 changes: 12 additions & 0 deletions docs/rust/primes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
A convenience method that returns an iterator over the prime numbers.

.. rust:fn:: primes::primes_until(x: u64) -> Eratosthenes
A convenience method that returns an iterator over the prime numbers until a given limit.

.. rust:struct:: primes::Eratosthenes
This class implements the `Sieve of Eratosthenes <https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes>`_. In general,
Expand Down Expand Up @@ -40,6 +44,14 @@ View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler
This function returns a vector of the proper divisors of a number.

.. rust:fn:: fn is_composite(x: u64) -> u64
Returns ``0`` if the number is prime, and the smallest prime factor otherwise.

.. rust:fn:: fn is_prime(x: u64) -> bool
Returns ``true`` if the number is prime, and ``false`` otherwise.

.. literalinclude:: ../../rust/src/primes.rs
:language: rust
:linenos:
6 changes: 5 additions & 1 deletion rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ fn test_primes() -> Result<(), String> {
#[cfg(test)]
#[test]
fn test_prime_factors() -> Result<(), String> {
for v in primes::primes().take(15).combinations(2) {
for v in primes::primes_until(1024).combinations(2) {
let p = v[0];
let s = v[1];
assert!(prime::is_prime(p));
assert!(prime::is_prime(s));
assert!(prime::is_composite(p * s) != 0);
for f in primes::prime_factors(p * s) {
assert!(prime::is_prime(f));
assert!(f == p || f == s);
}
}
Expand Down
22 changes: 22 additions & 0 deletions rust/src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ pub fn primes() -> Eratosthenes {
return Eratosthenes::new();
}

pub fn primes_until(x: u64) -> Eratosthenes {
return Eratosthenes::with_limit(x);
}

pub struct PrimeFactors {
number: u64
}
Expand Down Expand Up @@ -116,3 +120,21 @@ pub fn proper_divisors(x: u64) -> Vec<u64> {
ret.dedup();
return ret;
}

pub fn is_composite(x: u64) -> u64 {
match prime_factors(x).next() {
None => {
return 0;
}
Some(number) => {
if number == x {
return 0;
}
return number;
}
}
}

pub fn is_prime(x: u64) -> bool {
return is_composite(x) == 0;
}

0 comments on commit b55674e

Please sign in to comment.