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

Comments on Is it part of the mandelbrot set?

Parent

Is it part of the mandelbrot set?

+2
−0

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$
  • Output is a boolean value for inside (after 16 iterations) or not inside the mandelbrot set.
  • 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.
  • At least up to 16 iterations have to be checked. If more is easier to do, do more.

Optional

  • 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.
  • You can alternatively "output" a value by either returning very fast or running for a very long time (the factor between both possibilities should be at least 16).

Shortest code wins.

Non-golfed Python Example

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 ) )

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, a higher number of iterations or different limits (other than $|z_{16}|>2$) 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.
Why should this post be closed?

0 comment threads

Post
+3
−0

Haskell, 66 bytes

-7 bytes thanks to Razetime‭

import Data.Complex
(\c->(<2).magnitude$(iterate(\z->z*z+c)c)!!16)

I use the fact, that if the result is bigger than 2, it will be forever then, so I don't need to check this relation in each iterations. It is enough to check it in the end.

iterate(\z->z*z+c)c gives us a list of the results, the original c is the first, then I take 16 elements from the list, so with the last function I get the last element which was iterated 15 times, that is $z_{16}$ (the first 0 times, the nth element n-1 times). Then I get the absolute value, and compare it with 2.

Testing:

import Data.Complex

f=(\c->(<2).magnitude $ last $ take 16$ iterate (\z -> z*z+c) c)
map f [(-1.862069) :+ (-0.3448276),0.3448276 :+ 0.3448276,0.2068966 :+ 0.7586207]

gives

[False,True,False]

Or if you like it better to print to the standard output:

main = do   print $ map f [(-1.8620690) :+ (-0.3448276),  0.3448276 :+ 0.3448276, 0.2068966 :+ 0.7586207]
main
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Can I use a 3rd party package? (3 comments)
using indexing to shorten the answer (1 comment)
Can I use a 3rd party package?
Arpad Horvath‭ wrote about 1 year ago

Can I get the iterate function from a 3rd party package? Then I can get a similar solution in Python.

Where can I find a detailed description of the rules? How to count imports, for example.

Razetime‭ wrote about 1 year ago

using 3rd party libs is perfectly fine. Wheat Wizard for example uses their own haskell golfing library, hgl. you just have to mention that you used them. People generally try to achieve the best solution with the same combo of language+library+flags/etc. it is quite flexible. You can find the consensus for most rules in the meta side of this site. If you have doubts, feel free to ask a question there and we will clarify them.

H_H‭ wrote about 1 year ago

Arpad Horvath‭d Yes, absolutely. But then it isn't a (vanilla) python solution anymore and should be mention in the title. For example you can say N Byte Python+itertoolz You just count the bytes in your source code as normal. When you have:

import itertoolz as z
z.DoSomeStuff()

Then you have to count the bytes in import itertoolz as z and z.DoSomeStuff() (37 when i counted correctly, assuming a single byte for line feed).

See: https://codegolf.codidact.com/posts/282802