Project Euler — problem 17
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
It has been two weeks since my last post on the 16th Euler problem. Now, since I just need a break after supper, I’m coming the 17th problem.
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? NOTE: Do not count spaces or hyphens.
This looks like a simple one, although patience and carefulness are required. For one-digit numbers, it’s one to nine; for two-digit numbers, it’s ten to nineteen for 10-19 and twenty to ninety-nine for 20-29; for three-digit numbers, it’s some hundred and two-digit numbers. Sounds a pattern, but I’ll sum the letters up simply arithmetically. So enough talking, here comes the solution, ugly but correctly…
1 2 3 4 5 6 7 8 | nletter.single <- nchar(c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")) nletter.teen <- nchar(c("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "ninteen")) nletter.ty <- nchar(c("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty")) nletter.less100 <- sum(9 * nletter.single) + sum(nletter.teen) + sum(10 * nletter.ty) nletter.hundreds <- sum(nletter.single) + 9 * nchar("hundred") nletter.more100 <- 100 * nletter.hundreds + 99 * 9 * nchar("and") + 9 * nletter.less100 result <- nletter.less100 + nletter.more100 + nchar("onethousand") cat("The result is:", result, "\n") |
Meanwhile, I’m practising another language — perl. So I also came up with one perl script to convert number (1~999) to word. Just for fun!
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 108 109 110 111 112 | #!/usr/bin/perl # number2word.pl by Tony Wei for covert numbers to words use strict; use warnings; use Getopt::Long; my $NUMBER; my $opts = GetOptions( 'number|i=i' => \$NUMBER, ); my %single_number = ( '1' => 'one', '2' => 'two', '3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six', '7' => 'seven', '8' => 'eight', '9' => 'nine', ); my %teen_number = ( '10' => 'ten', '11' => 'eleven', '12' => 'twelve', '13' => 'thirteen', '14' => 'fourteen', '15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen', '18' => 'eightteen', '19' => 'ninteen', ); my %ty_number = ( '2' => 'twenty', '3' => 'thirty', '4' => 'forty', '5' => 'fifty', '6' => 'sixty', '7' => 'seventy', '8' => 'eighty', '9' => 'ninty', ); if ($NUMBER < 1 || $NUMBER > 999) { print "$NUMBER is beyond the range of 1~999.\n"; } else { my $word; my @digits = split("", $NUMBER); if (scalar(@digits) == 1) { $word = $single_number{$NUMBER}; } elsif (scalar(@digits) == 2) { if ($digits[0] == 1) { $word = $teen_number{$NUMBER}; } elsif ($digits[1] == 0) { $word = $ty_number{$digits[0]}; } else { $word = $ty_number{$digits[0]}."_".$single_number{$digits[1]}; } } else { if ($digits[1] == 0 && $digits[2] == 0) { $word = $single_number{$digits[0]}." hundred"; } else { $word = $single_number{$digits[0]}." hundred and "; if ($digits[1] == 0) { $word = $word.$single_number{$digits[2]}; } elsif ($digits[1] == 1) { $word = $word.$teen_number{join("", @digits[1,2])}; } elsif ($digits[2] == 0) { $word = $word.$ty_number{$digits[1]}; } } else { $word = $ty_number{$digits[0]}."-".$single_number{$digits[1]}; } } else { if ($digits[1] == 0 && $digits[2] == 0) { $word = $single_number{$digits[0]}." hundred"; } else { $word = $single_number{$digits[0]}." hundred and "; if ($digits[1] == 0) { $word = $word.$single_number{$digits[2]}; } elsif ($digits[1] == 1) { $word = $word.$teen_number{join("", @digits[1,2])}; } elsif ($digits[2] == 0) { $word = $word.$ty_number{$digits[1]}; } else { $word = $word.$ty_number{$digits[1]}."-".$single_number{$digits[2]}; } } } print "The word for $NUMBER is '$word'.\n"; } |
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.