Statistics

Problem Statement for "Mandelbrot"

Problem Statement

A complex number is one of the form a+bi, where a and b are real numbers, and i represents the square root of -1. We call a the real part of the complex number, and b the imaginary part. When multiplying two complex numbers a+bi and c+di, the result is (a*c-b*d)+(a*d+b*c)i. When adding two complex numbers, a+bi and c+di, the result is (a+c)+(b+d)i. Finally, the magnitude of a complex number, a+bi, is sqrt(a*a+b*b).

The Mandelbrot set is a set of complex numbers. To determine whether or not a complex number, C = a+bi, is in the Mandelbrot set, we use the following recurrence:
Z0 = C
Zn+1 = Zn*Zn+C

If, for all n greater than or equal to 0, the magnitude of Zn is less than or equal to 2, then the complex number represented by C = a+bi is in the Mandelbrot set.

You will be given an int, max, representing the maximum value of n to check. For example, if max were 10, you would be required to check Zn for all 0 <= n <= 10. You will also be given two doubles, a and b, representing the real and imaginary parts of Z0, respectively. If the magnitude of Zn is always less than or equal to 2, for all 0 <= n <= max, you should return -1. Otherwise, you should return the smallest n such that the magnitude of Zn is greater than 2.

Definition

Class:
Mandelbrot
Method:
iterations
Parameters:
int, double, double
Returns:
int
Method signature:
int iterations(int max, double a, double b)
(be sure your method is public)

Constraints

  • max will be between 1 and 30, inclusive.
  • a will be between -2 and 2, inclusive.
  • b will be between -2 and 2, inclusive.
  • To prevent rounding errors, the magnitude of Zn will not be within 1e-3 of 2, for any n between 0 and n', inclusive, where n' is the smallest number (or max) such that the magnitude of Zn' is greater than 2.

Examples

  1. 5

    1

    1

    Returns: 1

    Here, Z0 = 1 + 1i Thus, Z1 = 1 - 1 + 1i + 1i + 1 + 1i = 1 + 3i. The magnitude of 1+3i is sqrt(1*1 + 3*3) = sqrt(10) > 2. Therefore, the return is 1.

  2. 2

    -1

    -1

    Returns: 2

    Z0 = -1 - 1i Z1 = -1 + 1i Z2 = -1 - 3i

  3. 30

    .25

    .25

    Returns: -1

    In this example, the magnitude of Zn is never more than 2.

  4. 30

    -1.9

    0

    Returns: -1

  5. 30

    .375

    .3

    Returns: 21

  6. 1

    2

    2

    Returns: 0

  7. 2

    -0.9757845334894477

    -0.5324823850973428

    Returns: -1

  8. 6

    -1.215710286583013

    -1.023275515748423

    Returns: 2

  9. 30

    -0.6032723480130606

    0.6801881709559305

    Returns: 12

  10. 9

    -0.1400492142091494

    0.9486376937662278

    Returns: -1

  11. 29

    -1.3881517891455584

    -0.02556210636440648

    Returns: 23

  12. 29

    0.07455255150701579

    0.6537762357563279

    Returns: -1

  13. 30

    -0.7486429223273539

    0.09778329030624722

    Returns: -1

  14. 30

    -0.1704792625621141

    1.0402943048285471

    Returns: 17

  15. 30

    0.807870298963598

    -0.6543660653618817

    Returns: 2

  16. 30

    0.48810360453387314

    -0.665488534645124

    Returns: 3

  17. 30

    -0.4385781787323486

    0.9158359631218591

    Returns: 4

  18. 30

    0.3876303092429134

    0.553235880211189

    Returns: 9

  19. 30

    -0.77194541564171

    -0.2104729508313854

    Returns: 13

  20. 30

    -0.6607090366845301

    0.43366731784830215

    Returns: 25

  21. 30

    0.36797794165192954

    0.5888317981912186

    Returns: 30

  22. 30

    0.9368843570133458

    0.2622863903243109

    Returns: 2

  23. 30

    0.020762888899839904

    -0.8665753621994896

    Returns: 10

  24. 30

    -0.5103992341377386

    -0.626336828815395

    Returns: 17

  25. 30

    -0.5433603111649807

    0.5278900520117933

    Returns: 23

  26. 30

    -0.18776392698249889

    0.8274060437584858

    Returns: 29

  27. 30

    0.27948801973818593

    -0.5899191256436842

    Returns: 30

  28. 30

    -0.6622628142150653

    -0.7829791656315668

    Returns: 3

  29. 30

    0.28406318697915256

    0.7072663999550006

    Returns: 5

  30. 30

    0.4229352491383338

    -0.12264605278203278

    Returns: 6

  31. 30

    0.42969095586857

    -0.15675007921442896

    Returns: 7

  32. 30

    0.3481849498123293

    -0.5625241775222809

    Returns: 21

  33. 30

    -0.5735481327981646

    0.6373126110310805

    Returns: 24

  34. 30

    -0.693206984256536

    0.46813084929404014

    Returns: 25

  35. 30

    -0.5512674608820742

    0.49656191346955136

    Returns: 28

  36. 30

    0.9734017363676726

    0.8845911530371948

    Returns: 1

  37. 30

    -0.5895060563118308

    0.47673774547673853

    Returns: 19

  38. 30

    -0.013764719206932785

    0.7469920439985955

    Returns: 28

  39. 30

    -0.8015334414990403

    -0.1834105013004188

    Returns: 29

  40. 30

    -1.9

    0.0

    Returns: -1

  41. 1

    2.0

    2.0

    Returns: 0

  42. 1

    1.5

    0.0

    Returns: 1

  43. 25

    0.35565

    -0.2545

    Returns: -1

  44. 28

    0.395

    0.28

    Returns: 20

  45. 28

    0.38

    0.33

    Returns: -1

  46. 30

    0.375

    0.3

    Returns: 21

  47. 21

    0.375

    0.3

    Returns: 21

  48. 1

    1.0

    1.0

    Returns: 1

  49. 10

    2.0

    2.0

    Returns: 0

  50. 10

    0.45

    0.0

    Returns: 5

  51. 2

    -1.0

    -1.0

    Returns: 2

  52. 1

    -1.0

    -1.0

    Returns: -1

  53. 29

    -1.3

    0.7

    Returns: 2

  54. 30

    1.1

    1.1

    Returns: 1

  55. 10

    0.5

    0.2

    Returns: 4

  56. 30

    -1.9

    0.0

    Returns: -1

  57. 1

    2.0

    2.0

    Returns: 0

  58. 1

    1.5

    0.0

    Returns: 1

  59. 25

    0.35565

    -0.2545

    Returns: -1

  60. 28

    0.395

    0.28

    Returns: 20

  61. 28

    0.38

    0.33

    Returns: -1

  62. 30

    0.375

    0.3

    Returns: 21

  63. 21

    0.375

    0.3

    Returns: 21

  64. 1

    1.0

    1.0

    Returns: 1

  65. 10

    2.0

    2.0

    Returns: 0

  66. 10

    0.45

    0.0

    Returns: 5

  67. 2

    -1.0

    -1.0

    Returns: 2

  68. 1

    -1.0

    -1.0

    Returns: -1

  69. 29

    -1.3

    0.7

    Returns: 2

  70. 30

    1.1

    1.1

    Returns: 1

  71. 10

    0.5

    0.2

    Returns: 4

  72. 30

    -1.9

    0.0

    Returns: -1

  73. 1

    2.0

    2.0

    Returns: 0

  74. 1

    1.5

    0.0

    Returns: 1

  75. 25

    0.35565

    -0.2545

    Returns: -1

  76. 28

    0.395

    0.28

    Returns: 20

  77. 28

    0.38

    0.33

    Returns: -1

  78. 30

    0.375

    0.3

    Returns: 21

  79. 21

    0.375

    0.3

    Returns: 21

  80. 1

    1.0

    1.0

    Returns: 1

  81. 10

    2.0

    2.0

    Returns: 0

  82. 10

    0.45

    0.0

    Returns: 5

  83. 2

    -1.0

    -1.0

    Returns: 2

  84. 1

    -1.0

    -1.0

    Returns: -1

  85. 29

    -1.3

    0.7

    Returns: 2

  86. 30

    1.1

    1.1

    Returns: 1

  87. 10

    0.5

    0.2

    Returns: 4


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: