The Dottie number is the fixed point of the cosine function, or the solution to the equation cos(x)=x.1

Your task will be to make code that approximates this constant. Your code should represent a function that takes an integer as input and outputs a real number. The limit of your function as the input grows should be the Dottie number.

You may output as a fraction, a decimal, or an algebraic representation of a number. Your output should be capable of being arbitrarily precise, floats and doubles are not sufficient for this challenge. If your language is not capable of arbitrary precision numbers, then you must either implement them or choose a new language.

This is a question so answers will be scored in bytes, with fewer bytes being better.


One way of calculating the constant is to take any number and repeatedly apply the cosine to it. As the number of applications tends towards infinity the result tends towards the fixed point of cosine.

Here is a fairly accurate approximation of the number.


1: Here we will take cosine in radians

  • \$\begingroup\$ So, if we are using Python, we must implement our own type or import Decimal? \$\endgroup\$ – Mr. Xcoder Aug 4 '17 at 15:28
  • \$\begingroup\$ How accurate must our submissions be? \$\endgroup\$ – Mr. Xcoder Aug 4 '17 at 15:30
  • \$\begingroup\$ Goes to Jelly tutorial to steal ÆẠȷ¡ realizes it's invalid. Tries Brachylog; oh no Brachylog doesn't even do floats. \$\endgroup\$ – Erik the Outgolfer Aug 4 '17 at 15:30
  • 1
    \$\begingroup\$ I feel like the "arbitrarily precise" requirement is a bit too stringent. Why not consider an answer valid once x=cos(x)? \$\endgroup\$ – kamoroso94 Aug 4 '17 at 16:58
  • 2
    \$\begingroup\$ I would like to see this in Haskell, APL, and some Lisp flavor. \$\endgroup\$ – Mark C Oct 25 '17 at 8:50

19 Answers 19


MATL, 34 30 19 bytes

11 bytes off thanks to Sanchises!


The last decimal figures in the output may be off. However, the number of correct figures starting from the left increases with the input, and the result converges to the actual constant.

Try it online!


For input n, and starting at x=1, this applies the function

              x ↦ cos(x)

with n-digit variable-precision arithmetic n times.

48         % Push 48, which is ASCII for '1': initial value for x as a string
i:"        % Do n times, where n is the input
  'cos('   %   Push this string
  w        %   Swap. Moves current string x onto the top of the stack
  h        %   Concatenate
  41       %   Push 41, which is ASCII for ')'
  h        %   Concatenate. This gives the string 'cos(x)', where x is the
           %   current number
  GY$      %   Evaluate with variable-prevision arithmetic using n digits
           %   The result is a string, which represents the new x
           % End (implicit). Display (implicit). The stack contains the last x
  • 1
    \$\begingroup\$ Why not just apply it n times at n digits precision? This seems overly complicated. \$\endgroup\$ – Sanchises Aug 4 '17 at 21:13
  • 1
    \$\begingroup\$ This is incredible. I want to see it in APL. \$\endgroup\$ – Mark C Oct 25 '17 at 8:48

Python 3, 58 bytes

lambda n:S('cos('*n+'0'+')'*n).evalf(n)
from sympy import*

Try it online!


GNU bc -l, 30

Score includes +1 for -l flag to bc.


The final newline is significant and necessary.

Try it online.

-l does 2 things:

  • enable the "math" library, including c() for cos(x)
  • sets precision (scale) to 20 decimal places (bc has arbitrary precision calculation)

I'm not really clear on the precision requirement. As it is, this program calculates to 20 decimal places. If a different precision is required, then scale=n; needs to be inserted at the start of the program, where n is the number of decimal places. I don't know if I should add this to my score or not.

Note also that for some numbers of decimal places (e.g. 21, but not 20), the calculation oscillates either side of the solution in the last digit. Thus in the comparison of current and previous iterations, I divide both sides by 10 (A) to erase the last digit.


Wolfram Language (Mathematica), 22 bytes


Try it online!


dzaima/APL, 55 bytes


Using big integer (no big decimals!) arithmetic (where \$10^N\$ is the equivalent of a 1), iterate the first \$N\$ terms of the Taylor series (an overestimate, but that's fine) until a duplicate has been encountered. May be off by a bit due to lost precision in the end, but, as with other answers, those differences will disappear with higher \$N\$.

No TIO link as TIO's dzaima/APL hasn't been updated to support bigintegers.

Example I/O:





R (+Rmpfr), 55 bytes

function(n,b=Rmpfr::mpfr(1,n)){for(i in 1:n)b=cos(b);b}

Dennis has now added Rmpfr to TIO so this will work; added some test cases.


Takes the code I wrote from this challenge to evaluate cos n times starting at 1, but first I specify the precision I want the values to be in by creating an object b of class mpfr with value 1 and precision n, n>=2, so we get more precision as we go along.

Try it online!

  • 3
    \$\begingroup\$ Try again. :) In the future, if anything is missing from TIO, don't hesitate to drop a message in talk.tryitonline.net. \$\endgroup\$ – Dennis Aug 4 '17 at 16:05
  • \$\begingroup\$ @Dennis Thank you! I'll keep that in mind in the future! \$\endgroup\$ – Giuseppe Aug 4 '17 at 16:08

Octave, 42 bytes


Try it online!

Pretty much a duplicate of my answer to Approximate the Plastic Number, but somewhat shorter due to more relaxed requirements.


K: 6 bytes


f/ applies f until it reaches a fixed point.

  • \$\begingroup\$ what version of k is this? _ is floor in most versions i know. in k4 and oK you can get 5 bytes with cos/1 \$\endgroup\$ – scrawl Mar 9 at 16:39
  • \$\begingroup\$ K3. Built-ins start with an underscore there. \$\endgroup\$ – tangentstorm Mar 10 at 11:43
  • 2
    \$\begingroup\$ interesting! i haven't seen k3 in the wild. might be worth labeling it as such as there are more than a couple versions used on this forum :) \$\endgroup\$ – scrawl Mar 10 at 17:48
  • \$\begingroup\$ If the Dyalog APL solution is considered invalid, this should be considered invalid as well as it is exactly the same algorithm. \$\endgroup\$ – Jeff Zeitlin Mar 18 at 10:51

Mathics or Mathematica, 46 bytes


Try it online!


PHP, 50 bytes


Try it online!

  • \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ – James Aug 4 '17 at 18:13
  • \$\begingroup\$ I believe that for($a=$argv[1];$a--;)$j=cos($j);echo$j; (40 bytes) is enough. \$\endgroup\$ – Ismael Miguel Aug 5 '17 at 11:44

Pyth, 57 54 bytes


This would be much shorter if we didn't need the Decimal to be up to spec, but it is what it is.

Edit 1: -3 bytes because we need a number anyways, so we can use Xs returned copy of globals() length as our starting value, moving it to the end and removing a $ and some whitespace.

Try it online!


APL (Dyalog Unicode), 9 bytes


Try it online!

(Note: The TIO has an additional ⎕←; this is required by TIO. A “standalone” APL interpreter would use the exact expression shown above. The byte count given is what TIO calculates for the expression above, not for the one with the ⎕←.)


  ⍣          Apply repeatedly the function...
2○           ...cosine of x (in radians), such that...
    1        ...the initial value of x is 1, and...
   =         ...if cos x is NOT equal to x, then re-evaluate, substituting cos x for x...
             ...until they ARE equal.

The first time the function cos x (2○x) is evaluated, with x=1, they will not be equal. cos 1 is 0.5403..., so re-evaluate, replacing 1 with 0.5403... and repeat the process until (2○x)=x, which occurs for x=0.73908...

In creating this, I used the default value for the "printing precision", which can be set in APL using ⎕PP←; the maximum value for ⎕PP that Dyalog APL allows is 34 digits.

Also, the default precision for this implementation is 64-bit floats; one can use 128-bit floats by setting ⎕FR←1287. The TIO calculation is done with 64-bit floats.

No actual implementation of a computer language can give truly arbitrary precision; however, the code for a theoretical APL that did implement arbitrary precision would be exactly the same.

  • \$\begingroup\$ This is 5 bytes. \$\endgroup\$ – Adám Mar 15 at 19:30
  • \$\begingroup\$ As you mentioned in chat, this should be a NARS2000 solution, it is invalid in Dyalog. Then, to make it a proper entry, you should set ⎕CT←0, and probably assign input to ⎕FPC. \$\endgroup\$ – dzaima Mar 15 at 22:35
  • 1
    \$\begingroup\$ This doesn't support arbitrary precision, which is required by the spec: Your output should be capable of being arbitrarily precise, floats and doubles are not sufficient for this challenge. \$\endgroup\$ – Grimmy Mar 15 at 23:00
  • 1
    \$\begingroup\$ @Adám This answer is invalid; see Grimmy's comment above. dzaima's solution would work but it's not the first. \$\endgroup\$ – S.S. Anne Mar 18 at 0:18
  • 1
    \$\begingroup\$ @JeffZeitlin your answer is still invalid. Adding an (obviously wrong) remark about how arbitrary precision is impossible doesn’t help. Try looking at some valid answers for comparison. \$\endgroup\$ – Grimmy Mar 18 at 14:37

JavaScript (Node.js), 84 bytes


Try it online!

Has a precision of roughly n-1 digits. BigInt is used and cos(x) is calculated using its Taylor expansion. The I-Z>>2n part is used only to prevent looping forever (with a cost of 4 bytes and some precision). Although theoretical applicable for arbitrary precision, practical range is n<63 because of stack overflow.

Shorter (82 bytes), no worries about stack overflow, but far fewer precision


Much shorter (80 bytes), larger range until stack overflow (n<172), but same precision as the 82-byte.


If arbitrary precision is not the main point, then 25 bytes:


Python - 89 bytes

Uses decimal module.

from decimal import*
import math
lambda n:reduce(lambda a,b:Decimal(math.cos(a)),[1]*n,1)

Perl 5, 41 Bytes

use bignum;sub f{$_[0]?cos(f($_[0]-1)):0}

Bignum is required for the arbitrary precision. Defines a function f that recursively applies cosine to 0 N times.

TIO doesn't seem to have bignum so no link :(


Mathematica 44 Bytes


FindRoot uses Newton's method by default.


Python 2, 86 bytes

import math as m,decimal as d
def f(x,n):return f(d.Decimal(m.cos(x)),n-1)if n else x

New version using the tip provided.

Python 2, 105 bytes

import math as m,decimal as d
def f(x,n):return d.Decimal(f(x+(m.cos(x)-x)/(m.sin(x)+1),n-1))if n else x

Uses Newton's method and recursive function to calculate the value. x is initial value and n is the recursion limit.

  • \$\begingroup\$ Python's builtin float type does have indefinite precision, thus your function is not actually asymptotic. \$\endgroup\$ – Ad Hoc Garf Hunter Aug 4 '17 at 22:39
  • \$\begingroup\$ Thanks, good to know. Fixed I guess, not very short anymore tho :) \$\endgroup\$ – SydB Aug 4 '17 at 22:54
  • \$\begingroup\$ The tip provided in the question would probably be shorter than Newton's method. \$\endgroup\$ – Ad Hoc Garf Hunter Aug 4 '17 at 22:55
  • \$\begingroup\$ Thanks again, seems like I was too carried away with fancy mathematics. \$\endgroup\$ – SydB Aug 4 '17 at 23:13
  • \$\begingroup\$ Your function should only take 1 argument n as per requirement, so you need to default x=0. Also this function is not arbitrary-precise, since math.cos is fixed precision. \$\endgroup\$ – Surculose Sputum Mar 11 at 1:07

Axiom, 174 bytes

f(n:PI):Complex Float==(n>10^4=>%i;m:=digits(n+10);e:=10^(-n-7);a:=0;repeat(b:=a+(cos(a)-a)/(sin(a)+1.);if a~=0 and a-b<e then break;a:=b);a:=floor(b*10^n)/10.^n;digits(m);a)

ungolfed and commented

-- Input: n:PI numero di cifre
-- Output la soluzione x a cos(x)=x con n cifre significative dopo la virgola
-- Usa il metodo di Newton a_0:=a  a_(n+1)=a_n-f(a_n)/f'(a_n)
fo(n:PI):Complex Float==
  a:=0     -- Punto iniziale
     if a~=0 and a-b<e then break


(3) -> for i in 1..10 repeat output[i,f(i)]
                                                               Type: Void
           Time: 0.12 (IN) + 0.10 (EV) + 0.12 (OT) + 0.02 (GC) = 0.35 sec
(4) -> f 300
  0.7390851332 1516064165 5312087673 8734040134 1175890075 7464965680 635773284
  6 5488354759 4599376106 9317665318 4980124664 3987163027 7149036913 084203157
  8 0440574620 7786885249 0389153928 9438845095 2348013356 3127677223 158095635
  3 7765724512 0437341993 6433512538 4097800343 4064670047 9402143478 080271801
  8 8377113613 8204206631
                                                      Type: Complex Float
                                   Time: 0.03 (IN) + 0.07 (OT) = 0.10 sec

I would use the Newton method because it would be faster than 'repeated cos(x) method'

 800   92x
1000  153x
2000  379x

where in the first column there is the number of digit and in the second column there is how much Newton method is faster than use repeated cos(x) method, here. Good Morning


Stax, 5 bytes


Run and debug it Explaination:

1       Push 1 to the stack, this will be our initial variable
 {      Begin block
  |7    Cosine
    }N  Repeat block a number of times specified by the input
  • 2
    \$\begingroup\$ This does not support arbitrary precision, which is required by the spec: Your output should be capable of being arbitrarily precise, floats and doubles are not sufficient for this challenge. \$\endgroup\$ – Grimmy Mar 15 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.