Algorithms, Blockchain and Cloud

Teaching Kids Programming – Solving Math Equation n*n+19*n-n!=0 (Factorial Function and Unbounded Bruteforce Algorithm)


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given equation we know the n must be non-negative integers in order for factorial n! to make sense. If we bruteforce, we don’t know yet the upperbound, however we can ask computer to try until it can find one. If there is no solution, the algorithms will be non-stopping. This is unbounded bruteforce algorithm.

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 to other side:


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 to substitute so

is a line, and we can try first few numbers and we can see that when k is 4, while k is larger than 4, the grows a lot faster than and there is no cross between two functions.

Therefore, is the only solution to

–EOF (The Ultimate Computing & Technology Blog) —

796 words
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)

The Permanent URL is: Teaching Kids Programming – Solving Math Equation n*n+19*n-n!=0 (Factorial Function and Unbounded Bruteforce Algorithm) (AMP Version)

Exit mobile version