Skip to content

Instantly share code, notes, and snippets.

@paulclip
Created December 28, 2010 13:00
Show Gist options
  • Save paulclip/757187 to your computer and use it in GitHub Desktop.
Save paulclip/757187 to your computer and use it in GitHub Desktop.
Mandelbrot and Pi
#!/usr/bin/ruby
# Calculates Mandelbrot equation for point (-0.75, x) where x -> 0
# Amazingly the resulting iterations approximate pi
def mandel_iter (xp, yp, max)
x = y = 0.0
n = 0
while ((x*x + y*y) <= 4 and n < max) do
x , y , n = x*x - y*y + xp , 2*x*y + yp, n + 1
end
n
end
(0..7).each do |i|
t = Time.now
puts "#{10**(-i)}, #{mandel_iter(-0.75, 10**(-i), 10**10)}, #{Time.now - t}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment