Files
CodeTest/baekjoon/소인수분해/solution_19801979.rs
2024-08-29 16:22:23 +09:00

45 lines
927 B
Rust

macro_rules! parse_line {
($($t: ty),+) => ({
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
let mut iter = line.split_whitespace();
($(iter.next().unwrap().parse::<$t>().unwrap()),+)
})
}
fn factor(n: u32) -> String{
let mut result = String::new();
let mut temp = n;
while temp % 2 == 0{
temp = temp / 2;
result.push_str("2\n");
}
let mut fac = 3;
while fac * fac <= temp{
while temp % fac == 0{
temp = temp / fac;
result.push_str(format!("{}\n", fac).as_str());
}
fac += 2;
}
if temp > 1{
result.push_str(format!("{}\n", temp).as_str());
}
let len = result.len();
if len == 0{
return "".to_string();
}
else{
return result[..(len-1)].to_string();
}
}
fn main(){
let n = parse_line!(u32);
println!("{}", factor(n));
}