Skip to content

[pull] master from TheAlgorithms:master #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bit_manipulation/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
48 changes: 48 additions & 0 deletions src/bit_manipulation/sum_of_two_integers.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions src/machine_learning/linear_regression.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions src/machine_learning/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod linear_regression;
pub use linear_regression::linear_regression;
59 changes: 59 additions & 0 deletions src/math/perfect_square.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}