# 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? Sep 15, 2017 at 16:59
• (edit ^ this even (rounding)) Sep 15, 2017 at 17:07
• “Code length” ← in bytes or in characters?
– Lynn
Sep 17, 2017 at 21:19
• @Lynn Bytes, as usual. Sep 17, 2017 at 23:50
• Is just 3 allowed? Apr 23, 2018 at 21:47

# 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! Sep 16, 2017 at 9:30
• yes it is, dividing 314159265 by 100000000 :-) Sep 18, 2017 at 12:43

# 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. Sep 15, 2017 at 4:42
• You can do -ÆAær5 and use . instead of ,, but then it would be the same as my Jelly solution. Sep 15, 2017 at 21:02

# 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! Sep 15, 2017 at 4:09
• Oh, Thank you, Sir! :D Sep 15, 2017 at 4:18
• Isn't Atn arctangent? I'm not sure if this is allowed... asking OP Sep 15, 2017 at 4:28
• I thought OP is referring for built-in PI only. My bad. Sep 15, 2017 at 4:35

# 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). Sep 15, 2017 at 4:40
• @JonathanAllan python 2 in 59: tio.run/##K6gsycjPM/r/… Sep 15, 2017 at 13:01
• @primo looks like one to post if&when... Sep 15, 2017 at 13:02
• @JonathanAllan if you mean when it's reopened, it is now. Sep 15, 2017 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

# 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. Sep 18, 2017 at 11:19
• Was going to post 3'.EF, but then I saw this answer... Apr 23, 2018 at 23:48

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

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

# Charcoal, 8 bytes

”i‴Ａχ:&”

Try it online!

Just a compressed string.

_=>Math.acos(-1)

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

# 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! Sep 18, 2017 at 11:09
• @Zacharý thanks, just dropped a visit and couldn't help answering this :P Sep 18, 2017 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 Sep 15, 2017 at 4:27
• Your output is 17 bytes, not 16. Sep 15, 2017 at 6:24
• @ATaco exactly, it's 16 digits Sep 15, 2017 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. Sep 15, 2017 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. Sep 15, 2017 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? Sep 15, 2017 at 20:33
• It's in my username, and also, can you add an explanation? Sep 15, 2017 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. Sep 15, 2017 at 20:43
• @Zacharý also, added. Sep 15, 2017 at 20:46
• If MY had implicit IO, there would be no way to beat E3., I'm mad at myself now :p. Sep 15, 2017 at 20:48

# 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

# ForceLang, 269 bytes

io.write S.toString 268
• Link to the language on github at least. Sep 17, 2017 at 1:10

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

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

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

# Julia 1.0, 16 bytes

show(acos(-1))##

Try it online!

This is the default precision printed, I sadly have to add 2 dummy characters but I don't think it's possible to make it shorter

• No bytes improvement, but there is no unused characters in print(4*atan(1)) Dec 19, 2022 at 19:52

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 Sep 17, 2017 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 Sep 17, 2017 at 15:23

# ><>, 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.

# C (gcc), 99 bytes

i;main(){for(;i<46;)printf(i++?"%02d":"3.",i[L"0;5:a]î.+& O2TÅ©']KiR J^-\N¤>cV0ý*uO"]);}

Try it online!

Hardcode 99 bottle of digits

# Pyth, 9 bytes

<`.ttZ4 T

Try it here.

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

->{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)"