Teaching Kids Programming: Videos on Data Structures and Algorithms
Given equation
def findSolution():
n = 0
while True:
if n * n + 19 * n - math.factorial(n) == 0:
return n
n += 1
# never reach here...
We can use math.factorial to compute the factorial – to avoid reinventing the wheel. We can also use Recursion to compute the factorial:
def f(n):
if n == 0:
return 1
return n * f(n-1)
Alternatively, we can iterate this:
def f(n):
ans = 1
for i in range(2, n + 1):
ans *= i
return ans
Solving Math Equation n*n+19*n-n!=0
Let’s move
And then
Because n is not zero – or zero is not the solution to the equation, we can safely divide both sides by n.
That becomes
Let’s use
Therefore,
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Teaching Kids Programming - Converting (Binary) Trees to Undirectional Graphs via DFS and BFS Algorithms
Next Post: Teaching Kids Programming - Silver Ratio and Pell Numbers (Metal Quadratic Equation)