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.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Mathematics

Starter code: def num_of_open_lockers(n):
	

Solutions:
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))


num_of_open_lockers = lambda n: int(n ** 0.5)


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
	return open


from math import floor

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