简体   繁体   中英

If statement within while loop?

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

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 1 s:

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])

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

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)

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM