Bulb Switch

Bulb Switch_第1张图片
Bulb Switch_第2张图片


我觉得这题好扯啊。。。根本就测试不出面试者的水平。。。一个灯泡最后亮只有可能是因为开关被用了odd number times。 

第i 个灯泡开关会被处罚在第d轮只有当d divides i。 所以灯泡最后亮着只能是有一个odd number of divisors.

所以灯泡ends up on只有当i 是一个square。所以就是count the square numbers.

sqrt gives you the root of the largest square in the range [1,n]. And 1 is the smallest root. So you have the roots from 1 to sqrt, that's sqrt many roots. Which correspond to the sqrt many squares.

你可能感兴趣的:(Bulb Switch)