classSolution{public:intbulbSwitch(intn){// The k-th bulb can only be switched when k % i == 0.// So, we can rephrase the problem:// To find number of numbers <= n that have odd factors.// Obviously, only square numbers have odd factor(s).// e.g. n = 10, only 1, 4, and 9 are square numbers that <= 10returnsqrt(n);}};
1 2 3 4 5 6 7 8 910
classSolution{publicintbulbSwitch(intn){// The k-th bulb can only be switched when k % i == 0.// So, we can rephrase the problem:// To find number of numbers <= n that have odd factors.// Obviously, only square numbers have odd factor(s).// e.g. n = 10, only 1, 4, and 9 are square numbers that <= 10return(int)Math.sqrt(n);}}
12345678
classSolution:defbulbSwitch(self,n:int)->int:# The k-th bulb can only be switched when k % i == 0.# So, we can rephrase the problem:# To find number of numbers <= n that have odd factors.# Obviously, only square numbers have odd factor(s).# e.g. n = 10, only 1, 4, and 9 are square numbers that <= 10returnmath.isqrt(n)