0
\$\begingroup\$

Challenge

Given a real number as input, calculate the number of times which the natural logarithm function ln can be applied to it before the resulting number becomes imaginary.

Explanation

Applying ln to a negative number results in a non-real number. This means that, by repeatedly applying ln to a number, you will eventually get an imaginary number. For example, using 10 as input:

ln(10) = 2.303                - 1 time
ln(2.303) = 0.834             - 2 times
ln(0.834) = -0.181            - 3 times
ln(-0.181) = pi*i + ln(0.181) - stop; result is 3

Test cases

10 -> 3
1000000 -> 4
2.4 -> 2
-1 -> 0
0 -> 0

Details

  • This is code golf, so lowest byte answer wins.
  • The input number could be negative, so 0 should be returned.
  • Your solution may be either:
    • a function, which takes the input number as a parameter and returns the number of times ln can be applied;
    • or a program, which takes STDIN/command line argument input and prints the result.
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to PPCG! You can use the Sandbox to make sure your (future) challenges are good before posting. \$\endgroup\$ – user202729 Feb 4 '18 at 9:49
  • \$\begingroup\$ @user202729 Agreed, this is a dupe - I didn't realise that this process actually had a name. \$\endgroup\$ – Aaron Christiansen Feb 4 '18 at 9:53

Browse other questions tagged or ask your own question.