0

Using a while and an if statement i need to write the function div_3_5 (start, end) that computes the number of integers from start up to, but not including end that are divisible by 3 or 5.

I do not need to list the numbers just state how many there are.

I get an error either saying i need a return statement or that when variables are given the answer is incorrect using this code.

def div_3_5(start, end):
    x = 0
    while start < end:
        x = x + start
        start = start + 1
        if (x%3 == 0) or (x%5 == 0):    
            return x
5
  • 4
    I think you should put some print lines in to see how the variables change; that is not doing what you want it to. Better variable names (e.g. current and total) would also help you visualise the process. Commented Aug 10, 2014 at 8:19
  • Sometimes just explaining to yourself (and here us) can help make problems like this simpler. Can you tell us what the return keyword actually does? When it runs, what does it return from, and where does it return to? Therefore, what happens when the if statement runs the first time? And the second time? Does it run a second time? Commented Aug 10, 2014 at 8:22
  • 2
    In that case, it sounds like you should also have a Desktop IDE on your computer so that you can make little tests like these. Programming is only going to get more complicated, and no matter how good you are, you are still going to write bugs into your code. Learning to debug is probably the most important skill of all in programming, and if this MyPyTutor isn't allowing you to debug properly, you also need a tool alongside it which does. Commented Aug 10, 2014 at 8:28
  • Then use something else so you can actually see what's happening. Install Python on your computer, or use e.g. repl.it. Commented Aug 10, 2014 at 8:28
  • You aren't accumulating a count of the matching integers anywhere. X is not a count of them as it is incremented by "start" and not incremented when there is a match, but on every loop. Commented Aug 10, 2014 at 8:55

6 Answers 6

2

You can use generate a 1 for every number in the range that is either divisible by 3 or by 5, than sum all the 1s:

def div_3_5(start, end):
    return sum(1 for x in xrange(start, end) if (x % 3 == 0) or (x % 5 == 0))

Additional Trick

If needed you could also create a generator of such methods:

def div_generator(divs):
    return lambda start, end: sum(
        1 for x in xrange(start, end)
          if any(x % div == 0 for div in divs))

div_3_5 = div_generator([3, 5])
Sign up to request clarification or add additional context in comments.

Comments

2

For any number x, there's x//n numbers between 1 and x (inclusive) that are divisible by n. Therefore, there's x//3+x//5-x//15 numbers divisible by 3 or 5 (using the inclusion-exclusion principle).

This gives you an O(1) way to calculate your function:

def d35(x):
    return x//3 + x//5 - x//15

def div3or5(start, end):
    return d35(end-1) - d35(start-1)

Comments

2

Shouldn't your code look more like this:

def div_3_5(start, end):
    x = 0
    while start < end:
        if (start%3 == 0) or (start%5 == 0):
            x += 1
        start = start + 1  
    print x
    return x

Call:

div_3_5(1,9)

Output:

3

2 Comments

Your code is completly invalid. Why are you checking if counter value is divisible by 3 or 5?
Thank you! That is the result I was looking for. This is the first time I have used python and I am still learning.
0

Your problem is that you return when you meet the first criteria, you need to count all matches and then return.

You can try that method which return a list that matches the criteria:

def div_3_5(start, end):
    numbers_3_5 = []
    for number in range(start, end):
        if number % 3 == 0 or number % 5 == 0:
            numbers_3_5.append(number)
    return len(numbers_3_5)

Example:

print div_3_5(3, 10)

Output:

   4

Edit:

By a while loop:

def div_3_5(start, end):
    numbers_3_5 = []
    while start < end:
        if start % 3 == 0 or start % 5 == 0:
            numbers_3_5.append(start)
        start += 1
    return len(numbers_3_5)

2 Comments

This at least produces the right output and returns.
The question requires a while loop
0

when you use return, compiler exit the loop on first find. you can put a counter like below to find how many answer are there. also I'm curios about your algorithm. the part x = x + start, you are adding up all numbers from start to end, is it your objective? if so, the code should be:

def div_3_5(start, end):
    x = 0
    y = 0
    while start < end:
        x = x + start
        start = start + 1
        if (x%3 == 0) or (x%5 == 0):    
            y = y +1

    return y


print div_3_5(1,8)

the output is: 5

1 Comment

please read my post more carefully. I agree with you, but he did x = x+start. on my opinion code should be like: while start< end: if(start%3 == 0) or (x%%5 == 0 ): y = y+1 start = start +1
0

I am sure there are far more elegant solutions, but this seems to work.

def div_3_5(start, end):
count = 0
x = 0
while start < end:
    start += 1
    x += 1
    if (x%3 == 0) or (x%5 == 0):
        count += 1
return count

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.