Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

45%
+3 −4
Q&A 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 writ...

2 answers  ·  posted 2y ago by General Sebast1an‭  ·  last activity 2y ago by Dirk Herrmann‭

Question python function
#2: Post edited by user avatar Alexei‭ · 2022-08-22T07:54:18Z (over 2 years ago)
Put a more relevant title
  • I can't title this either; can't word the issue
  • Create a list of Niven numbers in Python
#1: Initial revision by user avatar General Sebast1an‭ · 2022-08-20T03:57:29Z (over 2 years ago)
I can't title this either; can't word the issue
There's this [new challenge](https://codegolf.codidact.com/posts/286855/) 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](https://tio.run/##PY1BCsIwEEXXySmyKaRUbKK4aRkvIi6CTeqATEI6RTx9TFXcvsf/L734HulYyuSDmjW1gxQLmJGBpHje8eEVn82wdMCNrbjvwRopsuc1k6JmATBSbuPwHTu4XKUIMStUSCo7mr22O@oOm/1dUuQaww8R2IEdb5EYafUVuL1LydNU/T/kSspIrIO2p7Ytbw) so far:

```python
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?