Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post over 1 year ago by H_H‭.

74 / 255
Is it part of the mandelbrot set? [FINALIZED]
## Now [posted](https://codegolf.codidact.com/posts/289681)

---

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.

Suggested over 1 year ago by trichoplax‭