Code golf: the Mandelbrot set

Posted by Stefano Borini on Stack Overflow See other posts from Stack Overflow or by Stefano Borini
Published on 2010-04-14T16:45:25Z Indexed on 2010/04/14 17:23 UTC
Read the original article Hit count: 396

Usual rules for the code golf. Here is an implementation in python as an example

from PIL import Image

im = Image.new("RGB", (300,300))
for i in xrange(300):
    print "i = ",i
    for j in xrange(300):
        x0 = float( 4.0*float(i-150)/300.0 -1.0)
        y0 = float( 4.0*float(j-150)/300.0 +0.0)
        x=0.0
        y=0.0
        iteration = 0
        max_iteration = 1000
        while (x*x + y*y <= 4.0 and iteration < max_iteration):
            xtemp = x*x - y*y + x0
            y = 2.0*x*y+y0
            x = xtemp
            iteration += 1
        if iteration == max_iteration:
            value = 255 
        else:
            value = iteration*10 % 255
        print value 
        im.putpixel( (i,j), (value, value, value))

im.save("image.png", "PNG")

The result should look like this

Mandelbrot set

Use of an image library is allowed. Alternatively, you can use ASCII art. This code does the same

for i in xrange(40):
    line = []
    for j in xrange(80):
        x0 = float( 4.0*float(i-20)/40.0 -1.0)
        y0 = float( 4.0*float(j-40)/80.0 +0.0)
        x=0.0
        y=0.0
        iteration = 0
        max_iteration = 1000
        while (x*x + y*y <= 4.0 and iteration < max_iteration):
            xtemp = x*x - y*y + x0
            y = 2.0*x*y+y0
            x = xtemp
            iteration += 1
        if iteration == max_iteration:
            line.append(" ")
        else:
            line.append("*")
    print "".join(line)

The result

********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
***************************************   **************************************
*************************************       ************************************
************************************         ***********************************
***********************************           **********************************
************************************         ***********************************
*************************************       ************************************
***********************************           **********************************
********************************                 *******************************
****************************                         ***************************
*****************************                       ****************************
****************************                         ***************************
************************   *                         *   ***********************
***********************    *                         *    **********************
******************** *******                         ******* *******************
****************************                         ***************************
******************************                     *****************************
*****************************  *        *        *  ****************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************

© Stack Overflow or respective owner

Related posts about rosetta-stone

Related posts about code-golf

  • Code Golf: Collatz Conjecture

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Inspired by http://xkcd.com/710/ here is a code golf for it. The Challenge Given a positive integer greater than 0, print out the hailstone sequence for that number. The Hailstone Sequence See Wikipedia for more detail.. If the number is even, divide it by two. If the number is odd, triple… >>> More

  • Code Golf - p day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character, followed by an approximation of p. Input is a single number, R. Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf - PI day

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The Challenge The shortest code by character count to display a representation of a circle of radius R using the *character. Followed by an approximation of pi Input is a single number, R Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. The approximation… >>> More

  • Code Golf: Triforce

    as seen on Stack Overflow - Search for 'Stack Overflow'
    This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383 The Problem Assume the user gives you a numeric input ranging from 1 to 7. Input should be taken from the console, arguments are less desirable. When the input is 1, print the following: *********** *********… >>> More

  • Code Golf: Tic Tac Toe

    as seen on Stack Overflow - Search for 'Stack Overflow'
    Post your shortest code, by character count, to check if a player has won, and if so, which. Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where: 0 = nothing set 1 = player 1 (X) 2 = player 2 (O) So, given the array b… >>> More