From 1050cb86c7a0bcc4b2076ccf1caa5c737c107024 Mon Sep 17 00:00:00 2001 From: Harsh Kumar <61012869+cyrixninja@users.noreply.github.com> Date: Mon, 16 Oct 2023 01:06:33 +0530 Subject: [PATCH 1/3] Add Perfect Square (#576) --- src/math/perfect_square.rs | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/math/perfect_square.rs diff --git a/src/math/perfect_square.rs b/src/math/perfect_square.rs new file mode 100644 index 00000000000..575da8d8c68 --- /dev/null +++ b/src/math/perfect_square.rs @@ -0,0 +1,59 @@ +// Author : cyrixninja +// Perfect Square : Checks if a number is perfect square number or not +// https://en.wikipedia.org/wiki/Perfect_square +fn perfect_square(num: i32) -> bool { + if num < 0 { + return false; + } + let sqrt_num = (num as f64).sqrt() as i32; + sqrt_num * sqrt_num == num +} + +fn perfect_square_binary_search(n: i32) -> bool { + if n < 0 { + return false; + } + + let mut left = 0; + let mut right = n; + + while left <= right { + let mid = (left + right) / 2; + let mid_squared = mid * mid; + + if mid_squared == n { + return true; + } else if mid_squared > n { + right = mid - 1; + } else { + left = mid + 1; + } + } + + false +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_perfect_square() { + assert!(perfect_square(9) == true); + assert!(perfect_square(81) == true); + assert!(perfect_square(4) == true); + assert!(perfect_square(0) == true); + assert!(perfect_square(3) == false); + assert!(perfect_square(-19) == false); + } + + #[test] + fn test_perfect_square_binary_search() { + assert!(perfect_square_binary_search(9) == true); + assert!(perfect_square_binary_search(81) == true); + assert!(perfect_square_binary_search(4) == true); + assert!(perfect_square_binary_search(0) == true); + assert!(perfect_square_binary_search(3) == false); + assert!(perfect_square_binary_search(-19) == false); + } +} From 173d856a53b3b1f76700f2c085dcb024f52f3a9c Mon Sep 17 00:00:00 2001 From: Abdellah GUIZOUL <95940388+guizo792@users.noreply.github.com> Date: Sun, 15 Oct 2023 20:38:29 +0100 Subject: [PATCH 2/3] Add sum of two integers using bit manipulation (#577) --- src/bit_manipulation/mod.rs | 2 + src/bit_manipulation/sum_of_two_integers.rs | 48 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/bit_manipulation/sum_of_two_integers.rs diff --git a/src/bit_manipulation/mod.rs b/src/bit_manipulation/mod.rs index 0416192f3d1..1c9fae8d3af 100644 --- a/src/bit_manipulation/mod.rs +++ b/src/bit_manipulation/mod.rs @@ -1,5 +1,7 @@ mod counting_bits; mod highest_set_bit; +mod sum_of_two_integers; pub use counting_bits::count_set_bits; pub use highest_set_bit::find_highest_set_bit; +pub use sum_of_two_integers::add_two_integers; diff --git a/src/bit_manipulation/sum_of_two_integers.rs b/src/bit_manipulation/sum_of_two_integers.rs new file mode 100644 index 00000000000..079ac4c3177 --- /dev/null +++ b/src/bit_manipulation/sum_of_two_integers.rs @@ -0,0 +1,48 @@ +/** + * This algorithm demonstrates how to add two integers without using the + operator + * but instead relying on bitwise operations, like bitwise XOR and AND, to simulate + * the addition. It leverages bit manipulation to compute the sum efficiently. + */ + +pub fn add_two_integers(a: i32, b: i32) -> i32 { + let mut a = a; + let mut b = b; + let mut carry; + let mut sum; + + // Iterate until there is no carry left + while b != 0 { + sum = a ^ b; // XOR operation to find the sum without carry + carry = (a & b) << 1; // AND operation to find the carry, shifted left by 1 + a = sum; + b = carry; + } + + a +} + +#[cfg(test)] +mod tests { + use super::add_two_integers; + + #[test] + fn test_add_two_integers_positive() { + assert_eq!(add_two_integers(3, 5), 8); + assert_eq!(add_two_integers(100, 200), 300); + assert_eq!(add_two_integers(65535, 1), 65536); + } + + #[test] + fn test_add_two_integers_negative() { + assert_eq!(add_two_integers(-10, 6), -4); + assert_eq!(add_two_integers(-50, -30), -80); + assert_eq!(add_two_integers(-1, -1), -2); + } + + #[test] + fn test_add_two_integers_zero() { + assert_eq!(add_two_integers(0, 0), 0); + assert_eq!(add_two_integers(0, 42), 42); + assert_eq!(add_two_integers(0, -42), -42); + } +} From 5a5c0d41d1053ee7cf1fdb70b4e9ebac34ed0c67 Mon Sep 17 00:00:00 2001 From: Aryan Srivastava <145459408+aryan20s@users.noreply.github.com> Date: Mon, 16 Oct 2023 01:15:51 +0530 Subject: [PATCH 3/3] Add linear regression (#579) --- src/lib.rs | 1 + src/machine_learning/linear_regression.rs | 48 +++++++++++++++++++++++ src/machine_learning/mod.rs | 2 + 3 files changed, 51 insertions(+) create mode 100644 src/machine_learning/linear_regression.rs create mode 100644 src/machine_learning/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 7d0274aeef6..0c92f73c2f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod dynamic_programming; pub mod general; pub mod geometry; pub mod graph; +pub mod machine_learning; pub mod math; pub mod navigation; pub mod number_theory; diff --git a/src/machine_learning/linear_regression.rs b/src/machine_learning/linear_regression.rs new file mode 100644 index 00000000000..22e0840e58b --- /dev/null +++ b/src/machine_learning/linear_regression.rs @@ -0,0 +1,48 @@ +/// Returns the parameters of the line after performing simple linear regression on the input data. +pub fn linear_regression(data_points: Vec<(f64, f64)>) -> Option<(f64, f64)> { + if data_points.is_empty() { + return None; + } + + let count = data_points.len() as f64; + let mean_x = data_points.iter().fold(0.0, |sum, y| sum + y.0) / count; + let mean_y = data_points.iter().fold(0.0, |sum, y| sum + y.1) / count; + + let mut covariance = 0.0; + let mut std_dev_sqr_x = 0.0; + let mut std_dev_sqr_y = 0.0; + + for data_point in data_points { + covariance += (data_point.0 - mean_x) * (data_point.1 - mean_y); + std_dev_sqr_x += (data_point.0 - mean_x).powi(2); + std_dev_sqr_y += (data_point.1 - mean_y).powi(2); + } + + let std_dev_x = std_dev_sqr_x.sqrt(); + let std_dev_y = std_dev_sqr_y.sqrt(); + let std_dev_prod = std_dev_x * std_dev_y; + + let pcc = covariance / std_dev_prod; //Pearson's correlation constant + let b = pcc * (std_dev_y / std_dev_x); //Slope of the line + let a = mean_y - b * mean_x; //Y-Intercept of the line + + Some((a, b)) +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_linear_regression() { + assert_eq!( + linear_regression(vec![(0.0, 0.0), (1.0, 1.0), (2.0, 2.0)]), + Some((2.220446049250313e-16, 0.9999999999999998)) + ); + } + + #[test] + fn test_empty_list_linear_regression() { + assert_eq!(linear_regression(vec![]), None); + } +} diff --git a/src/machine_learning/mod.rs b/src/machine_learning/mod.rs new file mode 100644 index 00000000000..3c31bd3175b --- /dev/null +++ b/src/machine_learning/mod.rs @@ -0,0 +1,2 @@ +mod linear_regression; +pub use linear_regression::linear_regression;