Generating Niven numbers in Raku

Print all Niven number up to 50, which are the numbers which are divisible the the sum of their digits.

Welcome to the page with solutions in the Raku programming language, the solutions of the first task of the Week 7 of the Perl Weekly Challenge.

Print all Niven number up to 50, which are the numbers which are divisible the the sum of their digits.

The solution is a one-liner:

.say if $_ %% [+] .comb for 1..50;

For the numbers in the range 1..50, split them into digits: .comb, add up them: [+], and check if the number is divisible by it: if $_ %%. If so, print the current value: .say.

The program prints the following row (shown here in a single line for brevity, but the program prints each number on a separate line):

1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 45 48 50

GitHub repository
Navigation to the Raku challenges post series

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code