How many switches are ON

Python, 19 characters

f=lambda n:n**.5//1

Perfect squares are the only numbers with an odd number of divisors.

This used to be a 21-character answer:

f=lambda n:int(n**.5)

but since I originally wrote it in 2011, the floor-division // operator was introduced, which can replace the int call.

As a consequence, the answer is now returned as a floating-point number. (But f(10) == 3.0 is still a correct number of lockers!)


dc - 5 chars

[v]sf

Square root in dc is v. Using stdin/stdout takes 3 chars (?vp)


J, 9 chars

(Just for a simple example on J)

f=:[:<.%:

floor (<.) of square root (%:)

Tags:

Math

Code Golf