Recursion wierdness? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Recursion wierdness?

Already having some trouble with a factorial recursion but noticed this bit- where I try to print something and the output is not what I expected? Is it something to do with printing within a recursion function and calling local variables? It looks sort of right but the numbers only make sense in each loop going down one line. https://code.sololearn.com/cEbYqsSIAkJr/?ref=app

28th Feb 2020, 1:02 PM
ren[paused]
ren[paused] - avatar
3 Answers
+ 1
Your recursion works correctly: 9! = 362880 But your code prints wrong values because you forgot to put parenthesis around num-1 in line#9 (multiplication has higher precendence than addtion). It sholud be: print(num,'x',num-1,"=",num * (num-1)) it will print the product of the current and the next numbers
28th Feb 2020, 1:32 PM
andriy kan
andriy kan - avatar
+ 1
What is your expected output? 9! => 362880 only.. Edit: Oh.. It should be num*(num-1), instead of num*num-1
28th Feb 2020, 1:20 PM
Jayakrishna 🇮🇳
0
Haha probably wasn't a good idea to work on it at 4am :) so it was operator precidence it looks like from typo
28th Feb 2020, 2:30 PM
ren[paused]
ren[paused] - avatar