A high school has a strange principal. On the first day, he has his students perform an odd opening day ceremony:

There are number of lockers "n" and number of students "n" in the school. The principal asks the first student to go to every locker and open it. Then he has the second student go to every second locker and close it. The third goes to every third locker and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth locker, and so on. After the process is completed with the "n"th student, how many lockers are open?

The task is to write a function which gets any number as an input and returns the number of open lockers after last sudent complets his activity. So input of the function is just one number which shows number of lockers and number of students. For example if there are 1000 lockers and 1000 students in school then input is 1000 and function returns number of open lockers after 1000th student completes his action.

The given input is always an integer greater than or equal to zero that is why there is no need for validation.

Difficulty: introductory

Starter code:

def num_of_open_lockers(n):
	

Solutions:
# First, I coded a straightforward solution. It would work, but because it had to loop over all
# students, and for every student over each locker, it had to do n^2 iterations. With numbers under
# 10k this proved no problem, however the test cases apparently tested bigger numbers, since my
# program kept getting timed out. I decided to record the number of open lockers for each n in range
# 1-500 and discovered a nice pattern: 0 -1-1-1 (3 1's) -2-2-2-2-2 (5 2's) -3-3-3-3-3-3-3 (7 4's)
# - et cetera. In other words, the number of consecutive numbers is (2n + 1). This was still not easy
# to do so I looked up the sequence in OEIS. There, I discovered that the sequence was the integer
# part of the square root of n. Now if that isn't easy to program... more of a maths problem, really.

def num_of_open_lockers(n):
    return int(n**0.5)

from math import floor, sqrt
def num_of_open_lockers(n):
  return floor(sqrt(n))

def num_of_open_lockers(n):
    return int(n ** 0.5)


"""
def num_of_open_lockers(n):
    doors = [0 for i in range(n)]
    for d in range(n):
        for i in range(d, n, d + 1):
            doors[i] = 0 if doors[i] else 1
    return sum(doors)
"""


#  ...  a-ha, OK, gotcha:


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



def num_of_open_lockers(n):
    return int(n ** 0.5)
    
#def num_of_open_lockers_not_opt(n):
#    nums = []
#    for i in xrange(0, n):
#        nums.append(False)
#
#    for i in xrange(1, n + 1):
#        for j in xrange(1, n + 1):
#            if j % i == 0:
#                nums[j - 1] = not nums[j - 1]
#
#    return nums.count(True)


from math import sqrt, floor

def num_of_open_lockers(n):
    return floor(sqrt(n))

def num_of_open_lockers(n):
    print("lockers:",n)

    i = 0
    count = 1
    open = 0
    even = True
    while i < n:
        if even:
            i += 1
            open += 1
            even = False
        else:
            even = True
            i += count*2
            count += 1
        
    #print "open:", open
    return open


from math import floor
def num_of_open_lockers(n): return floor(n ** .5)

import math

def num_of_open_lockers(n):
    return math.floor(math.sqrt(n))

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