-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactor.rs
62 lines (57 loc) · 962 Bytes
/
factor.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
fn factorize(mut num: i32) -> Vec<i32> {
let mut i = 2;
let mut v: Vec<i32> = Vec::new();
while i * i <= num {
while num % i == 0 {
println!("{:}", i);
num = num / i;
v.push(i);
}
i += 1;
}
if num > 1 {
println!("{:}", num);
v.push(num);
}
return v;
}
fn main() {
println!("factorize:");
let mut num = 20;
println!("number is: {:}", num);
let v: Vec<i32> = factorize(num);
println!("{:?}", v);
num = 140;
println!("number is: {:}", num);
let v: Vec<i32> = factorize(num);
println!("{:?}", v);
num = 120;
println!("number is: {:}", num);
let arr = factorize(num);
println!("{:?}", arr);
}
/*
jarry@jarrys-MacBook-Pro rust % rustc -V
rustc 1.51.0 (2fd73fabe)
jarry@jarrys-MacBook-Pro factor % rustc factor.rs
jarry@jarrys-MacBook-Pro factor % ./factor
factorize:
number is: 20
2
2
5
[2, 2, 5]
number is: 140
2
2
5
7
[2, 2, 5, 7]
number is: 120
2
2
2
3
5
[2, 2, 2, 3, 5]
*/