Skip to main content
added 380 characters in body
Source Link
Bubbler
  • 79.5k
  • 5
  • 164
  • 490

Factor + math.polynomials math.factorials, 66 bytes

[| x | 1 x [ sq neg x [0,b) [ 2 * n! recip ] map polyval ] times ]

Try it online!

Factor has rational numbers, but no arbitrary-precision decimals. This answer tries to exploit it as much as possible: iterate the evaluation of the fist n terms of Taylor series (more precisely, Maclaurin series) n times, with the starting value of 1. The output is given as a rational number; the floating-point representation is also shown on TIO to check the value in human-readable form.

The function is very slow to calculate and very slow to converge, but in theory it must converge to the Dottie number as n increases, since the iterated function approaches the cosine function, the iteration count increases to infinity, and the whole computation is done in rationals (and therefore exact).

How it works

Given a positive integer \$n\$, the code approximates the Dottie number as follows:

$$ \cos{x} = \sum_{i=0}^{\infty}{\frac{(-1)^i}{(2i)!}x^{2i}} \approx \sum_{i=0}^{n-1}{\frac{(-x^2)^i}{(2i)!}} \\ \text{Dottie number } = \cos{x} \text{ iterated } \infty \text{ times on } 1 \\ \approx \sum_{i=0}^{n-1}{\frac{(-x^2)^i}{(2i)!}} \text{ iterated } n \text{ times on } 1 $$

[| x |  ! an anonymous function that takes one arg `x` as a local variable
  1 x [ ... ] times  ! repeat the inner function x times on the value 1...
    sq neg           !   val -> -val^2
    x [0,b)          !   0..x-1
    [ 2 * n! recip ] map  ! convert each number `i` to 1/(2i)!
    polyval          !   evaluate the array as polynomial at the value of -val^2
]

Factor + math.polynomials math.factorials, 66 bytes

[| x | 1 x [ sq neg x [0,b) [ 2 * n! recip ] map polyval ] times ]

Try it online!

Factor has rational numbers, but no arbitrary-precision decimals. This answer tries to exploit it as much as possible: iterate the evaluation of the fist n terms of Taylor series (more precisely, Maclaurin series) n times, with the starting value of 1. The output is given as a rational number; the floating-point representation is also shown on TIO to check the value in human-readable form.

The function is very slow to calculate and very slow to converge, but in theory it must converge to the Dottie number as n increases, since the iterated function approaches the cosine function, the iteration count increases to infinity, and the whole computation is done in rationals.

How it works

[| x |  ! an anonymous function that takes one arg `x` as a local variable
  1 x [ ... ] times  ! repeat the inner function x times on the value 1...
    sq neg           !   val -> -val^2
    x [0,b)          !   0..x-1
    [ 2 * n! recip ] map  ! convert each number `i` to 1/(2i)!
    polyval          !   evaluate the array as polynomial at the value of -val^2
]

Factor + math.polynomials math.factorials, 66 bytes

[| x | 1 x [ sq neg x [0,b) [ 2 * n! recip ] map polyval ] times ]

Try it online!

Factor has rational numbers, but no arbitrary-precision decimals. This answer tries to exploit it as much as possible: iterate the evaluation of the fist n terms of Taylor series (more precisely, Maclaurin series) n times, with the starting value of 1. The output is given as a rational number; the floating-point representation is also shown on TIO to check the value in human-readable form.

The function is very slow to calculate and very slow to converge, but in theory it must converge to the Dottie number as n increases, since the iterated function approaches the cosine function, the iteration count increases to infinity, and the whole computation is done in rationals (and therefore exact).

How it works

Given a positive integer \$n\$, the code approximates the Dottie number as follows:

$$ \cos{x} = \sum_{i=0}^{\infty}{\frac{(-1)^i}{(2i)!}x^{2i}} \approx \sum_{i=0}^{n-1}{\frac{(-x^2)^i}{(2i)!}} \\ \text{Dottie number } = \cos{x} \text{ iterated } \infty \text{ times on } 1 \\ \approx \sum_{i=0}^{n-1}{\frac{(-x^2)^i}{(2i)!}} \text{ iterated } n \text{ times on } 1 $$

[| x |  ! an anonymous function that takes one arg `x` as a local variable
  1 x [ ... ] times  ! repeat the inner function x times on the value 1...
    sq neg           !   val -> -val^2
    x [0,b)          !   0..x-1
    [ 2 * n! recip ] map  ! convert each number `i` to 1/(2i)!
    polyval          !   evaluate the array as polynomial at the value of -val^2
]
Source Link
Bubbler
  • 79.5k
  • 5
  • 164
  • 490

Factor + math.polynomials math.factorials, 66 bytes

[| x | 1 x [ sq neg x [0,b) [ 2 * n! recip ] map polyval ] times ]

Try it online!

Factor has rational numbers, but no arbitrary-precision decimals. This answer tries to exploit it as much as possible: iterate the evaluation of the fist n terms of Taylor series (more precisely, Maclaurin series) n times, with the starting value of 1. The output is given as a rational number; the floating-point representation is also shown on TIO to check the value in human-readable form.

The function is very slow to calculate and very slow to converge, but in theory it must converge to the Dottie number as n increases, since the iterated function approaches the cosine function, the iteration count increases to infinity, and the whole computation is done in rationals.

How it works

[| x |  ! an anonymous function that takes one arg `x` as a local variable
  1 x [ ... ] times  ! repeat the inner function x times on the value 1...
    sq neg           !   val -> -val^2
    x [0,b)          !   0..x-1
    [ 2 * n! recip ] map  ! convert each number `i` to 1/(2i)!
    polyval          !   evaluate the array as polynomial at the value of -val^2
]