0

What is the Time Complexity/Order of Growth for the function below?

def multiply(a, b):
    '''Takes two integers and computes their product.'''
    res = 0
    for i in range(1, b+1):
        res += a
    return res

I know the size of b makes it linear, however what about the size of a?

Thanks!

1
  • A is just a number, how would that alter the time complexity?
    – jonrsharpe
    Dec 5, 2018 at 20:55

1 Answer 1

0

Size of 'a' doesn't affect the time complexity of your algorithm. Since you are performing addition for b number of times, your complexity would be in the order of 'b'. Being O(b) is a mathematical property of the function and not the exact characterization of it. The exact running time might be 2045*b + 3542 where the constants 2045 and 3542(which are just stated as an example for here) will depend on the input and size of the input which refers to the size of variable 'a' in this problem

Hence the size of a affects the running time of your code and not the time complexity of the code.

1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.