Fundamentals 5 min read

Why Only Square-Number Bulbs Stay On: A Simple Ruby Demo

A third‑grader’s bulb‑toggling puzzle is solved by showing that a bulb stays on only when its number is a perfect square, leading to 14 lit bulbs out of 200, and a simple Ruby program is used to demonstrate the result.

Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Huawei Cloud Developer Alliance
Why Only Square-Number Bulbs Stay On: A Simple Ruby Demo

My son, a third‑grade student, asked for help with an Olympiad‑style problem about 200 light bulbs numbered 1…200, each controlled by its own switch. All bulbs start off. First we toggle every bulb whose number is divisible by 1, then those divisible by 2, and so on up to 200. How many bulbs remain on?

Observing a few bulbs shows that a bulb ends up on if its switch is pressed an odd number of times. A bulb numbered N is toggled once for each divisor of N, so the question becomes: for which N does the divisor count become odd?

If N is not a perfect square, its divisors can be paired (d, N/d) with d < √N, giving an even count. If N is a perfect square, the square root pairs with itself, adding one unpaired divisor, resulting in an odd count. Therefore exactly the bulbs whose numbers are perfect squares stay on.

There are 14 perfect squares ≤ 200 (1², 2², … 14²), so 14 bulbs remain lit.

To illustrate the reasoning to my son I wrote a short Ruby script that simulates the toggling process. Running the script prints the numbers of the bulbs that stay on, confirming the mathematical result.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmEducationRubyNumber theoryMath Puzzle
Huawei Cloud Developer Alliance
Written by

Huawei Cloud Developer Alliance

The Huawei Cloud Developer Alliance creates a tech sharing platform for developers and partners, gathering Huawei Cloud product knowledge, event updates, expert talks, and more. Together we continuously innovate to build the cloud foundation of an intelligent world.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.