# How many three digit numbers are divisible by 17?

Page 1 of 1

## 7 Replies - 8649 Views - Last Post: 21 September 2012 - 06:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=292600&amp;s=ff7ad0cb67204a93557e05ae0407e778&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 luvmybbyjay

Reputation: 0
• Posts: 3
• Joined: 20-September 12

# How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 10:31 AM

I am very new to python, actually I'm a beginner in programming. I'm having such a hard time to get this code right.
I need to print a list of the three-digit numbers that are divisible by 17, and print how many there are in total. This is what I have so far.
I'm sure some of is right, but I get an error in line 7
TypeError: 'int' object is not iterable

```print ("How many three-digit numbers are divisible by 17?")
input("Press Enter to find out...")
list_of_ints = []

for number in range(100,1000):
if number % 17 == 0:
list_of_ints += number #line 7 error

print(list_of_ints)
print(len(list_of_ints))

```

I'm using Python 3

Is This A Good Question/Topic? 0

## Replies To: How many three digit numbers are divisible by 17?

### #2 baavgai

• Dreaming Coder

Reputation: 7166
• Posts: 14,933
• Joined: 16-October 07

## Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 10:32 AM

Using the += operator on lists can be quirky. Prefer append:
```list_of_ints.append(number)

```

Also, investigate Python list comprehensions. You can do that in one line, believe it or not.

### #3 luvmybbyjay

Reputation: 0
• Posts: 3
• Joined: 20-September 12

## Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 10:35 AM

Thank you very much. This really helped a lot, "append" worked.

### #4 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 11:22 AM

Your initial code was great, much better than what I expect to see from people just starting out, good job

I thought the assignment was kind of a fun one, so I decided to take a whack at it. Here it is:
```def get_multiples(minimum, maximum, multiple):
if minimum > maximum:#if minimum is bigger than maximum, swap their values
minimum,maximum = maximum,minimum
results = []
value = minimum + (multiple - (minimum % multiple))
while value < maximum:
results.append(str(value))
value+=multiple
print("All "+str(len(results))+" multiples of "+str(multiple)+" between "+str(minimum)+" and "+str(maximum)+": "+", ".join(results))

```

this one is exclusive, which means that if you want all the multiples of 17 from 100 to 999, you'd say check(100,1000,17). I store them as strings so that I can use the join function

### #5 luvmybbyjay

Reputation: 0
• Posts: 3
• Joined: 20-September 12

## Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 12:08 PM

Thanks, but it took me a while to get that code. I knew I had to get a list and the total amount of numbers just wasn't sure how, fortunately I got it.

That's a little confusing to me still but thanks.

### #6 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: How many three digit numbers are divisible by 17?

Posted 20 September 2012 - 09:32 PM

Hehe, I know my code uses some techniques you might not be familiar with, I'd be happy to discuss any parts you'd like more info on if you're curious

### #7 baavgai

• Dreaming Coder

Reputation: 7166
• Posts: 14,933
• Joined: 16-October 07

## Re: How many three digit numbers are divisible by 17?

Posted 21 September 2012 - 02:12 AM

Well, since we're on to alternatives...

This would get you your list:
```list_of_ints = [ n for n in range(100,1000) if n % 17 == 0 ]

```

However, a more efficient way to get this list would be start at the first value in the list and increment by the value. This is what atraub did and could be one of the source for confusion.

```d = 17 # divisor
start = 100 # bottom threshold
offset = start % d # this is how many we're off
offset = d - offset # the inverse of how much we're off
start = start + offset # first value we can use
list_of_ints = [ n for n in range(start,1000,d) ] # loop, skipping by divisor

```

Now, if you're just looking for the count...
```def threeDigitsDivdedBy(d):
return 999/d - 99/d

```

### #8 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: How many three digit numbers are divisible by 17?

Posted 21 September 2012 - 06:43 AM

Ahh damnit!! I had the same idea while I was laying in bed this morning and you beat me to it!

EDIT:
```def get_multiples(minimum, maximum, multiple):
if minimum > maximum:#if minimum is bigger than maximum, swap their values
minimum,maximum = maximum,minimum
minimum = minimum + (multiple - (minimum % multiple))
return [i for i in range(minimum,maximum,multiple)]

```

To do it in one line (assuming minimum is less than maximum):
```results = [i for i in range((minimum + (multiple - (minimum % multiple))),maximum,multiple)]
```

This post has been edited by atraub: 21 September 2012 - 06:48 AM