-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_1.rs
107 lines (101 loc) · 1.76 KB
/
task_1.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::utils::{
input::{get_numeric_input, invalid_input},
Error,
};
const MIN: u32 = 2;
const MAX: u32 = 2000;
/// # HOMEWORK 2 | TASK 1
///
/// ## Input
///
/// 1. One number
///
/// ### Constaints
///
/// 2. The input should be in the inclusive range of: `[2;2000]`.
///
/// ### Output
///
/// The count of all the positive numbers up to (and including) `n` which are divisible by `n`.
///
/// ### Sample
///
/// #### Input Format
///
/// ```txt
/// 200
/// ```
///
/// #### Output Format
///
/// ```txt
/// 12
/// ```
///
/// #### Explanation
///
/// The count of the numbers, divisble by n is 12 and they are:
///
/// 1 - 1
/// 2 - 2
/// 3 - 4
/// 4 - 5
/// 5 - 8
/// 6 - 10
/// 7 - 20
/// 8 - 25
/// 9 - 40
/// 10 - 50
/// 11 - 100
/// 12 - 200
///
/// ### Error
///
/// If the input is invalid, return:
///
/// ```rust
/// String::from("Invalid input data!");
/// ```
///
/// ## Test cases
///
/// ```rust
/// use hackerrank::hw2::task_1::Solution;
///
/// assert_eq!(
/// Solution::main(200),
/// String::from("12")
/// );
/// assert_eq!(
/// Solution::main(40),
/// String::from("8")
/// );
/// assert_eq!(
/// Solution::main(2001),
/// String::from("Invalid input data!")
/// );
/// assert_eq!(
/// Solution::main(7),
/// String::from("2")
/// );
/// ```
pub struct Solution;
impl Solution {
pub fn main(n: u32) -> String {
if !(MIN..=MAX).contains(&n) {
return invalid_input();
}
let mut result = 0;
for i in 1..=n {
if n % i == 0 {
result += 1;
}
}
format!("{}", result)
}
}
pub fn main() -> Result<(), Error> {
let n = get_numeric_input::<u32>()?;
println!("{}", Solution::main(n));
Ok(())
}