9
\$\begingroup\$

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

32 Answers 32

3
\$\begingroup\$

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.

\$\endgroup\$
8
\$\begingroup\$

Mathematica, 18 bytes

using zeta function

enter image description here

Sqrt[6Zeta@2]~N~18

Try it online!

\$\endgroup\$
7
\$\begingroup\$

Jelly, 9 bytes

“Œı⁸Ç’÷ȷ8

Try it online!

Outputs 3.14159265

How it Works

“Œı⁸Ç’     - The integer 314159265
      ÷    - divide
       ȷ8  - The integer 100000000
\$\endgroup\$
  • \$\begingroup\$ Pi is not being calculated! \$\endgroup\$ – sergiol Sep 16 '17 at 9:30
  • 5
    \$\begingroup\$ yes it is, dividing 314159265 by 100000000 :-) \$\endgroup\$ – Florian F Sep 18 '17 at 12:43
6
\$\begingroup\$

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:

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

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

Python 3, 64 bytes

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

Try it online!

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

SOGL V0.12, 10 9 7 bytes

4ΘΞ“6⌡υ

Try it Here!

Explanation:

4ΘΞ“     push 31415
    6⌡   6 times do
      υ    divide by 10
\$\endgroup\$
4
\$\begingroup\$

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!

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

Charcoal, 8 bytes

”i‴Aχ:&”

Try it online!

Just a compressed string.

\$\endgroup\$
3
\$\begingroup\$

Javascript, 16 bytes

_=>Math.acos(-1)

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

\$\endgroup\$
3
\$\begingroup\$

CJam, 5 bytes

3.14F

Try it online!

How it works

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

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!

\$\endgroup\$
3
\$\begingroup\$

APL, 6 bytes

10○⍟-1

Takes the imaginary part of ln(-1).

Try it online!

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

J, 16 bytes

17j15":4*_3 o.1:

Try it online!

Similar to the VBA answer. Outputs: 3.141592653589793.

\$\endgroup\$
  • \$\begingroup\$ "You may not use a builtin macro or constant to determine pi." Funny, it's your comment \$\endgroup\$ – Leaky Nun Sep 15 '17 at 4:27
  • \$\begingroup\$ Your output is 17 bytes, not 16. \$\endgroup\$ – ATaco Sep 15 '17 at 6:24
  • 1
    \$\begingroup\$ @ATaco exactly, it's 16 digits \$\endgroup\$ – ASCII-only Sep 15 '17 at 9:55
  • \$\begingroup\$ @LeakyNun it doesn't. _3 o.1 calculates atan(1). As you may have known, I'm well aware of the rules. \$\endgroup\$ – Conor O'Brien Sep 15 '17 at 10:50
2
\$\begingroup\$

Math.JS, 17 bytes.

     log(-1,e).im

That's 5 leading spaces

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

Try It Online!

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Zacharý Sep 15 '17 at 20:43
2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ @Zacharý did I miss something? Why is that special xD? \$\endgroup\$ – Magic Octopus Urn Sep 15 '17 at 20:33
  • \$\begingroup\$ It's in my username, and also, can you add an explanation? \$\endgroup\$ – Zacharý Sep 15 '17 at 20:41
  • \$\begingroup\$ @Zacharý HA! My brain was going through all the possible funny PPCG-related jokes it could be, coming up blank. I overthink crap. \$\endgroup\$ – Magic Octopus Urn Sep 15 '17 at 20:43
  • \$\begingroup\$ @Zacharý also, added. \$\endgroup\$ – Magic Octopus Urn Sep 15 '17 at 20:46
  • \$\begingroup\$ If MY had implicit IO, there would be no way to beat E3., I'm mad at myself now :p. \$\endgroup\$ – Zacharý Sep 15 '17 at 20:48
2
\$\begingroup\$

ForceLang, 269 bytes

set S 0x5F258E476FC1B3B5571012206C637089460E41E814CB071E61431B5F0671F551D18D2C5D2D3A2565E408C6DE8D753F595B6E9979C3866D1C9965008DCFB02E3BD11D21DFFAF17978F05C8BBACF55A5ED5E90B1D8CAD8736AA4B728EB342B453F86353DB371D322B6A98613BC5CCB00AC2.mult 1e-270
io.write S.toString 268
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Haskell, 15 bytes

 acos(-1)-3e-15

Try it online!

Using @JungHwanMin formula.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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

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

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

Pyth, 9 bytes

<`.ttZ4 T

Try it here.

Works with the fact that .

\$\endgroup\$
0
\$\begingroup\$

Ruby, 16 bytes

->{Math.acos -1}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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)"
\$\endgroup\$
0
\$\begingroup\$

Octave, 16 bytes

vpa(acos(-1),16)

Try it online!

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

\$\endgroup\$
0
\$\begingroup\$

PHP, 14 Bytes

Try it online!

<?=acos(-001);

A bit tricky, but the precition of M_PI is 3.1415926535898

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

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.