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 »
Sandbox

Comments on Is it part of the mandelbrot set? [FINALIZED]

Post

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

The cost of flexibility (5 comments)
The cost of flexibility
trichoplax‭ wrote over 1 year ago

I never know how much flexibility to include in a challenge. I want the challenge to be flexible enough to allow as many people and languages as possible to enter. I also don't want too much flexibility as this puts a burden on the people solving the challenge.

For example, if there are 2 permitted ways of solving the challenge, people have to try both ways to see which is golfier. If there are n permitted ways of solving the challenge, people have to try n ways. If there are n binary choices in how to solve the challenge, people have to try 2n ways.

I never know where to draw the line, so I don't have any specific suggestion. I just thought I'd mention in case the rules could be simplified a little if not all of the flexibility is needed.

I already like this challenge as it is, so this is just a fine tuning comment...

trichoplax‭ wrote over 1 year ago

The other problem with having many permitted approaches is that it becomes more difficult to write test cases. I recommend adding test cases to make it easier for people to check their own and each other's answers, particularly if unexpected edge cases appear. It is still possible to write test cases for many permitted approaches, it's just more work for you.

Allowing non-terminating code makes testing difficult too. There's no rule against it, so it's another judgement call - you can have easy testing of answers or non-terminating code, so it's your choice.

H_H‭ wrote over 1 year ago

Thank you for the valuable input.

I agree to some extend. Yes, too much flexibility makes creating testcases hard. Especially in this case, since the mandelbrot is a fractal that can change in a hard to predict way with very tiny changes in the input number. This was the reason to not include testcases, since different rounding methods can lead to different results in edge cases. Maybe i will find some points that should work the same even after different rounding.

My goal was it to not restrict anything that isn't strictly necessary. Or to accept almost any solution that can be considered. While this makes it harder to find the shortest possible solution for a given language, it doesn't make it harder to find a short solution. I even believe it makes it easier to come up with a solution that is shorter than N bytes.

I may drop the non-halting option, since it is hard to test and it may amplifies rounding errors.

trichoplax‭ wrote over 1 year ago

Finding test cases that work under several different types of rounding sounds like a good compromise - I hadn't considered that. This might allow you to write just one set of test cases that works for all approaches, which sounds much easier to work with.

H_H‭ wrote over 1 year ago

Edit it: Remove the halting vs non-halting option. And i added test cases that should work with multiple different rounding methods and a different number of iterations.