Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First : circular equation solution in Clear category for Counting Tiles by keromage
from math import ceil
"""
In the case of the first quadrant, consider whether the upper right
and lower left of the tile are included in the circle.
Circular equation was used. (x**2 + y**2 = r**2)
"""
def checkio(radius):
tile = ceil(radius)
inner_tile = []
border_tile = []
for i in range(1, tile + 1):
for j in range(1, tile + 1):
if i ** 2 + j ** 2 <= radius ** 2:
inner_tile.append((i, j))
elif (i - 1) ** 2 + (j - 1) ** 2 <= radius ** 2:
border_tile.append((i, j))
return [len(inner_tile) * 4, len(border_tile) * 4] # quadrant 1-4
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(2) == [4, 12], "N=2"
assert checkio(3) == [16, 20], "N=3"
assert checkio(2.1) == [4, 20], "N=2.1"
assert checkio(2.5) == [12, 20], "N=2.5"
Oct. 28, 2020
Comments: