<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/euler-problem-17-number-letter-counts/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-17-number-letter-counts/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/writtennumbers.jpg?fit=350%2C336" data-orig-size="350,336" data-image-title="Euler Problem 17: Written numbers" class="wp-image-236" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="236" data-permalink="http://r.prevos.net/euler-problem-17-number-letter-counts/writtennumbers/" alt="Euler Problem 17: written numbers" data-medium-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/writtennumbers.jpg?fit=300%2C288" data-large-file="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/writtennumbers.jpg?fit=350%2C336" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/writtennumbers.jpg?resize=300%2C288">Euler Problem 17 asks to count the letters in numbers written as words. This is a skill we all learnt in primary school mainly useful when writing cheques—to those that still use them.</p>
<p>Each language has its own rules for writing numbers. My native language Dutch has very different logic to English. Both Dutch and English use a single syllable until the number twelve. Linguists have theorised this is evidence that early Germanic numbers were duodecimal. This factoid is supported by the importance of a “dozen” as a counting word and the twelve hours in the clock. There is even a Dozenal Society that promotes the use of a number system based on 12.</p>
<p><iframe width="450" allowfullscreen="true" src="https://www.youtube.com/embed/U6xJfP7-HCc?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" type="text/html"></iframe></p>
<p>The English language changes the rules when reaching the number 21. While we say eight-teen in English, we do no say “one-twenty”. Dutch stays consistent and the last number is always spoken first. For example, 37 in English is “thirty-seven”, while in Dutch it is written as “zevenendertig” (seven and thirty).</p>
<h2>Euler Problem 17 Definition</h2>
<p>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, <a target="_blank" rel="nofollow" href="https://projecteuler.net/problem=17">how many letters would be used</a>?</p>
<p>NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.</p>
<h2>Solution</h2>
<p>The first piece of code provides a function that generates the words for numbers 1 to 999,999. This is more than the problem asks for, but it might be a useful function for another application. The last line concatenates all words together and removes the spaces.</p>
<pre>
numword.en &lt;- function(x) { if (x > 999999) return(&quot;Error: Oustide my vocabulary&quot;)
    # Vocabulary 
    single &lt;- c(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;)
    teens &lt;- c( &quot;ten&quot;, &quot;eleven&quot;, &quot;twelve&quot;, &quot;thirteen&quot;, &quot;fourteen&quot;, &quot;fifteen&quot;, &quot;sixteen&quot;, &quot;seventeen&quot;, &quot;eighteen&quot;, &quot;nineteen&quot;)
    tens &lt;- c(&quot;ten&quot;, &quot;twenty&quot;, &quot;thirty&quot;, &quot;forty&quot;, &quot;fifty&quot;, &quot;sixty&quot;, &quot;seventy&quot;, &quot;eighty&quot;, &quot;ninety&quot;)
    # Translation
    numword.10 &lt;- function (y) {
        a &lt;- y %% 100
        if (a != 0) {
            and &lt;- ifelse(y > 100, &quot;and&quot;, &quot;&quot;)
            if (a &lt; 20)
                return (c(and, c(single, teens)[a]))
            else
                return (c(and, tens[floor(a / 10)], single[a %% 10]))
        }
    }
    numword.100 &lt;- function (y) {
        a &lt;- (floor(y / 100) %% 100) %% 10
        if (a != 0)
            return (c(single[a], &quot;hundred&quot;))
    }
    numword.1000 &lt;- function(y) {
        a &lt;- (1000 * floor(y / 1000)) / 1000
        if (a != 0)
            return (c(numword.100(a), numword.10(a), &quot;thousand&quot;))
    }
    numword &lt;- paste(c(numword.1000(x), numword.100(x), numword.10(x)), collapse=&quot; &quot;)
    return (trimws(numword))
}

answer &lt;- nchar(gsub(&quot; &quot;, &quot;&quot;, paste0(sapply(1:1000, numword.en), collapse=&quot;&quot;)))
print(answer)
</pre>
<h2>Writing Numbers in Dutch</h2>
<p>I went beyond Euler Problem 17 by translating the code to spell numbers in Dutch. Interesting bit of trivia is that it takes 307 fewer characters to spell the numbers 1 to 1000 in Dutch than it does in English.</p>
<p>It would be good if other people can submit functions for other languages in the comment section. Perhaps we can create an R package with a multi-lingual function for spelling numbers.</p>
<pre>
numword.nl &lt;- function(x) { if (x > 999999) return(&quot;Error: Getal te hoog.&quot;)
    single &lt;- c(&quot;een&quot;, &quot;twee&quot;, &quot;drie&quot;, &quot;vier&quot;, &quot;vijf&quot;, &quot;zes&quot;, &quot;zeven&quot;, &quot;acht&quot;, &quot;nenen&quot;)
    teens &lt;- c( &quot;tien&quot;, &quot;elf&quot;, &quot;twaalf&quot;, &quot;dertien&quot;, &quot;veertien&quot;, &quot;fifteen&quot;, &quot;zestien&quot;, &quot;zeventien&quot;, &quot;achtien&quot;, &quot;nenentien&quot;)
    tens &lt;- c(&quot;tien&quot;, &quot;twintig&quot;, &quot;dertig&quot;, &quot;veertig&quot;, &quot;vijftig&quot;, &quot;zestig&quot;, &quot;zeventig&quot;, &quot;tachtig&quot;, &quot;negengtig&quot;)
    numword.10 &lt;- function(y) {
        a &lt;- y %% 100
        if (a != 0) {
            if (a &lt; 20)
                return (c(single, teens)[a])
            else
                return (c(single[a %% 10], &quot;en&quot;, tens[floor(a / 10)]))
        }
    }
    numword.100 &lt;- function(y) {
        a &lt;- (floor(y / 100) %% 100) %% 10 if (a == 1) return (&quot;honderd&quot;) if (a > 1) 
            return (c(single[a], &quot;honderd&quot;))
    }
    numword.1000 &lt;- function(y) {
        a &lt;- (1000 * floor(y / 1000)) / 1000 if (a == 1) return (&quot;duizend &quot;) if (a > 0)
            return (c(numword.100(a), numword.10(a), &quot;duizend &quot;))
    }
    numword&lt;- paste(c(numword.1000(x), numword.100(x), numword.10(x)), collapse=&quot;&quot;)
    return (trimws(numword))
}

antwoord &lt;- nchar(gsub(&quot; &quot;, &quot;&quot;, paste0(sapply(1:1000, numword.nl), collapse=&quot;&quot;)))
print(antwoord)

print(answer - antwoord)
</pre>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-17-number-letter-counts/">Euler Problem 17: Number Letter Counts</a> appeared first on <a target="_blank" rel="nofollow" href="http://r.prevos.net/">The Devil is in the Data</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/euler-problem-17-number-letter-counts/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/i7jDPNvs_m4"></a></div>