Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

3 of 4
clarify
RubenVerg
  • 1.3k
  • 6
  • 20

Dyalog APL (no builtin), (削除) 13 (削除ここまで) 12 bytes

Dyalog does have the builtin , but this way it's more fun :)

(⊢-1-÷∘*)⍣=⍨

Basically just Newton's method:

$$ x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} $$

where (L is the number we are taking the log of)

$$ f(x) = e^x - L \\ f'(x) = e^x \\ x_{n+1} = x_n - \frac{e^{x_n} - L}{e^{x_n}} = x_n - 1 - \frac L {e^{x_n}} $$

(⊢-1-÷∘*)⍣=⍨
 ⍨ ⍝ ‎⁡x_0 is L
 ⍣= ⍝ ‎⁢repeat Newton's method until x_n and x_n+1 are "equal":
 ⊢-1- ⍝ ‎⁣x_n - 1 -
 ÷∘* ⍝ ‎⁤L / e^x_n
💎 Created with the help of Luminespire at https://vyxal.github.io/Luminespire

"Equal" here means

$$ \left|x_n-x_{n+1}\right|\leq10^{-14}\max\left(\left|x_n\right|, \left|x_{n+1}\right|\right) $$

test cases

gotta say, ln(1) resulting in 1e-16 is kinda annoying but well within the spec

This is 1 byte less than an implementation of Jos Woolley's, 1e×ばつ ̄1+*∘1e ̄9, but probably still has some room to be golfed further.

RubenVerg
  • 1.3k
  • 6
  • 20

AltStyle によって変換されたページ (->オリジナル) /