-
-
Save rust-play/5d25f823e587afae2a526d0c7f82013e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The core concept is that for a number 'n', if 'n % d == 0', then 'n' is divisible by 'd'. | |
| // Divisibility tests rely on properties of the digits. | |
| /// 🔢 Divisibility Tests Module | |
| mod divisibility_tests { | |
| // Helper function to get the sum of digits | |
| pub fn sum_digits(n: u64) -> u64 { | |
| let mut sum = 0; | |
| let mut num = n; | |
| while num > 0 { | |
| sum += num % 10; | |
| num /= 10; | |
| } | |
| sum | |
| } | |
| // Helper function to get the difference between the sum of odd and even place digits (for Divisibility by 11) | |
| pub fn alternating_sum_digits(n: u64) -> i64 { | |
| let s = n.to_string(); | |
| let mut alternating_sum = 0; | |
| // The first digit (right-most) is usually considered the 'odd' place (1st, 3rd, 5th...). | |
| for (i, c) in s.chars().rev().enumerate() { | |
| if let Some(digit) = c.to_digit(10) { | |
| // If index i is even (0, 2, 4...), it's the 1st, 3rd, 5th... (odd) place from the right. | |
| if i % 2 == 0 { | |
| alternating_sum += digit as i64; | |
| } else { | |
| alternating_sum -= digit as i64; | |
| } | |
| } | |
| } | |
| alternating_sum | |
| } | |
| // --- Divisibility by 2: The last digit is divisible by 2. --- | |
| pub fn is_divisible_by_2(n: u64) -> bool { | |
| n % 2 == 0 | |
| } | |
| // --- Divisibility by 3: The sum of the digits is divisible by 3. --- | |
| pub fn is_divisible_by_3(n: u64) -> bool { | |
| sum_digits(n) % 3 == 0 | |
| } | |
| // --- Divisibility by 4: The number formed from the last 2 digits is divisible by 4. --- | |
| pub fn is_divisible_by_4(n: u64) -> bool { | |
| n % 100 % 4 == 0 | |
| } | |
| // --- Divisibility by 5: The last digit is either 0 or 5. --- | |
| pub fn is_divisible_by_5(n: u64) -> bool { | |
| let last_digit = n % 10; | |
| last_digit == 0 || last_digit == 5 | |
| } | |
| // --- Divisibility by 6: The number is divisible by both 2 and 3. --- | |
| pub fn is_divisible_by_6(n: u64) -> bool { | |
| is_divisible_by_2(n) && is_divisible_by_3(n) | |
| } | |
| // --- Divisibility by 7: The rule-based test. --- | |
| pub fn is_divisible_by_7(n: u64) -> bool { | |
| let mut current_num = n; | |
| if current_num == 0 { | |
| return true; | |
| } | |
| while current_num > 99 { | |
| let last_digit = current_num % 10; | |
| let rest_of_number = current_num / 10; | |
| // Subtract twice the last digit from the rest of the number | |
| current_num = rest_of_number - 2 * last_digit; | |
| } | |
| // Check the final result | |
| current_num % 7 == 0 | |
| } | |
| // --- Divisibility by 8: A number is divisible by 8 if the number formed by its last three digits is divisible by 8. --- | |
| pub fn is_divisible_by_8(n: u64) -> bool { | |
| n % 1000 % 8 == 0 | |
| } | |
| // --- Divisibility by 9: The sum of the digits is divisible by 9. --- | |
| pub fn is_divisible_by_9(n: u64) -> bool { | |
| sum_digits(n) % 9 == 0 | |
| } | |
| // --- Divisibility by 10: The last digit is 0. --- | |
| pub fn is_divisible_by_10(n: u64) -> bool { | |
| n % 10 == 0 | |
| } | |
| // --- Divisibility by 11: The difference between the sum of the odd place digits and the sum of the even place digits is divisible by 11. --- | |
| pub fn is_divisible_by_11(n: u64) -> bool { | |
| alternating_sum_digits(n).abs() % 11 == 0 | |
| } | |
| } | |
| // Full example block for demonstration | |
| fn main() { | |
| use divisibility_tests::*; | |
| let number: u64 = 2457; | |
| println!("## 🧪 Testing Divisibility for the Number: {} 🧪", number); | |
| println!("------------------------------------------------------"); | |
| // Test 7 and 11 are the most complex | |
| println!("Divisible by 7 (Expected: True): {}", is_divisible_by_7(number)); | |
| println!("Divisible by 11 (Expected: False): {}", is_divisible_by_11(number)); | |
| println!("Divisible by 9 (Expected: True): {}", is_divisible_by_9(number)); | |
| println!("Divisible by 2 (Expected: False): {}", is_divisible_by_2(number)); | |
| } | |
| // The new test module | |
| #[cfg(test)] | |
| mod tests { | |
| use super::divisibility_tests::*; | |
| #[test] | |
| fn test_divisible_by_2() { | |
| assert_eq!(is_divisible_by_2(42), true); // Even | |
| assert_eq!(is_divisible_by_2(43), false); // Odd | |
| assert_eq!(is_divisible_by_2(0), true); | |
| } | |
| #[test] | |
| fn test_divisible_by_3() { | |
| assert_eq!(is_divisible_by_3(123), true); // Sum: 6 | |
| assert_eq!(is_divisible_by_3(13), false); // Sum: 4 | |
| assert_eq!(is_divisible_by_3(0), true); | |
| } | |
| #[test] | |
| fn test_divisible_by_4() { | |
| assert_eq!(is_divisible_by_4(124), true); // Last two: 24 | |
| assert_eq!(is_divisible_by_4(125), false); // Last two: 25 | |
| assert_eq!(is_divisible_by_4(4), true); | |
| } | |
| #[test] | |
| fn test_divisible_by_5() { | |
| assert_eq!(is_divisible_by_5(105), true); // Ends in 5 | |
| assert_eq!(is_divisible_by_5(110), true); // Ends in 0 | |
| assert_eq!(is_divisible_by_5(111), false); // Ends in 1 | |
| } | |
| #[test] | |
| fn test_divisible_by_6() { | |
| assert_eq!(is_divisible_by_6(42), true); // 42 is divisible by 2 and 3 | |
| assert_eq!(is_divisible_by_6(30), true); | |
| assert_eq!(is_divisible_by_6(14), false); // Divisible by 2, not 3 | |
| assert_eq!(is_divisible_by_6(9), false); // Divisible by 3, not 2 | |
| } | |
| #[test] | |
| fn test_divisible_by_7() { | |
| assert_eq!(is_divisible_by_7(2457), true); // 2457 -> 245 - 14 = 231 -> 23 - 2 = 21 (divisible by 7) | |
| assert_eq!(is_divisible_by_7(1848), true); // 184 - 16 = 168 -> 16 - 16 = 0 (divisible by 7) | |
| assert_eq!(is_divisible_by_7(10), false); | |
| } | |
| #[test] | |
| fn test_divisible_by_8() { | |
| assert_eq!(is_divisible_by_8(1000), true); // Last three: 000 | |
| assert_eq!(is_divisible_by_8(1888), true); // Last three: 888 | |
| assert_eq!(is_divisible_by_8(1004), false); // Last three: 004 | |
| } | |
| #[test] | |
| fn test_divisible_by_9() { | |
| assert_eq!(is_divisible_by_9(2457), true); // Sum: 18 | |
| assert_eq!(is_divisible_by_9(99), true); | |
| assert_eq!(is_divisible_by_9(10), false); // Sum: 1 | |
| } | |
| #[test] | |
| fn test_divisible_by_10() { | |
| assert_eq!(is_divisible_by_10(100), true); // Ends in 0 | |
| assert_eq!(is_divisible_by_10(101), false); // Ends in 1 | |
| } | |
| #[test] | |
| fn test_divisible_by_11() { | |
| assert_eq!(is_divisible_by_11(1331), true); // Alt sum: (1+3) - (3+1) = 0 | |
| assert_eq!(is_divisible_by_11(121), true); // Alt sum: (1+1) - 2 = 0 | |
| assert_eq!(is_divisible_by_11(2457), false); // Alt sum: 4 | |
| assert_eq!(is_divisible_by_11(123456789), false); // Alt sum: 5 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment