DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Simon Green
Simon Green

Posted on

Weekly Challenge 174

Challenge, My solution

Just doing one task this week. The second one is nothing more than copying and pasting code from the mentioned website, so I cannot see the point. YMMV.

Task: Disarium Numbers

Write a script to generate first 19 Disarium Numbers.

A disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.

My solution

This is pretty straight forward. Have a list (array in Perl) called solutions. Keep iterating a number until their are nineteen items in that list. Add the number to the list if the number is disarium.

To determine disarium numbers I loop over each digit using enumerate(str(n)). The enumerate function is roughly equivalent to using the each function over an array in Perl (the first value is the counter, the second value is the digit at that position).

If the sum is the original number, return True otherwise return False.

The Perl code is similar to the Python code. The code to calculate the sum is slightly different.

Examples

$ ./ch-1.py 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676, 2427, 2646798

$ ./ch-1.pl 
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676, 2427, 2646798
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

๐ŸŒš Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.