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

9

2

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.

MD XF

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

Answers

3

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.

Shaggy

Posted 2017-09-15T03:35:14.343

Reputation: 24 623

8

Mathematica, 18 bytes

using zeta function

enter image description here

Sqrt[6Zeta@2]~N~18

Try it online!

J42161217

Posted 2017-09-15T03:35:14.343

Reputation: 15 931

7

Jelly, 9 bytes

“Œı⁸Ç’÷ȷ8

Try it online!

Outputs 3.14159265

How it Works

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

fireflame241

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

6

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:

remoel

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

5

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

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

4

Python 3, 64 bytes

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

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

4

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!

ATaco

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

4

SOGL V0.12, 10 9 7 bytes

4ΘΞ“6⌡υ

Try it Here!

Explanation:

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

dzaima

Posted 2017-09-15T03:35:14.343

Reputation: 19 048

3

Charcoal, 8 bytes

”i‴Aχ:&”

Try it online!

Just a compressed string.

ASCII-only

Posted 2017-09-15T03:35:14.343

Reputation: 4 687

3

Javascript, 16 bytes

_=>Math.acos(-1)

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

Herman L

Posted 2017-09-15T03:35:14.343

Reputation: 3 611

3

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!

Okx

Posted 2017-09-15T03:35:14.343

Reputation: 15 025

3

CJam, 5 bytes

3.14F

Try it online!

How it works

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

Dennis

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

3

APL, 6 bytes

10○⍟-1

Takes the imaginary part of ln(-1).

Try it online!

Uriel

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

2

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

2

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!

ATaco

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

2

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

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

2

ForceLang, 269 bytes

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

SuperJedi224

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

2

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!

B. Mehta

Posted 2017-09-15T03:35:14.343

Reputation: 763

1

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

Zacharý

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

1

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

Zacharý

Posted 2017-09-15T03:35:14.343

Reputation: 5 710

1

Haskell, 15 bytes

 acos(-1)-3e-15

Try it online!

Using @JungHwanMin formula.

jferard

Posted 2017-09-15T03:35:14.343

Reputation: 1 764

1

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

primo

Posted 2017-09-15T03:35:14.343

Reputation: 30 891

1

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

Heimdall

Posted 2017-09-15T03:35:14.343

Reputation: 191

1

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

Jo King

Posted 2017-09-15T03:35:14.343

Reputation: 38 234

0

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

0

Ruby, 16 bytes

->{Math.acos -1}

Try it online!

Snack

Posted 2017-09-15T03:35:14.343

Reputation: 251

0

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

Taylor Scott

Posted 2017-09-15T03:35:14.343

Reputation: 6 709

0

Octave, 16 bytes

vpa(acos(-1),16)

Try it online!

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

Sanchises

Posted 2017-09-15T03:35:14.343

Reputation: 8 530

0

PHP, 14 Bytes

Try it online!

<?=acos(-001);

A bit tricky, but the precition of M_PI is 3.1415926535898

Francisco Hahn

Posted 2017-09-15T03:35:14.343

Reputation: 591

0

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.

orthoplex

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

0

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.

Tau

Posted 2017-09-15T03:35:14.343

Reputation: 1 935

0

Perl 6, 16 bytes

{4*atan       1}

Try it online!

bb94

Posted 2017-09-15T03:35:14.343

Reputation: 1 831

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