Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Counting Tiles by l.szyman10
from math import hypot, sqrt, ceil
def checkio(radius):
"""count tiles"""
base = int(radius//sqrt(2))
whole, part = 0, 0
# check one quarter
for y in range(base+1, ceil(radius)+1):
for x in range(1, base+2):
if hypot(x,y) <= radius:
whole += 1
elif hypot(x-1, y-1) <= radius:
part += 1
else:
break
whole *= 8
part = part*8 - 4
whole += base**2*4 # adding tiles form square inscribed in circle
return [whole, part]
#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"
April 5, 2020
Comments: