Recently, I cam across Code Golf, a site where people compete for writing the smallest piece of code to solve a challenge and people are ranked according to how many few characters does it take to run the code.
This post describes my solution to solve Niven number challenge in the Code Golf site , where I got the 72th position.😬 not a very good score, but in this challenge I got to experience writing super small functional code.
So the problem statement goes something like this:
A Niven number is a positive integer that is divisible by the sum of its digits.
Print all the Niven numbers from 1 to 10,000 inclusive, each on their own line.
for i in range(1,10001): if(i%sum(list(map(int,str(i).strip())))==0):print(i)
First we take the input and convert to string and split it into a list and then us sum on those values. This gives us the sum of digits and then to get the result we check if the mod value is 0. If yes, then we print the value for it.