### Communities

tag:snake search within a tag
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
created:<1w created < 1 week ago
post_type:xxxx type of post
Sandbox

# Is it part of the mandelbrot set? [FINALIZED]

+1
−0

## Now posted

Input is a number, you have to decide if it is part of the mandelbrot set or not, after at least 16 iterations.

This is done by applying this formula: $z_n = z_{n-1}^2 + c$ repeatedly. $c$ is the input number and $z_0 = 0$. Therefore:

• $z_1 = c$
• $z_2 = c^2 + c$
• $z_3 = ( c^2 + c )^2 +c$
• $z_4 = ( ( c^2 + c )^2 +c )^2 +c$
• ...

If $|z_{16}|>2$, then the input $c$ is not part of the mandelbrot set. If $|z_{16}| ≤ 2$, then we consider it part of the mandelbrot set for this challenge.

## Rules

• The input is a complex number $|c|≤2$
• At least up to 16 iterations have to be checked. If more is easier to do, do more.
• If it is easier, you can check for the real part $Re(z_{n})$ to be $Re(z_{n})>2$ or $|Re(z_{n})|>2$ instead of $|z_{n}|>2$. But then it should be checked for each iteration. This includes some numbers that are not part of the mandelbrot set but we ignore that for this challenge.
• If it is easier, the limit can be anywhere between 2-6 and doesn't have to be 2, for example you could check for $|z_{16}|>4$ instead of $|z_{16}|>2$.
• If it is easier, an additional input for the number of iterations can be given, but then at least 3-64 iterations (chosen by the input) have to be supported.
• If it is easier, an additional input for $z_0$ or $z_1$ can be given.
• The input format can be chosen how it is best for you, but input has to be representable with an error of $|E|≤ { 1 \over 1024}$ on each axis i.e. you need at least 11 bit for each axis.
• Output one value for inputs in the mandelbrot set (after 16 iterations) and a different value for inputs outside the mandelbrot set. If it is easier you can return the number of iterations till $|z_{n}|>2$ is reached.

Shortest code wins.

## Non-golfed Examples

Python example using native complex number

def isPartOfMandelbrot(c):
z=c
for i in range(16):
z=z**2+c
if abs(z)>2:
return 0
return 1

print( isPartOfMandelbrot( float(input()) + float(input())*1j ) )



C example using fractions:

#include <stdio.h>
#include <stdlib.h>

/*
Return 0 when the input is not in the mandelbrot set
Return 1 when the input is in the mandelbrot set

cr: real part of the input, as numerator of a fraction
ci: imaginary part of the input, as numerator of a fraction
denominator: the denominator of all fractions
iterations: Up to how many iterations we check.
*/
int insideMandelbrot(int cr, int ci, int denominator, int iterations)
{
int zr=cr;
int zi=ci;
while( zr<2*denominator )
{
int t = zr*zr/denominator - zi*zi/denominator;
zi = zr*zi/denominator*2 + ci;
zr = t + cr;
if( !iterations-- )
{ return 1; }
}
return 0;
}

int main(int argc, char **argv)
{
if( argc<3 )
{ return 0; }

//We use 2 fraction to store the complex number
//One fraction for the real one fraction for the imaginary part
//The fraction have a fixed denominator of 512.
//Both are expected to be between -1024 (for -2) and 1024 (for 2)
//cr is the real part. ci the imaginary part
int cr = atoi( argv[1] );
int ci = atoi( argv[2] );

puts( insideMandelbrot(cr,ci,512,16) ? "Inside" : "Outside" );
}



## Testcases

-1.8620690 -0.3448276i: 0
-1.7241379 -0.0689655i: 0
-1.7241379 +0.0689655i: 0
-1.5862069 -0.0689655i: 0
-1.5862069 +0.0689655i: 0
-1.5862069 +1.0344828i: 0
-1.5862069 +1.1724138i: 0
-1.4482759 -1.1724138i: 0
-1.4482759 -0.0689655i: 0
-1.4482759 +0.0689655i: 0
-1.3103448 -0.8965517i: 0
-1.3103448 -0.3448276i: 0
-1.3103448 -0.2068966i: 0
-1.3103448 +0.2068966i: 0
-1.3103448 +0.3448276i: 0
-1.1724138 -1.0344828i: 0
-1.1724138 -0.0689655i: 1
-1.1724138 +0.0689655i: 1
-1.0344828 -0.2068966i: 1
-1.0344828 -0.0689655i: 1
-1.0344828 +0.0689655i: 1
-1.0344828 +0.2068966i: 1
-0.8965517 -1.1724138i: 0
-0.8965517 -0.4827586i: 0
-0.8965517 -0.3448276i: 0
-0.8965517 -0.2068966i: 1
-0.8965517 -0.0689655i: 1
-0.8965517 +0.0689655i: 1
-0.8965517 +0.2068966i: 1
-0.8965517 +0.3448276i: 0
-0.7586207 -0.6206897i: 0
-0.7586207 -0.4827586i: 0
-0.7586207 -0.3448276i: 0
-0.7586207 +0.3448276i: 0
-0.7586207 +0.4827586i: 0
-0.6206897 -0.6206897i: 0
-0.6206897 -0.4827586i: 0
-0.6206897 -0.3448276i: 1
-0.6206897 -0.2068966i: 1
-0.6206897 -0.0689655i: 1
-0.6206897 +0.0689655i: 1
-0.6206897 +0.2068966i: 1
-0.6206897 +0.3448276i: 1
-0.6206897 +0.4827586i: 0
-0.6206897 +0.6206897i: 0
-0.6206897 +0.8965517i: 0
-0.6206897 +1.3103448i: 0
-0.6206897 +1.8620690i: 0
-0.4827586 -1.7241379i: 0
-0.4827586 -0.4827586i: 1
-0.4827586 -0.3448276i: 1
-0.4827586 -0.2068966i: 1
-0.4827586 -0.0689655i: 1
-0.4827586 +0.0689655i: 1
-0.4827586 +0.2068966i: 1
-0.4827586 +0.3448276i: 1
-0.4827586 +0.4827586i: 1
-0.4827586 +1.5862069i: 0
-0.3448276 -1.3103448i: 0
-0.3448276 -0.7586207i: 0
-0.3448276 -0.4827586i: 1
-0.3448276 -0.3448276i: 1
-0.3448276 -0.2068966i: 1
-0.3448276 -0.0689655i: 1
-0.3448276 +0.0689655i: 1
-0.3448276 +0.2068966i: 1
-0.3448276 +0.3448276i: 1
-0.3448276 +0.4827586i: 1
-0.3448276 +0.7586207i: 0
-0.3448276 +1.3103448i: 0
-0.3448276 +1.8620690i: 0
-0.2068966 -1.0344828i: 0
-0.2068966 -0.8965517i: 0
-0.2068966 -0.7586207i: 1
-0.2068966 -0.6206897i: 1
-0.2068966 -0.4827586i: 1
-0.2068966 -0.3448276i: 1
-0.2068966 -0.2068966i: 1
-0.2068966 -0.0689655i: 1
-0.2068966 +0.0689655i: 1
-0.2068966 +0.2068966i: 1
-0.2068966 +0.3448276i: 1
-0.2068966 +0.4827586i: 1
-0.2068966 +0.6206897i: 1
-0.2068966 +0.7586207i: 1
-0.2068966 +0.8965517i: 0
-0.2068966 +1.0344828i: 0
-0.0689655 -1.0344828i: 0
-0.0689655 -0.7586207i: 1
-0.0689655 -0.6206897i: 1
-0.0689655 -0.4827586i: 1
-0.0689655 -0.3448276i: 1
-0.0689655 -0.2068966i: 1
-0.0689655 -0.0689655i: 1
-0.0689655 +0.0689655i: 1
-0.0689655 +0.2068966i: 1
-0.0689655 +0.3448276i: 1
-0.0689655 +0.4827586i: 1
-0.0689655 +0.6206897i: 1
-0.0689655 +0.7586207i: 1
-0.0689655 +1.0344828i: 0
-0.0689655 +1.1724138i: 0
-0.0689655 +1.3103448i: 0
+0.0689655 -0.7586207i: 0
+0.0689655 -0.4827586i: 1
+0.0689655 -0.3448276i: 1
+0.0689655 -0.2068966i: 1
+0.0689655 -0.0689655i: 1
+0.0689655 +0.0689655i: 1
+0.0689655 +0.2068966i: 1
+0.0689655 +0.3448276i: 1
+0.0689655 +0.4827586i: 1
+0.0689655 +0.7586207i: 0
+0.2068966 -1.8620690i: 0
+0.2068966 -0.6206897i: 0
+0.2068966 -0.4827586i: 1
+0.2068966 -0.3448276i: 1
+0.2068966 -0.2068966i: 1
+0.2068966 -0.0689655i: 1
+0.2068966 +0.0689655i: 1
+0.2068966 +0.2068966i: 1
+0.2068966 +0.3448276i: 1
+0.2068966 +0.4827586i: 1
+0.2068966 +0.6206897i: 0
+0.2068966 +0.7586207i: 0
+0.2068966 +1.0344828i: 0
+0.2068966 +1.4482759i: 0
+0.2068966 +1.5862069i: 0
+0.3448276 -0.4827586i: 0
+0.3448276 -0.3448276i: 1
+0.3448276 -0.2068966i: 1
+0.3448276 +0.2068966i: 1
+0.3448276 +0.3448276i: 1
+0.3448276 +0.4827586i: 0
+0.3448276 +1.7241379i: 0
+0.4827586 -0.3448276i: 0
+0.4827586 -0.2068966i: 0
+0.4827586 +0.0689655i: 0
+0.4827586 +0.2068966i: 0
+0.4827586 +0.3448276i: 0
+0.4827586 +1.7241379i: 0
+0.6206897 +0.4827586i: 0
+0.6206897 +1.7241379i: 0
+0.7586207 +0.4827586i: 0
+0.7586207 +1.4482759i: 0
+0.7586207 +1.7241379i: 0
+0.8965517 -1.7241379i: 0
+0.8965517 -0.4827586i: 0
+0.8965517 -0.2068966i: 0
+1.0344828 -1.5862069i: 0
+1.3103448 -1.0344828i: 0
+1.3103448 +1.1724138i: 0
+1.4482759 -0.4827586i: 0
+1.4482759 +0.3448276i: 0
+1.5862069 -0.6206897i: 0
+1.5862069 +0.2068966i: 0
+1.7241379 -0.4827586i: 0
+1.8620690 +0.2068966i: 0


I tried to eliminate testcases that are on a edge where different rounding methods yield different results. But it may still have some that are on a edge.

Why does this post require moderator attention?