Output with the same number of digits of pi as the length of the code



Your challenge is to print x digits of pi where x is your code length.


Source code (length) -> output
foo!        (4)         3.141
foobar      (6)         3.14159
kinda long  (10)        3.141592653
+++++       (5)         3.1415
123456789   (9)         3.14159265

You can use floor(π/10 * 10code_length) / 10code_length - 1 to determine the number of digits you need to print. Note that the decimal point does not count as a digit of pi - so code of length 3 should have output 3.14, of length 4.


  • Your code length must be larger than three characters.
  • You may not use any standard loopholes.
  • You may use any standard allowed output methods.
  • You may not read your source to determine code length.
  • You may not use a builtin pi constant.
  • Pi must be completely accurate and not approximated.
  • The decimal point in the output is necessary. If you choose to output via return value, you must return a floating point integer.
  • The goal of this challenge is to find the shortest solution in each language, not the shortest language for the solution. Don't be afraid to post a solution in a language when a shorter solution is posted in the same language as long as your solution uses a different method.


Posted 2017-09-15T03:35:14.343

Reputation: 11 605

...so would this be valid or not?

– Jonathan Allan – 2017-09-15T16:59:11.143

(edit ^ this even (rounding))

– Jonathan Allan – 2017-09-15T17:07:04.200

“Code length” ← in bytes or in characters? – Lynn – 2017-09-17T21:19:27.830

@Lynn Bytes, as usual. – MD XF – 2017-09-17T23:50:20.990

Is just 3 allowed? – NoOneIsHere – 2018-04-23T21:47:03.163

@NoOneIsHere Your code length must be larger than three characters. – Jo King – 2018-04-23T22:51:56.587



Japt, 9 4 bytes

As short as it can allowably get :)

Includes an unprintable (reverse line feed, charcode 141) after the #.


Test it

In Japt, any character following # is converted to its character code and appended to any digits or decimal points which may precede it, in this case the 3.


Posted 2017-09-15T03:35:14.343

Reputation: 24 623


Mathematica, 18 bytes

using zeta function

enter image description here


Try it online!


Posted 2017-09-15T03:35:14.343

Reputation: 15 931


Jelly, 9 bytes


Try it online!

Outputs 3.14159265

How it Works

“Œı⁸Ç’     - The integer 314159265
      ÷    - divide
       ȷ8  - The integer 100000000


Posted 2017-09-15T03:35:14.343

Reputation: 7 021

Pi is not being calculated! – sergiol – 2017-09-16T09:30:36.633

5yes it is, dividing 314159265 by 100000000 :-) – Florian F – 2017-09-18T12:43:51.163


VBA (Excel), 15 bytes

MsgBox 4*Atn(1)

Output: 3.14159265358979

I think this is not acceptable as I golfed it just to fit the length of the PI. The un-golfed length of it is 17 bytes. D:


Posted 2017-09-15T03:35:14.343

Reputation: 511

1Looks as though you have not yet been welcomed, so, welcome to the site! – MD XF – 2017-09-15T04:09:53.883

Oh, Thank you, Sir! :D – remoel – 2017-09-15T04:18:27.943

Isn't Atn arctangent? I'm not sure if this is allowed... asking OP – JungHwan Min – 2017-09-15T04:28:02.090

I thought OP is referring for built-in PI only. My bad. – remoel – 2017-09-15T04:35:53.233


Jelly, 6 bytes


Try it online!

Prints 3,14159 with the allowed "European" decimal separator, ,.


3;⁽4ƒṾ - Main link: no arguments
3      - literal 3                3
  ⁽4ƒ  - base 250 literal 14159   14159
 ;     - concatenate              [3,14159]
     Ṿ - unevaluate               ['3', ',', '1', '4', '1', '5', '9']
       - implicit print           3,14159

Jonathan Allan

Posted 2017-09-15T03:35:14.343

Reputation: 67 804

1That digit list looks odd. I think there is an extra ,' in there. – Jonathan Frech – 2017-09-15T04:42:09.047

1You can do -ÆAær5 and use . instead of ,, but then it would be the same as my Jelly solution. – Zacharý – 2017-09-15T21:02:09.013


Python 3, 64 bytes


Try it online!

Leaky Nun

Posted 2017-09-15T03:35:14.343

Reputation: 45 011

The Python 2 equivalent is 60 bytes print'3.%d'%int('3p3aqd50r7654t9ywkfkgsm0yx0bikcluxlhsm',36). – Jonathan Allan – 2017-09-15T04:40:16.620

@primo looks like one to post if&when... – Jonathan Allan – 2017-09-15T13:02:56.877

1@JonathanAllan if you mean when it's reopened, it is now. – MD XF – 2017-09-15T16:47:49.127


Desmos, 130 bytes

f\left(\right)=\frac{\operatorname{floor}\left(10^{128}\cdot 4\sum_{k=1}^{9^9}\frac{\left(-1\right)^{k+1}}{2k-1}\right)}{10^{128}}

The source code for this (Which can be accessed by copy and pasting inside and outside of Desmos) isn't optimal when generated using the Desmos editor, so a few bytes of whitespace were golfed down where possible.

This defines a function f which takes no arguments returns pi, calculated using the Gregory Sequence to k=9^9 (I can't confirm this is accurate enough, however I am of the believe that it is, it can be made more accurate with a greater value of k) it then floors the result to 128 decimal places, which, alongside the 3., is the length of the source code.

Try it online!


Posted 2017-09-15T03:35:14.343

Reputation: 7 898

How does one actually see the result - I only see 3.14159265617, am I looking in the wrong place; do I need to hit a button? – Jonathan Allan – 2017-09-15T05:58:44.833

Desmos doesn't defaultly print to as many decimal places as provided, but the value should still be accurate. – ATaco – 2017-09-15T05:59:28.970

1That is disappointing since the challenge is "to print..." – Jonathan Allan – 2017-09-15T06:00:06.353

1"The decimal point in the output is necessary. If you choose to output via return value, you must return a floating point integer." which makes this valid. – ATaco – 2017-09-15T06:02:27.587

I feel the intent of that statement was not to allow for less precision but to ensure a decimal separator. Mind you I don't think your answer is bad. – Jonathan Allan – 2017-09-15T06:06:51.320

The precision is there, it's just that Desmos can't print it all in a single output, but it can return from a function fine. – ATaco – 2017-09-15T06:20:55.847


SOGL V0.12, 10 9 7 bytes


Try it Here!


4ΘΞ“     push 31415
    6⌡   6 times do
      υ    divide by 10


Posted 2017-09-15T03:35:14.343

Reputation: 19 048


Charcoal, 8 bytes


Try it online!

Just a compressed string.


Posted 2017-09-15T03:35:14.343

Reputation: 4 687


Javascript, 16 bytes



Herman L

Posted 2017-09-15T03:35:14.343

Reputation: 3 611


Neim, 5 bytes



3      Push 3
 FB    F pushes 45, B converts it into a character code, '.'
   ρ   Push 14
    σ  Push 15
Implicitly concatenate and print stack

Try it online!


Posted 2017-09-15T03:35:14.343

Reputation: 15 025


CJam, 5 bytes


Try it online!

How it works

3.14   e# Push 3.14.
    F  e# Push 15.


Posted 2017-09-15T03:35:14.343

Reputation: 196 637

If only you could've done 3.E. :P, either way, amazing abuse of IO. – Zacharý – 2017-09-18T11:19:40.927

Was going to post 3'.EF, but then I saw this answer... – Esolanging Fruit – 2018-04-23T23:48:22.920


APL, 6 bytes


Takes the imaginary part of ln(-1).

Try it online!


Posted 2017-09-15T03:35:14.343

Reputation: 11 708

Hey, welcome back @Uriel! Nice one, I wasn't thinking of anything like that (with logs of negative numbers), nice job! – Zacharý – 2017-09-18T11:09:44.103

@Zacharý thanks, just dropped a visit and couldn't help answering this :P – Uriel – 2017-09-18T11:16:00.137


J, 16 bytes

17j15":4*_3 o.1:

Try it online!

Similar to the VBA answer. Outputs: 3.141592653589793.

Conor O'Brien

Posted 2017-09-15T03:35:14.343

Reputation: 36 228

"You may not use a builtin macro or constant to determine pi." Funny, it's your comment – Leaky Nun – 2017-09-15T04:27:15.023

Your output is 17 bytes, not 16. – ATaco – 2017-09-15T06:24:04.750

1@ATaco exactly, it's 16 digits – ASCII-only – 2017-09-15T09:55:46.420

@LeakyNun it doesn't. _3 o.1 calculates atan(1). As you may have known, I'm well aware of the rules. – Conor O'Brien – 2017-09-15T10:50:24.113


Math.JS, 17 bytes.


That's 5 leading spaces

This calculates to 15 decimal places, and implicitly prints it.

Try It Online!


Posted 2017-09-15T03:35:14.343

Reputation: 7 898

Well, technically this calculates the imaginary portion of ln(-1), but since it's a purely imaginary number, it's equivalent to dividing by i. – Zacharý – 2017-09-15T20:43:34.667


05AB1E, 11 bytes


Try it online!

3           # [3]                   | Push 3.
 •∊&SK•     # [3, 1415926535]       | Push compressed base-255 number 1415926535.
       )    # [[3,1415926535]]      | Wrap the 2 to an array.
        '.  # [[3,1415926535],'.']  | Push a decimal point.
          ý # ['3.1415926535']      | Join 3 and 1415926535 with a decimal point.


05AB1E, 4 bytes


Try it online!

3žs doesn't work for some reason...

Magic Octopus Urn

Posted 2017-09-15T03:35:14.343

Reputation: 19 422

@Zacharý did I miss something? Why is that special xD? – Magic Octopus Urn – 2017-09-15T20:33:28.173

It's in my username, and also, can you add an explanation? – Zacharý – 2017-09-15T20:41:30.373

@Zacharý HA! My brain was going through all the possible funny PPCG-related jokes it could be, coming up blank. I overthink crap. – Magic Octopus Urn – 2017-09-15T20:43:52.763

@Zacharý also, added. – Magic Octopus Urn – 2017-09-15T20:46:45.307

If MY had implicit IO, there would be no way to beat E3., I'm mad at myself now :p. – Zacharý – 2017-09-15T20:48:35.567

@Zacharý can I get a link to "MY"? If you want a comment link use: "[MY](<link to my github>)". – Magic Octopus Urn – 2017-09-15T21:08:16.790

Here: https://bitbucket.org/zacharyjtaylor/my-language/src. MY does have a TIO (it's not synced due to me being suspended from chat for just pushing it way too far, (if anyone wants to ask Dennis to pull MY for me, that would be nice)).

– Zacharý – 2017-09-15T21:22:43.250

@Zacharý I have requested a pull but apparently it doesn't work.

– caird coinheringaahing – 2017-09-18T16:46:29.743

There, I fixed that >_<. It can hopefully be pulled successfully. – Zacharý – 2017-09-18T20:15:28.207

2žs does work now and for the non-built-in version you could have •Iò₅U•8°/. – Emigna – 2019-04-16T09:09:53.583


ForceLang, 269 bytes

set S 0x5F258E476FC1B3B5571012206C637089460E41E814CB071E61431B5F0671F551D18D2C5D2D3A2565E408C6DE8D753F595B6E9979C3866D1C9965008DCFB02E3BD11D21DFFAF17978F05C8BBACF55A5ED5E90B1D8CAD8736AA4B728EB342B453F86353DB371D322B6A98613BC5CCB00AC2.mult 1e-270
io.write S.toString 268


Posted 2017-09-15T03:35:14.343

Reputation: 11 342

Link to the language on github at least.

– Jonathan Allan – 2017-09-17T01:10:25.367


MATL, 9 8 bytes


Try it online!


-1      % number literal
X;      % inverse cosine (radians), giving pi
V       % convert to string
9       % specify precision
:)      % keep first 9 characters - 8 digits of precision

Saved one byte thanks to Luis Mendo!

B. Mehta

Posted 2017-09-15T03:35:14.343

Reputation: 763


MY, 7 bytes


Try it online!


  • , decrement the top of the stack (0 is popped if empty, making the stack [-1]). (Same symbol as Jelly)
  • 2, push 2.
  • ō, since the top of the stack is two, discard the 2, pop n, then push acos(n) (in radians, this gives pi, this symbol is o with a negative sign on top of it. o (for normal trig) comes from APL).
  • ", pop the top element of the stack and push it as a string.
  • 8, push 8.
  • , pop a then b, push b[:a] (This is another symbol taken from APL).
  • , output with a new line (OUTPUT <- STACK).


Posted 2017-09-15T03:35:14.343

Reputation: 5 710

Then add implicit output :P – MD XF – 2017-09-17T07:07:39.827

I'm keeping true to the nature of MY, I might make an improved version with actual syntax, and definitely with implicit output – Zacharý – 2017-09-17T15:23:03.177


Jelly, 6 bytes


Try it online!

Uses a different approach than the other Jelly answers.


-ÆA is inverse cosine of -1 (which is pi), ær5 retrieves the needed section. It worked out so rounding is equivalent to truncation in this case


Posted 2017-09-15T03:35:14.343

Reputation: 5 710


Haskell, 15 bytes


Try it online!

Using @JungHwanMin formula.


Posted 2017-09-15T03:35:14.343

Reputation: 1 764


Perl 5, 40 bytes


Requires the command line option -pMbignum, counted as 8.

Sample Useage:

$ perl -pMbignum test.pl < /dev/null

I apologize for no TIO link, it doesn't seem to support bignum (or an unmatched opening brace...).


Posted 2017-09-15T03:35:14.343

Reputation: 30 891


RPL (HP48), 86.5 bytes, 87 digits [Does this count?]

« GROB 8 43

GROB is an image keyword. The 2 numbers that follow are width and height. The hexadecimal digits that follow are bitmap. That's 1 byte of storage for every 2 digits plus the image metadata.

Empty program « » takes 10 bytes. A command takes 2.5 bytes. A float takes 10.5 bytes, but if it's equal to a single digit integer, it can take 2.5 bytes.

HP48 stores 4 rather than 8 bits at each memory location, so a byte occupies 2 consecutive locations (low endian). Often 20-bit chunks are used, and they do take 2.5 bytes (not 3).


Posted 2017-09-15T03:35:14.343

Reputation: 191


><>, 16 bytes


Try it online!

There's a couple of unprintables in there, with the first having value 26 and the second 141.

Jo King

Posted 2017-09-15T03:35:14.343

Reputation: 38 234


Pyth, 9 bytes

<`.ttZ4 T

Try it here.

Works with the fact that .

Mr. Xcoder

Posted 2017-09-15T03:35:14.343

Reputation: 39 774


Ruby, 16 bytes

->{Math.acos -1}

Try it online!


Posted 2017-09-15T03:35:14.343

Reputation: 251


Excel VBA, 16 Bytes

Anonymous VBE immediate window functions that takes no input and output to any of Excel VBA's STDOUTs

Each line represents a separate function that returns Pi of length 16


Taylor Scott

Posted 2017-09-15T03:35:14.343

Reputation: 6 709


Octave, 16 bytes


Try it online!

I'm having a hard time imagining how this could be shorter...


Posted 2017-09-15T03:35:14.343

Reputation: 8 530


PHP, 14 Bytes

Try it online!


A bit tricky, but the precition of M_PI is 3.1415926535898

Francisco Hahn

Posted 2017-09-15T03:35:14.343

Reputation: 591


Brainfuck (32 bit), 672 660 bytes


All credit should go to Felix Nawothnig's yapi.b.

Should you decide to downvote, please leave a comment explaining why.


Posted 2017-09-15T03:35:14.343

Reputation: 339

@JoKing Thanks for your feedback. To get the correct amount of digits you have to use 32 bit cells as specified in the title (160 = 672 mod 256). I have now also added a link to the source. – orthoplex – 2019-04-16T07:40:54.633


TI-BASIC, 7 bytes




These three statements will print 3.141593.

Note: TI-BASIC is a tokenized language. Character count does not equal byte count.


Posted 2017-09-15T03:35:14.343

Reputation: 1 935


Perl 6, 16 bytes

{4*atan       1}

Try it online!


Posted 2017-09-15T03:35:14.343

Reputation: 1 831

113 bytes using tau – Jo King – 2019-04-16T06:13:56.927