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 Answer

change test and add table
Source Link
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 test cases

xln(x)builtin ln(x)steps until convergence
0.1-2.3025850929940455E0-2.30258509299404558
0.25-1.3862943611198906E0-1.38629436111989067
0.5-6.9314718055994520E-1-0.69314718055994537
0.75-2.8768207245178090E-1-0.28768207245178097
0.9-1.0536051565782634E-1-0.105360515657826287
11.1102230246251565E-1608
1.32.6236426446749117E-10.262364264467491067
26.9314718055994530E-10.69314718055994537
2.7182818284590459.9999999999999990E-118
3.1415926535897931.1447298858494002E01.14472988584940028
41.3862943611198906E01.38629436111989069
51.6094379124341003E01.60943791243410039
71.9459101490553135E01.945910149055313211
102.3025850929940460E02.30258509299404614
533.9702919135521220E03.97029191355212255
54.598150033144234457
994.5951198501345900E04.59511985013459100

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.

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.

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

xln(x)builtin ln(x)steps until convergence
0.1-2.3025850929940455E0-2.30258509299404558
0.25-1.3862943611198906E0-1.38629436111989067
0.5-6.9314718055994520E-1-0.69314718055994537
0.75-2.8768207245178090E-1-0.28768207245178097
0.9-1.0536051565782634E-1-0.105360515657826287
11.1102230246251565E-1608
1.32.6236426446749117E-10.262364264467491067
26.9314718055994530E-10.69314718055994537
2.7182818284590459.9999999999999990E-118
3.1415926535897931.1447298858494002E01.14472988584940028
41.3862943611198906E01.38629436111989069
51.6094379124341003E01.60943791243410039
71.9459101490553135E01.945910149055313211
102.3025850929940460E02.30258509299404614
533.9702919135521220E03.97029191355212255
54.598150033144234457
994.5951198501345900E04.59511985013459100

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.

clarify
Source Link
RubenVerg
  • 1.3k
  • 6
  • 20

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

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

(⊢-1-÷∘*)⍣=∘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-÷∘*)⍣=∘1­⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁤​‎⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­⍣=⍨
 ∘1  ⍝ ‎⁡x_0 is 1L
 ⍣= ⍝ ‎⁢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 the same length as1 byte less than an implementation of Jos Woolley's, 1e×ばつ ̄1+*∘1e ̄9, but probably still has some room to be golfed further.

Dyalog APL (no builtin), 13 bytes

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

(⊢-1-÷∘*)⍣=∘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-÷∘*)⍣=∘1­⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁤​‎⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
 ∘1  ⍝ ‎⁡x_0 is 1
 ⍣= ⍝ ‎⁢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 the same length as an implementation of Jos Woolley's, 1e×ばつ ̄1+*∘1e ̄9, but probably still has some room to be golfed further.

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.

added 148 characters in body
Source Link
RubenVerg
  • 1.3k
  • 6
  • 20

Dyalog APL (no builtin), 13 bytes

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

(⊢-1-÷∘*)⍣=∘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-÷∘*)⍣=∘1­⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁤​‎⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
 ∘1 ⍝ ‎⁡x_0 is 1
 ⍣= ⍝ ‎⁢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 the same length as an implementation of Jos Woolley's, 1e×ばつ ̄1+*∘1e ̄9, but probably still has some room to be golfed further.

Dyalog APL (no builtin)

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

(⊢-1-÷∘*)⍣=∘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-÷∘*)⍣=∘1­⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁤​‎⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
 ∘1 ⍝ ‎⁡x_0 is 1
 ⍣= ⍝ ‎⁢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

Dyalog APL (no builtin), 13 bytes

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

(⊢-1-÷∘*)⍣=∘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-÷∘*)⍣=∘1­⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁤​‎⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌­
 ∘1 ⍝ ‎⁡x_0 is 1
 ⍣= ⍝ ‎⁢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 the same length as an implementation of Jos Woolley's, 1e×ばつ ̄1+*∘1e ̄9, but probably still has some room to be golfed further.

Source Link
RubenVerg
  • 1.3k
  • 6
  • 20
Loading

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