Skip to content

Commit

Permalink
Add another Perfect Cube implementation (TheAlgorithms#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
octavianarmasu authored Jan 8, 2024
1 parent 3a1724d commit 5f1c45a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
* [Newton Raphson](https://github.com/TheAlgorithms/Rust/blob/master/src/math/newton_raphson.rs)
* [Nthprime](https://github.com/TheAlgorithms/Rust/blob/master/src/math/nthprime.rs)
* [Pascal Triangle](https://github.com/TheAlgorithms/Rust/blob/master/src/math/pascal_triangle.rs)
* [Perfect Cube](https://github.com/TheAlgorithms/Rust/blob/master/src/math/perfect_cube.rs)
* [Perfect Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/perfect_numbers.rs)
* [Perfect Square](https://github.com/TheAlgorithms/Rust/blob/master/src/math/perfect_square.rs)
* [Pollard Rho](https://github.com/TheAlgorithms/Rust/blob/master/src/math/pollard_rho.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod miller_rabin;
mod newton_raphson;
mod nthprime;
mod pascal_triangle;
mod perfect_cube;
mod perfect_numbers;
mod perfect_square;
mod pollard_rho;
Expand Down Expand Up @@ -131,6 +132,7 @@ pub use self::miller_rabin::{big_miller_rabin, miller_rabin};
pub use self::newton_raphson::find_root;
pub use self::nthprime::nthprime;
pub use self::pascal_triangle::pascal_triangle;
pub use self::perfect_cube::{perfect_cube, perfect_cube_binary_search};
pub use self::perfect_numbers::perfect_numbers;
pub use self::perfect_square::perfect_square;
pub use self::perfect_square::perfect_square_binary_search;
Expand Down
55 changes: 55 additions & 0 deletions src/math/perfect_cube.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
pub fn perfect_cube(n: i32) -> bool {
// Calculate the cube root using floating-point arithmetic.
let val = (n as f64).powf(1.0 / 3.0);
// Check if the cube of the cube root equals the original number.
(val * val * val) == (n as f64)
}

// Check if a number is a perfect cube using binary search.
pub fn perfect_cube_binary_search(n: i64) -> bool {
// Handle negative numbers, as cube roots are not defined for negatives.
if n < 0 {
return false;
}

// Initialize left and right boundaries for binary search.
let mut left = 0;
let mut right = n.abs(); // Use the absolute value to handle negative numbers

// Binary search loop to find the cube root.
while left <= right {
// Calculate the mid-point.
let mid = left + (right - left) / 2;
// Calculate the cube of the mid-point.
let cube = mid * mid * mid;

// Check if the cube equals the original number.
match cube.cmp(&n) {
std::cmp::Ordering::Equal => return true,
std::cmp::Ordering::Less => left = mid + 1,
std::cmp::Ordering::Greater => right = mid - 1,
}
}

// If no cube root is found, return false.
false
}

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

#[test]
fn test_perfect_cube() {
assert!(perfect_cube_binary_search(27));
assert!(!perfect_cube_binary_search(4));
}

#[test]
fn test_perfect_cube_binary_search() {
assert!(perfect_cube_binary_search(27));
assert!(perfect_cube_binary_search(64));
assert!(!perfect_cube_binary_search(4));
assert!(!perfect_cube_binary_search(-8));
}
}

0 comments on commit 5f1c45a

Please sign in to comment.