You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.5 KiB

fn less_20(n: u32) -> String {
if n == 1 {
return String::from("one");
}
if n == 2 {
return String::from("two");
}
if n == 3 {
return String::from("three");
}
if n == 4 {
return String::from("four");
}
if n == 5 {
return String::from("five");
}
if n == 6 {
return String::from("six");
}
if n == 7 {
return String::from("seven");
}
if n == 8 {
return String::from("eight");
}
if n == 9 {
return String::from("nine");
}
if n == 10 {
return "ten".to_string();
}
if n == 11 {
return "eleven".to_string();
}
if n == 12 {
return "twelve".to_string();
}
if n == 13 {
return "thirteen".to_string();
}
if n == 14 {
return "fourteen".to_string();
}
if n == 15 {
return "fifteen".to_string();
}
if n == 16 {
return "sixteen".to_string();
}
if n == 17 {
return "seventeen".to_string();
}
if n == 18 {
return "eighteen".to_string();
}
if n == 19 {
return "nineteen".to_string();
}
return String::from("idontknow");
}
fn tens_from_20(n: u32) -> String {
if n == 20 {
return "twenty".to_string();
}
if n == 30 {
return "thirty".to_string();
}
if n == 40 {
return "forty".to_string();
}
if n == 50 {
return "fifty".to_string();
}
if n == 60 {
return "sixty".to_string();
}
if n == 70 {
return "seventy".to_string();
}
if n == 80 {
return "eighty".to_string();
}
if n == 90 {
return "ninety".to_string();
}
return "what".to_string();
}
fn to_99(n: u32) -> String {
if n < 20 {
return less_20(n);
}
if n % 10 == 0 {
return tens_from_20(n);
}
let l = (n / 10) * 10;
let r = n % 10;
let mut res = tens_from_20(l);
res.push_str(&less_20(r));
return res;
}
fn to_999(n: u32) -> String {
if n < 100 {
return to_99(n);
}
let ll = n / 100;
let rr = n % 100;
let mut res = less_20(ll);
res.push_str("hundred");
if rr != 0 {
res.push_str("and");
res.push_str(&to_99(rr));
}
return res;
}
fn main() {
let mut res = 0;
for i in 1..=999 {
let lll = to_999(i);
//println!("{}", lll);
res += lll.len();
}
res += "onethousand".to_string().len();
println!("{}", res);
}