Hello there, and for those of you who have read my previous work, welcome back.

Now, what is an Armstrong number:

“In recreational number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong) or a plus perfect number) is a number that is the sum of its own digits each raised to the power of the number of digits.” – wikipedia

So in layman terms, a three digit number abc is narcisistic if a^3 + b^3 + c^3 = abc.

Before we go through the code let’s analyze what we need to do. We need a number to check, we need to find out how many digits it has, we need to raise each individual digit to the power of how many digits there are, add them up and check to see if it’s equal to the original number.

So down below you will see a little script that checks if a number is an armstrong number or not. After we will go through the code line by line to understand what each bit of code does what.

Now we have one class and just 2 methods inside it. The first method is pretty straight forward and doesn’t need to much explaining. It just greets the user, asks for an input, takes that input, turns it into an integer  and passes it to the is_narcissistic? method to be checked.

So in essence the first method – which is public – holds the interface, and the second method – which is private – holds the business logic.

Now onto the second method.

The first line of code just take the user input and turns it into an array of integers corresponding to the digits of the number. so for example 153 will be turned into [1, 5, 3] and saves that array into the variable “all_digits” because we will use that later on.

The second line of code counts the number of digits inside the array and saves that in a variable called “number_of_digits” because we will need this number to be able to raise to power the individual digits inside the “all_digits” array.

The third line of code:

This line of code maps over the array “all_digits” and raises each and every one of them to the power of the “number_of_digits” and saves that inside the “raised_to_power” variable.

The last five lines of code does the check against the original number

It does this with a simple if/else statement in which it checks if the sum of the “raised_to_power” digits is equal to the original “user_input” in which case it prints a statement that tells the user that it truly is a narcissistic number or a statement that it’s not.

And that is all there is too it. Now, do you think you could write up a new program that uses the above code to find out all the narcissistic numbers in a range of, let’s say [1..999] ?

Have a great week!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8183508,"math_prob":0.9811587,"size":3653,"snap":"2020-45-2020-50","text_gpt3_token_len":932,"char_repetition_ratio":0.15922171,"word_repetition_ratio":0.093457945,"special_character_ratio":0.26033399,"punctuation_ratio":0.093706295,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9751073,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-19T16:15:01Z\",\"WARC-Record-ID\":\"<urn:uuid:922f15b7-d24d-4444-a8e9-a8ecfce6ad81>\",\"Content-Length\":\"81487\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fab03616-d7ff-49eb-af88-972a3d0dbabd>\",\"WARC-Concurrent-To\":\"<urn:uuid:c2fa12db-e553-49a7-9ae2-a86e8f979cbd>\",\"WARC-IP-Address\":\"67.205.30.193\",\"WARC-Target-URI\":\"http://www.devslife.com/how-to-write-an-armstrong-number-checker/\",\"WARC-Payload-Digest\":\"sha1:75O7V4JDLATNWDADQCEG7XZVNGY5COJT\",\"WARC-Block-Digest\":\"sha1:BFTKUYXWCV52ILF4DQ673E4N23BLYNER\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107863364.0_warc_CC-MAIN-20201019145901-20201019175901-00461.warc.gz\"}"}