{"id":5456,"date":"2018-04-29T13:00:00","date_gmt":"2018-04-29T13:00:00","guid":{"rendered":"http:\/\/writeasync.net\/?p=5456"},"modified":"2018-04-29T07:15:10","modified_gmt":"2018-04-29T07:15:10","slug":"know-your-large-numbers","status":"publish","type":"post","link":"http:\/\/writeasync.net\/?p=5456","title":{"rendered":"Know your (large) numbers"},"content":{"rendered":"<p>As a child, I was fascinated by large numbers. I recall being unbelievably excited when my dad gave me a clipping of a newspaper trivia column which listed the names of numbers from a million up to <a href=\"https:\/\/www.merriam-webster.com\/dictionary\/decillion\">decillion<\/a>. While most of us had some practical knowledge of millions, billions, and trillions, the numbers beyond that were far more obscure. It felt good to be privy to even a small part of the arcana of numerical nomenclature.<\/p>\n<p>Not long after that, I stumbled upon a list of large numbers up to <a href=\"https:\/\/www.merriam-webster.com\/dictionary\/vigintillion\">vigintillion<\/a> in the phone book-sized dictionary in my elementary school classroom. This was incredible! Now I could give precise names to any number up to about 10<sup>65<\/sup>. Still, knowing that there were numbers even bigger than that, I was not satisfied.<\/p>\n<p>Much later, I was able to turn to the internet to resolve many previous research roadblocks. There I found references to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Names_of_large_numbers#Extensions_of_the_standard_dictionary_numbers\">far larger numbers<\/a> using extensions to the established Latin-based conventions &#8212; trigintillion, quadragintillion, and so on. By then, the early magic had faded, yet my idle curiosity about large numbers never quite went away. In fact, one of the first programs I wrote while learning C# was a &#8220;numbers to words&#8221; converter which supported thousands of digits.<\/p>\n<p>Since I&#8217;m feeling nostalgic, let&#8217;s recreate that program now! First the boilerplate to read the number (as a string &#8212; since we will quickly shoot past the limit of a primitive integral type) and handle cases like negative and zero:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nnamespace n2w\r\n{\r\n    using System;\r\n    using System.Collections.Generic;\r\n    using System.Linq;\r\n\r\n    internal sealed class Program\r\n    {\r\n        private const string Negative = &quot;negative&quot;;\r\n        private const string Zero = &quot;zero&quot;;\r\n\r\n        public static void Main(string&#x5B;] args)\r\n        {\r\n            string words;\r\n            try\r\n            {\r\n                words = ToWords(Num(args));\r\n            }\r\n            catch (FormatException e)\r\n            {\r\n                words = &quot;ERROR: &quot; + e.Message;\r\n            }\r\n\r\n            Console.WriteLine(words);\r\n        }\r\n\r\n        private static string Num(string&#x5B;] args)\r\n        {\r\n            if (args.Length != 1)\r\n            {\r\n                throw new FormatException(&quot;Invalid arguments.&quot;);\r\n            }\r\n\r\n            return args&#x5B;0].Trim();\r\n        }\r\n\r\n        private static string ToWords(string number)\r\n        {\r\n            string prefix = null;\r\n            if ((number.Length &gt; 0) &amp;&amp; (number&#x5B;0] == '-'))\r\n            {\r\n                prefix = Negative + ' ';\r\n                number = number.Substring(1);\r\n            }\r\n\r\n            if (number.Length == 0)\r\n            {\r\n                throw new FormatException(&quot;No digits.&quot;);\r\n            }\r\n\r\n            string words = string.Join(&quot; &quot;, Groups(number).Where(g =&gt; g != null));\r\n            if (words.Length == 0)\r\n            {\r\n                return Zero;\r\n            }\r\n\r\n            return prefix + words;\r\n        }\r\n\r\n        private static IEnumerable&lt;string&gt; Groups(string number)\r\n        {\r\n            yield return null; \/\/ TODO\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>We&#8217;ve laid the groundwork here to separate the number into groups of digits and then name each group. Right now, though, it simply calls every number &#8220;zero.&#8221; Let&#8217;s extend this to name every number up to 999,999 &#8212; what I&#8217;ll call the &#8220;small numbers.&#8221;<\/p>\n<p>To name small numbers in English, we need to know only a few dozen unique words and a few rules, to be applied in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Decimal_separator#Digit_grouping\">digit groupings<\/a> of three. So, we end up with the single digits, the numbers for the multiples of 10 up to 90, the special cases for 11-19, and the suffixes for 100 and 1000. We combine names of 1000s, 100s, 10s and digits to build up the word in groups, omitting any portion if it is zero (since we don&#8217;t say &#8220;two thousand zero hundred&#8221; for 2000). Add to that the one <a href=\"https:\/\/www.grammarly.com\/blog\/hyphen-in-compound-numbers\/\">orthographical peculiarity<\/a> where we hyphenate 10s names like &#8220;twenty-one&#8221; and we have a complete algorithm:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/...\r\nprivate static IEnumerable&lt;string&gt; Groups(string number)\r\n{\r\n    int start = number.Length % 3;\r\n    int n = (number.Length - 1) \/ 3;\r\n    if (start == 1)\r\n    {\r\n        yield return Group('0', '0', number&#x5B;0], n--);\r\n    }\r\n    else if (start == 2)\r\n    {\r\n        yield return Group('0', number&#x5B;0], number&#x5B;1], n--);\r\n    }\r\n\r\n    for (int i = start; i &lt; number.Length; i += 3)\r\n    {\r\n        yield return Group(number&#x5B;i], number&#x5B;i + 1], number&#x5B;i + 2], n--);\r\n    }\r\n}\r\n\r\nprivate static string Group(char h, char t, char u, int n)\r\n{\r\n    string g = Small.Hundreds(D(h), D(t), D(u));\r\n    if (g == null)\r\n    {\r\n        return null;\r\n    }\r\n\r\n    return Join(g, ' ', Small.Suffix(n != 0));\r\n}\r\n\r\nprivate static string Join(string a, char s, string b) =&gt; (b == null) ? a : a + s + b;\r\n\r\nprivate static int D(char c)\r\n{\r\n    int d = c - '0';\r\n    if ((d &lt; 0) || (d &gt; 9))\r\n    {\r\n        throw new FormatException(&quot;Bad digit '&quot; + c + &quot;'.&quot;);\r\n    }\r\n\r\n    return d;\r\n}\r\n\r\nprivate static class Small\r\n{\r\n    private static readonly string&#x5B;] Units = new string&#x5B;]\r\n    {\r\n        null, &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;\r\n    };\r\n\r\n    private static readonly string&#x5B;] Teens = new string&#x5B;]\r\n    {\r\n        &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;\r\n    };\r\n\r\n    private static readonly string&#x5B;] Tens = new string&#x5B;]\r\n    {\r\n        null, null, &quot;twenty&quot;, &quot;thirty&quot;, &quot;forty&quot;, &quot;fifty&quot;, &quot;sixty&quot;, &quot;seventy&quot;, &quot;eighty&quot;, &quot;ninety&quot;\r\n    };\r\n\r\n    private static readonly string&#x5B;] Names = new string&#x5B;] { null, &quot;hundred&quot;, &quot;thousand&quot; };\r\n\r\n    public static string Suffix(bool thousand) =&gt; Names&#x5B;thousand ? 2 : 0];\r\n\r\n    public static string Hundreds(int h, int t, int u)\r\n    {\r\n        switch (h)\r\n        {\r\n            case 0: return TensPart(t, u);\r\n            default: return Join(Units&#x5B;h] + ' ' + Names&#x5B;1], ' ', TensPart(t, u));\r\n        }\r\n    }\r\n\r\n    private static string TensPart(int t, int u)\r\n    {\r\n        switch (t)\r\n        {\r\n            case 0: return Units&#x5B;u];\r\n            case 1: return Teens&#x5B;u];\r\n            default: return Join(Tens&#x5B;t], '-', Units&#x5B;u]);\r\n        }\r\n    }\r\n}\r\n\/\/...\r\n<\/pre>\n<p>This is a start, but remember that we&#8217;re talking about large numbers here! The program so far will name every grouping &#8220;thousand&#8221; so we need to teach it the rules of big numbers. Above I summarized what are sometimes called &#8220;dictionary numbers&#8221; containing the names from million up to vigintillion. The extension to this system by Conway and Guy is <a href=\"https:\/\/sites.google.com\/site\/largenumbers\/home\/2-4\/6\">explained fairly well by Sbiis.ExE<\/a> (whose <a href=\"https:\/\/sites.google.com\/site\/largenumbers\/home\/information_desk\/about_website\">large numbers journey<\/a> bears more than a passing similarity to my own). But basically, it uses the dictionary numbers as a starting point, and extrapolates Latin names for the units, tens, and hundreds places of ever larger number groups. For example, 10<sup>39<\/sup> = 10<sup>3N+3<\/sup> where N=12, which gives us duodecillion &#8212; the ones portion is duo = 2 and the tens portion is deci = 10. Similarly, for 10<sup>1002<\/sup>, N=333, or tre (3) + triginti (30) + trecent(i) (300) + illion (note that we drop the last vowel before adding the -illion suffix).<\/p>\n<p>Now for the changes to make the program observe these rules. First, we need to edit the <code>Group<\/code> function to handle the big suffixes:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ OLD CODE:\r\n\/\/   return Join(g, ' ', Small.Suffix(n != 0));\r\n\/\/ NEW CODE:\r\nreturn Join(g, ' ', (n &lt; 2) ? Small.Suffix(n != 0) : Big.Suffix(n - 1));\r\n<\/pre>\n<p>And now the rules above, codified into a private inner class <code>Big<\/code>:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate static class Big\r\n{\r\n    private const string Ending = &quot;illion&quot;;\r\n\r\n    private static readonly string&#x5B;] Ones = new string&#x5B;]\r\n    {\r\n        null, &quot;m&quot;, &quot;b&quot;, &quot;tr&quot;, &quot;quadr&quot;, &quot;quint&quot;, &quot;sext&quot;, &quot;sept&quot;, &quot;oct&quot;, &quot;non&quot;\r\n    };\r\n\r\n    private static readonly string&#x5B;] Units = new string&#x5B;]\r\n    {\r\n        null, &quot;un&quot;, &quot;duo&quot;, &quot;tre&quot;, &quot;quattuor&quot;, &quot;quinqua&quot;, &quot;sex&quot;, &quot;septem&quot;, &quot;octo&quot;, &quot;novem&quot;\r\n    };\r\n\r\n    private static readonly string&#x5B;] Tens = new string&#x5B;]\r\n    {\r\n        null, &quot;deci&quot;, &quot;viginti&quot;, &quot;triginti&quot;, &quot;quadraginta&quot;, &quot;quinquaginta&quot;, &quot;sexaginta&quot;, &quot;septuaginta&quot;, &quot;octoginta&quot;, &quot;nonaginta&quot;\r\n    };\r\n\r\n    private static readonly string&#x5B;] Hundreds = new string&#x5B;]\r\n    {\r\n        null, &quot;centi&quot;, &quot;ducenti&quot;, &quot;trecenti&quot;, &quot;quadringenti&quot;, &quot;quingenti&quot;, &quot;sescenti&quot;, &quot;septingenti&quot;, &quot;octingenti&quot;, &quot;nongenti&quot;\r\n    };\r\n\r\n    public static string Suffix(int n)\r\n    {\r\n        string prefix;\r\n        if (n &lt; 10)\r\n        {\r\n            prefix = Ones&#x5B;n];\r\n        }\r\n        else\r\n        {\r\n            prefix = Units&#x5B;n % 10] + Tens&#x5B;(n \/ 10) % 10] + Hundreds&#x5B;n \/ 100];\r\n            prefix = prefix.Substring(0, prefix.Length - 1);\r\n        }\r\n        \r\n        return prefix + Ending;\r\n    }\r\n}\r\n<\/pre>\n<p>It is possible to extend this naming system even further, but I think 10<sup>3002<\/sup> is a good stopping point for today&#8217;s program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a child, I was fascinated by large numbers. I recall being unbelievably excited when my dad gave me a clipping of a newspaper trivia column which listed the names of numbers from a million up to decillion. While most&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103],"tags":[],"class_list":["post-5456","post","type-post","status-publish","format-standard","hentry","category-math"],"_links":{"self":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5456"}],"version-history":[{"count":1,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5456\/revisions"}],"predecessor-version":[{"id":5457,"href":"http:\/\/writeasync.net\/index.php?rest_route=\/wp\/v2\/posts\/5456\/revisions\/5457"}],"wp:attachment":[{"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5456"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/writeasync.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}