Skip to content

Instantly share code, notes, and snippets.

@samdroid-apps
Last active August 29, 2015 13:56
Show Gist options
  • Save samdroid-apps/9298292 to your computer and use it in GitHub Desktop.
Save samdroid-apps/9298292 to your computer and use it in GitHub Desktop.
maths-fractal-area
import math
# This is run recusivly
# s is the length of the squares sides
# x is a counter
def f(s, x):
if x == 1: # if x = 1 then
# return returns the value and quits the function
return s * s # Just return the square if we are on the end
# otherwise
small_side = math.sqrt((s * s) / 2)
# _______
# small_side = ./ s x s
# -----
# 2
triangle_height = math.sqrt(
s * s / 4.0 + small_side * small_side)
# _____________________
# triangle_height = ./ s x s 2
# ----- + small_side
# 4
triangle_area = triangle_height * (s / 2.0)
# s
# triangle_area = triangle_height x ---
# 2
return (s * s) + triangle_area + (f(small_side, x-1) * 2)
# return s x s + triangle_area +
# then run again but with s = small_side and times the return by 2
# and keep repeating until the end of the branch
# end of function
start = float(input('Starting square: '))
times = int(input('How many times: '))
print 'The area is', f(start, times), 'units squared!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment