Determining more streamlined code

I’m practicing functions and I’m working on using recursion to find the sum of a sequence of numbers.

I’ve got two codes:
The first one I wrote and the second is the answer on the website I’m using to practice.
Which one is faster (or is this negligible as they both use recursion)? I was the second one is faster because of extra computing power of the input() function.

``````def recurs(num):
if num == 0:
return num
else: return num + recurs(num-1)

num = int(input("Enter number  "))
print(recurs(num))

if num:
return num + addition(num - 1)
else:
return 0

print(res)
``````

Neither is faster in a time complexity sense, they both do exactly the same thing.

You wouldn’t really compare input() versus a hard coded number. Does the program require user input or not?

In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm.
https://en.wikipedia.org/wiki/Time_complexity

You need to understand the idea of time complexity, not all the math outlined here but the basic idea that functions times are compared by degrees.

This might be a good primer: https://www.freecodecamp.org/news/big-o-cheat-sheet-time-complexity-chart/

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.