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
Notifications
Mark all as read
Q&A

Create a list of Niven numbers in Python

+2
−4

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?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

One element too much (1 comment)
Golfing (1 comment)

2 answers

+3
−0

The way your code handles the variable i within the for loop seems to indicate a wrong understanding of the meaning of

for i in range(1,n+2):

range(1,n+2) will provide an object of type range. This object represents the sequence of numbers from 1 to (not including) n+2. At the begin of each iteration the next element from that sequence is fetched and assigned to i, no matter what happened to i in between.

In other words, you can not "skip" elements from the range object just by incrementing i within the for loop. You can see this with the help of the following example code:

for i in range(3):
    print("Value of i at start of loop body: i = ", i)
    i += 20
    print("Value of i after modification: i = ", i)
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−1

Think of what your for loop is doing, specifically at iterations 11 and 12.

for i in range(1,n+2):
	while not g(i):
		i+=1;continue
	a.append(i)

In pseudo instructions:

  • for-loop iteration 11

    • i = 11
    • while-loop:
      • check not g(i) (i = 11, so it's true)
      • i += 1
      • check not g(i) (i = 12, so it's false)
    • append i (appends 12)
  • for-loop iteration 12

    • i = 12
    • while-loop:
      • check not g(i) (i = 12, so it's false)
    • append i (appends 12)

That is, the problem is that as it is written, it is "for i from 1 to n + 2, get the smallest Niven number greater than i" which is probably not what you want.

One possible fix to this is just to separate the list index counter and the Niven number counter. The implementation is left as an exercise to the reader.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »