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.

Examples:

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.

Rules:

• 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 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.
• ...so would this be valid or not? – Jonathan Allan Sep 15 '17 at 16:59
• (edit ^ this even (rounding)) – Jonathan Allan Sep 15 '17 at 17:07
• “Code length” ← in bytes or in characters? – Lynn Sep 17 '17 at 21:19
• @Lynn Bytes, as usual. – MD XF Sep 17 '17 at 23:50
• Is just 3 allowed? – NoOneIsHere Apr 23 '18 at 21:47

Japt, 9 4 bytes

As short as it can allowably get :)

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

3.#


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.

Mathematica, 18 bytes

using zeta function

Sqrt[6Zeta@2]~N~18


Try it online!

Jelly, 9 bytes

“Œı⁸Ç’÷ȷ8


Try it online!

Outputs 3.14159265

How it Works

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

• Pi is not being calculated! – sergiol Sep 16 '17 at 9:30
• yes it is, dividing 314159265 by 100000000 :-) – Florian F Sep 18 '17 at 12:43

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:

• Looks as though you have not yet been welcomed, so, welcome to the site! – MD XF Sep 15 '17 at 4:09
• Oh, Thank you, Sir! :D – remoel Sep 15 '17 at 4:18
• Isn't Atn arctangent? I'm not sure if this is allowed... asking OP – JungHwan Min Sep 15 '17 at 4:28
• I thought OP is referring for built-in PI only. My bad. – remoel Sep 15 '17 at 4:35

Jelly, 6 bytes

3;⁽4ƒṾ


Try it online!

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

How?

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

• That digit list looks odd. I think there is an extra ,' in there. – Jonathan Frech Sep 15 '17 at 4:42
• You can do -ÆAær5 and use . instead of ,, but then it would be the same as my Jelly solution. – Zacharý Sep 15 '17 at 21:02

Python 3, 64 bytes

print("3.%d"%int("SIXW57LUPVBUA10HBQJOL57QLF54UT0U00KIN32O",36))


Try it online!

• The Python 2 equivalent is 60 bytes print'3.%d'%int('3p3aqd50r7654t9ywkfkgsm0yx0bikcluxlhsm',36). – Jonathan Allan Sep 15 '17 at 4:40
• @JonathanAllan python 2 in 59: tio.run/##K6gsycjPM/r/… – primo Sep 15 '17 at 13:01
• @primo looks like one to post if&when... – Jonathan Allan Sep 15 '17 at 13:02
• @JonathanAllan if you mean when it's reopened, it is now. – MD XF Sep 15 '17 at 16:47

SOGL V0.12, 109 7 bytes

4ΘΞ“6⌡υ


Try it Here!

Explanation:

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


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!

• 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 Sep 15 '17 at 5:58
• Desmos doesn't defaultly print to as many decimal places as provided, but the value should still be accurate. – ATaco Sep 15 '17 at 5:59
• That is disappointing since the challenge is "to print..." – Jonathan Allan Sep 15 '17 at 6:00
• "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 Sep 15 '17 at 6:02
• 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 Sep 15 '17 at 6:06

Charcoal, 8 bytes

”i‴Ａχ:&”


Try it online!

Just a compressed string.

Javascript, 16 bytes

_=>Math.acos(-1)


f=
_=>Math.acos(-1)
console.log(f())

CJam, 5 bytes

3.14F


Try it online!

How it works

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

• If only you could've done 3.E. :P, either way, amazing abuse of IO. – Zacharý Sep 18 '17 at 11:19
• Was going to post 3'.EF, but then I saw this answer... – Esolanging Fruit Apr 23 '18 at 23:48

Neim, 5 bytes

3FBρσ


Explanation:

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!

APL, 6 bytes

10○⍟-1


Takes the imaginary part of ln(-1).

Try it online!

• Hey, welcome back @Uriel! Nice one, I wasn't thinking of anything like that (with logs of negative numbers), nice job! – Zacharý Sep 18 '17 at 11:09
• @Zacharý thanks, just dropped a visit and couldn't help answering this :P – Uriel Sep 18 '17 at 11:16

J, 16 bytes

17j15":4*_3 o.1:


Try it online!

Similar to the VBA answer. Outputs: 3.141592653589793.

• "You may not use a builtin macro or constant to determine pi." Funny, it's your comment – Leaky Nun Sep 15 '17 at 4:27
• Your output is 17 bytes, not 16. – ATaco Sep 15 '17 at 6:24
• @ATaco exactly, it's 16 digits – ASCII-only Sep 15 '17 at 9:55
• @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 Sep 15 '17 at 10:50

Math.JS, 17 bytes.

     log(-1,e).im


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

Try It Online!

• 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ý Sep 15 '17 at 20:43

05AB1E, 11 bytes

3•∊&SK•)'.ý


Try it online!

COMMAND     # CURRENT STACK         | EXPLANATION
------------#-----------------------+-----------------------------------
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.


USING BUILT-INS:

05AB1E, 4 bytes

žq5£


Try it online!

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

• @Zacharý did I miss something? Why is that special xD? – Magic Octopus Urn Sep 15 '17 at 20:33
• It's in my username, and also, can you add an explanation? – Zacharý Sep 15 '17 at 20:41
• @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 Sep 15 '17 at 20:43
• @Zacharý also, added. – Magic Octopus Urn Sep 15 '17 at 20:46
• If MY had implicit IO, there would be no way to beat E3., I'm mad at myself now :p. – Zacharý Sep 15 '17 at 20:48

ForceLang, 269 bytes

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


MATL, 9 8 bytes

-1X;V9:)


Try it online!

Explanation:

-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!

Jelly, 6 bytes

-ÆAær5


Try it online!

Uses a different approach than the other Jelly answers.

How?

-Æ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

 acos(-1)-3e-15


Try it online!

Using @JungHwanMin formula.

Perl 5, 40 bytes

}{$\=$\/~(2*$_)*~$_+2for-130..-2


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

Sample Useage:

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


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

MY, 7 bytes

’2ō"8↑↵


Try it online!

How?

• ’, 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).
• Then add implicit output :P – MD XF Sep 17 '17 at 7:07
• I'm keeping true to the nature of MY, I might make an improved version with actual syntax, and definitely with implicit output – Zacharý Sep 17 '17 at 15:23

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

« GROB 8 43
14159265358979323846264338327950288419716939937510582097494459230781640628620899862803
→STR 9 OVER SIZE SUB 2 "." REPL »


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).

><>, 16 bytes

"]a:5;."3no>n<


Try it online!

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

Pyth, 9 bytes

<.ttZ4 T


Try it here.

Works with the fact that $\pi\approxeq\arccos(-1)$.

Ruby, 16 bytes

->{Math.acos -1}


Try it online!

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

?Mid(4*Atn(1),1)
?Str([Acos(-1)])
Debug.?4*Atn(1);
MsgBox[Acos(-1)]
[A1]="=ACOS(-1)"

Octave, 16 bytes

vpa(acos(-1),16)


Try it online!

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

PHP, 14 Bytes

Try it online!

<?=acos(-001);


A bit tricky, but the precition of M_PI is 3.1415926535898

TI-BASIC, 7 bytes

round(4tan⁻¹(1),6

round(2sin⁻¹(1),6

round(cos⁻¹(-1),6


These three statements will print 3.141593`.

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