Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Create a list of Niven numbers in Python
Post
Create a list of Niven numbers in Python
There's this new challenge on Code Golf CD and I'm using Python to do it. A little bit of golfing already took place, so the code might look a bit messy for you. Anyway, I'm proud of what I've written so far:
def g(n):
s=0;t=n
while t>0:s+=t%10;t//=10
return n%s==0
def f(n):
a=[]
for i in range(1,n+2):
while not g(i):
i+=1;continue
a.append(i)
return a
First function checks for Niven numbers, the second one creates a list for them. Problem that occurred is something like this since I can't word it.
If I'd input 14 into the function, I should get:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24]
But instead, I'd get this:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 12, 18, 18, 18]
Any ways to fix this?
2 comment threads