Teaching Kids Programming – The Birthday Candles Problem (Three Algorithms)


Teaching Kids Programming: Videos on Data Structures and Algorithms

The Birthday Candles Problem

If a person blows 1 candle at Age 1, 2 candles at Age 2, 3 candles at Age 3, then what is the year of the birthday if he/she has blown total 231 candles.

This can be solved using three algorithms/approaches.

The Bruteforce Algorithm

We can try brute forcing (perform a linear search) the year and compute the sum, until it reaches T (total) or exceed it.

def f(T):
    year = 0
    total = 0
    while total < T:
        year += 1
        total += year
    return year if total == T else -1

To understand the number of iterations, notice that the total is essentially the sum of the first n natural numbers, where n is the number of years. This sum is known to be tex_add1068f2d46b5ed5f83332b269a4599 Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms). The loop stops when this sum is greater than or equal to T. So, we’re looking for the smallest n such that:

tex_f265c91300d69a189eabe23ede8cc92a Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms)

Solving this quadratic inequality gives us the smallest n that satisfies the condition. Since this is essentially forming a series, the number of iterations is proportional to the square root of T because you’re solving for n in a quadratic equation. This means the time complexity of the loop is O(√T).

The Binary Search Algorithm

We can binary search the range [1, T].

def f(T):
    L = 1
    R = T
    while L <= R:
        M = L + R >> 1
        s = (M + 1) * M >> 1
        if s == T:
            return M
        if s < T:
            L = M + 1
        else:
            R = M - 1
    return -1

The key to understanding the time complexity lies in the while loop and how quickly the range between L and R is reduced.

  • Each iteration of the while loop approximately halves the search range.
  • The initial range of values is from 1 to T.
  • The number of times you can halve T until you get down to 1 (the smallest possible range) is tex_abc89e5722046cc16219d478ad34398b Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms)

So, the while loop, which dominates the time complexity, runs in O(log T) time. Thus, the overall time complexity of the function is O(log T).

The Quadratic Equation Solver

Given the Year n, and the total T, we have a quadratic equation for the first n accumulated sum:

tex_e69ce53584fe4daad861cbbc526760e2 Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms)
tex_50f20b07dd807b6a588f204cdc1bd2e0 Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms)

We know there are two roots: tex_81731ef66cf02126e708794b04ebc8d8 Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms) and tex_e1c063a3888e1fd8cc76735d746fc696 Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms)
When tex_a9a48852df1196d99027403928c710f3 Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms) we have real roots. And since the root is positive, we only consider the tex_296e3a60f370922f60d43292b4a9ed8f Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms).

We compute the value of tex_296e3a60f370922f60d43292b4a9ed8f Teaching Kids Programming - The Birthday Candles Problem (Three Algorithms) and check if it is a whole integer. This can be done via pythons’ is_integer method. The time/complexity is O(1) considering that the square root is amortized O(1) constant.

def f(T):
    a = 1
    b = 1
    c = -2 * T
    x = (-b + (b*b-4*a*c)**0.5)/(2*a)
    if x.is_integer():
        return int(x)
    return -1

See the variation of this problem: Teaching Kids Programming – Another Birthday Candles Problem

–EOF (The Ultimate Computing & Technology Blog) —

992 words
Last Post: Adsense/Blog Traffic Declines after ChatGPT is Widely Adopted
Next Post: How to Check Balances of a Bitcoin (BTC) Wallet Address via NodeJs or Python?

The Permanent URL is: Teaching Kids Programming – The Birthday Candles Problem (Three Algorithms) (AMP Version)

Leave a Reply