[Leetcode] [JavaScript] Bulb switcher

Source: Internet
Author: User

Bulb switcher

There is n bulbs that is initially off. You first turn the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the n-th round, you are only toggle the last bulb. Find How many bulbs is on after N rounds.

Example:

N

So you should return 1, because there are only one bulb are on.

https://leetcode.com/problems/bulb-switcher/

The title means that the bulb is off at the beginning, starting from 2 the nth wheel switch n multiples of the bulb, open the original off the bulb, closed open.

A look is math problem, make the number group to simulate definitely tle, there are large numbers, need to find the law.

Like my math clumsy player, is dead, from 1 to 10 should be able to see the law, all the remaining bulbs are square number 1, 4, 9.

Look at the explanation of the great God:

Https://leetcode.com/discuss/75014/math-solution

My understanding is that 12 can be split into 1 and 12, 2 and 6, 3 and 4, they don't touch either of them, or they hit an odd number of times, and finally it's off.

And like 10, 11 this can only hit once, and finally is off.

If it is a square number such as 9, 3 of the time off, 9 will open again, so the end is open.

1 /* * 2 * @param {number} n 3 * @return {number} 4  */ 5 var function (n) {6     return  parseint (MATH.SQRT (n)); 7 };

[Leetcode] [JavaScript] Bulb switcher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.