Skip to main content
Leetcode_319: Light Bulb Switch

Title

Link: Bulb Switcher

There are initially n bulbs turned off.
In the first round, you turn on all the bulbs. In the second round, you toggle every second bulb. In the third round, you toggle every third bulb (if the bulb is on, turn it off; if it is off, turn it on).
For the i-th round, you toggle every i-th bulb. For the n-th round, you only toggle the last bulb.
Find how many bulbs are on after n rounds.


init-qyAbout 2 minarithmeticleetcodearithmetic
0x5f375a86 Magic Number

By chance, I came across this magical hexadecimal number 0x5f3759df and decided to make a note of it.

Science changes the world, and mathematics changes science.

Last time, I encountered an irrational number, 0.75, which appeared in the source code of Java HashMap and represents the default load factor. Let me briefly explain the meaning of this value: when the number of elements in the HashMap reaches 0.75 times its capacity (i.e., 12 elements when the capacity is 16), the HashMap will be resized to reduce hash collisions and avoid the degradation of HashMap into a linked list, which significantly increases the query time. As for why this value is 0.75, there is a simple explanation: it is a compromise between improving space utilization and reducing query costs, mainly based on the Poisson distribution, and 0.75 minimizes collisions.


init-qyAbout 3 minarithmeticby-talkarithmetic