Thicket data repository for the EEG
1{
2 "id": "https://www.tunbury.org/2014/01/02/narcissistic-numbers",
3 "title": "Narcissistic Numbers",
4 "link": "https://www.tunbury.org/2014/01/02/narcissistic-numbers/",
5 "updated": "2014-01-02T12:41:29",
6 "published": "2014-01-02T12:41:29",
7 "summary": "I heard about these on BBC Radio 4 More or Less and they just intrigued me, perhaps in part because they have no known application! In the past similar obsessions have appeared with the calculation of PI and right back to my childhood calculating powers of 2 on a BBC Micro.",
8 "content": "<p>I heard about these on <a href=\"http://www.bbc.co.uk/programmes/b006qshd\">BBC Radio 4 More or\nLess</a> and they just intrigued\nme, perhaps in part because they have no known application! In the past\nsimilar obsessions have appeared with the calculation of PI and right\nback to my childhood calculating powers of 2 on a BBC Micro.</p>\n\n<p>The full definition, as for everything, is on\n<a href=\"https://en.wikipedia.org/wiki/Narcissistic_number\">Wikipedia</a> but in\nshort a narcissistic number is one where the sum of the digits raised to\nthe power of the number of digits equals the number itself. For example</p>\n\n\\[153 = 1^3 + 5^3 + 3^3\\]\n\n<p>Here’s some quick and dirty Perl code to calculate them:</p>\n\n<div><div><pre><code>use strict;\nuse warnings;\n\nfor (my $i = 10; $i < 10000; $i++) {\n my $pwr = length($i);\n my $total = 0;\n for (my $j = 0; $j < $pwr; $j++) {\n $total += int(substr $i, $j, 1) ** $pwr;\n }\n if ($total == $i) {\n print $i . \" is narcissistic\\n\";\n }\n}\n</code></pre></div></div>\n\n<p>This yields this output</p>\n\n<div><div><pre><code>153 is narcissistic\n370 is narcissistic\n371 is narcissistic\n407 is narcissistic\n1634 is narcissistic\n8208 is narcissistic\n9474 is narcissistic\n</code></pre></div></div>\n\n<p>However, due to the typical limitation in the implementation of integers\nthis doesn’t get you very far. Perl’s <code>Math::BigInt</code> gets you further if\nyou are very patient</p>\n\n<div><div><pre><code>use strict;\nuse warnings;\nuse Math::BigInt;\n\nmy $i = Math::BigInt->bone();\n\nwhile ((my $pwr = $i->length()) < 10) {\n my $total = Math::BigInt->bzero;\n for (my $j = 0; $j < $pwr; $j++) {\n my $t = Math::BigInt->new($i->digit($j));\n $total->badd($t->bpow($pwr));\n }\n if ($total == $i) {\n print $i . \" is narcissistic\\n\";\n }\n $i->binc();\n}\n</code></pre></div></div>",
9 "content_type": "html",
10 "author": {
11 "name": "Mark Elvers",
12 "email": "mark.elvers@tunbury.org",
13 "uri": null
14 },
15 "categories": [
16 "perl"
17 ],
18 "source": "https://www.tunbury.org/atom.xml"
19}