Prithvi Boinpally
Prithvi Boinpally

Reputation: 486

Incrementing while loop isn't working in some test cases

I have to define a function where:

Starting with a positive integer original, keep multiplying original by n and calculate the sum of all multiples generated including original until the sum is no longer smaller than total. Return the minimum number of multiplications needed to reach at value at or above the given total.

So for example:

  1. multiply_until_total_reached (1,5,2)

    1*2=2, (1+2)<5, 2*2=4, (1+2+4)>5, 2 multiplications needed

  2. multiply_until_total_reached (1,15,2)

    1*2=2, (1+2)<15, 2*2=4, (1+2+4)<15, 4*2=8, (1+2+4+8)=15, 3 multiplications

My current code works but the returned value is off by 1 in some cases. In a 1,1038,2 case, I get 9 multiplication needed instead of 10 but in the 1,15,2 case, I get the correct amount (3) multiplications.

Here's my code:

def multiply_until_total_reached(original, total, n):
    if total < original:
        return 0
    elif total > original:
        sumofdigits = 0 #declares var to keep track of sum of digits to compare to total
        timesofmult = 0 #track how many multiplication operations
        while sumofdigits <= total + 1:
            multnum = original * n
            sumofdigits = multnum + original
            original = multnum
            sumofdigits = sumofdigits + multnum
            timesofmult = timesofmult + 1
        return timesofmult

What's causing it to be off?

Upvotes: 1

Views: 91

Answers (3)

Guillaume
Guillaume

Reputation: 6009

Since solution for your code has already been posted, and you accept alternative solutions, allow me to suggest the following, which makes good use of Python's > 3.2 accumulate() function:

from itertools import accumulate, count

def multiply_until_total_reached(original, total, n):
    for i, result in enumerate(accumulate(original*n**c for c in count())):
        if result >= total: return i

assert multiply_until_total_reached(1,5,2) == 2
assert multiply_until_total_reached(1,15,2) == 3
assert multiply_until_total_reached(1,1038,2) == 10

Upvotes: 1

Abhishek J
Abhishek J

Reputation: 2584

Try this, lot smaller and neater. Explanation is in the comments..

def multiply_until_total_reached(original, total, n):
        sum = original    #Initialize sum to original
        mult_no = 0

        while sum < total:       #Will auto return 0 if original>=total
            sum += original*n    #Add original * n
            original = original*n   #Update the new original
            mult_no += 1    #Increase multiplications by 1

        return mult_no

print multiply_until_total_reached(1,5,2)
print multiply_until_total_reached(1,15,2)
print multiply_until_total_reached(1,1038,2)

#Output
#2
#3
#10

Upvotes: 2

EvilTak
EvilTak

Reputation: 7579

Your problem is that you are reassigning sumofdigits in every loop iteration. You just have to add multnum to sumofdigits in every iteration (sumofdigits += multnum). Also, your loop condition needs to be fixed to sumofdigits < total since you have to "Return the minimum number of multiplications needed to reach at value or above the given total."

Upvotes: 2

Related Questions