Output Pi without math [closed]

In as few bytes as possible, your job is to write a program that outputs:

3.14


In celebration of a late Pi day of course! :)

Rules

You can do it anyway you like, but there are some restrictions.

• You may not use arithmetic operations anywhere in your program. This includes +, -, *, /, %, ^ (exponentiation), etc... This also includes incrementing (usually ++), decrementing (--), bitwise operations, and any built in functions that can be used as a replacement such as sum(), prod(), mod(), double(), pow(), sqrt(), inc(), dec(), etc... (Author's discretion)
• The digits 3, 1, and 4 may not appear anywhere in your code.
• You may not use any predefined variables/constants that your language may have as a replacement for 3, 1, and 4. (Author's discretion)
• You may also not use any trigonometric functions such as sin(), cos(), tan(), arcsin(), arccos(), arctan(), sinh(), cosh(), tanh(), etc... (Author's discretion)
• You may not use built-in or predefined values of Pi (or 3.14). No web requests.
• Your program cannot require input of any kind (besides running it).
• 3.14 is the only thing that you may output, it cannot be part of some longer/larger output..

Winner

The answer with the least amount of bytes on April 18th wins. Good luck!

Looking forward to clever answers from clever minds! Malbolge anyone? :P

• You may want to clarify that ^ is the exponentiation operator, not bitwise XOR operator. Apr 10 '14 at 16:05
• Are bitwise operators allowed? Apr 10 '14 at 16:07
• @ProgramFOX Well, they can be used as a replacement for normal operations, so, no. Not allowed. I'll edit the post. Apr 10 '14 at 16:09
• Web requests allowed? Apr 10 '14 at 16:35
• @swish: I wouldn't use web requests, because that belongs to the list of Standard "loopholes" which are no longer funny Apr 10 '14 at 16:38

PHP - 22 bytes

<?=date('n.j',8899e5);


Pi Day in year 1998 was really cool!

• @kukac67: n.j is the format, and 8899e5 is the Unix timestamp of March 14th in 1998. Apr 10 '14 at 17:43
• That is so clever. Apr 12 '14 at 14:00
• You can save 7 characters by removing ,8899e5. It will be correct once a year. Apr 15 '14 at 10:44
• @Timo You're right, but it contains the illegal digit "3" ;). Apr 16 '14 at 10:31
• @Blackhole 20 bytes: <?=date('w.W',78e5); April 1, 1970 was a Wednesday in week 14. Apr 17 '14 at 8:53

C, 39

Assumes the machine is little-endian and uses IEEE floating-point standard.

main(){printf("%.2f",*(float*)"P@I@");}


The fun fact is that "P@I@" is actually not related to PI but is equivalent to 0x40494050 which is the representation of 3.144550323486328

http://ideone.com/uXVEtl

• That's pretty awesome. Apr 13 '14 at 3:05
• Your code assumes type-punning on a little-endian machine. Apr 14 '14 at 19:31
• @NayukiMinase, yes you're right, it worth pointing it out. And it also assumes the use of IEEE floating-point standard. Apr 14 '14 at 22:03
• And it contains undefined behavior (implicit declaration of variadic function) Apr 15 '14 at 14:35
• This code actually assumes that the machine is Michael's machine :-). Apr 15 '14 at 15:52

dc, 11 9 bytes

zE[.]znPp

• z Push current stack depth (0) onto the stack
• E Push E16 (==1410)
• [.] Push the string "."
• z Push current stack depth (3) onto the stack
• n Pop 3 and print as number
• P Pop "." and print as char
• p Pop 14 and print as number with newline

Output:

$dc <<< zE[.]znPp 3.14$

• So "use input base" is an operator? It sort of smells like an arithmetic operation, but probably still valid. Apr 10 '14 at 20:18
• @Brilliand There's nothing I see in the question that bans base conversions. In fact the OP appears to be encouraging base conversions in a comment here codegolf.stackexchange.com/a/25681/11259 Apr 10 '14 at 20:27
• I do agree that it's valid; I'm just thinking that when the base conversion is an operator rather than part of the number syntax, it blurs the line between inline data and an arithmetic operation. Apr 10 '14 at 20:54
• I think this should be 15 bytes? Apr 11 '14 at 13:21
• @AdityaPatil Why? dc is a language and interpreter, just like all the other languages here. dc -e  is just the invocation of that interpreter. I could have just as easily saved the 9-byte program in a file and run dc pi.dc. If these bytes need to be counted, then the same would have to apply to all other languages. e.g. include python  in all python entries or ruby golfscript.rb  in all golfscript entries. I believe the agreed convention here is not to include these, unless special options need to be passed. Apr 11 '14 at 14:33

JavaScript (ES5) 20 bytes

A variation of m.buettner's JS solution.

(x='.'+0xE).length+x


Edit: JavaScript (ES5) 18 bytes

A variation of ComFreek's idea of using the base64 decode function.

atob('Mw')+'.'+0xE


Edit: Javascript (ES5) 16 bytes

If the unary + operator is allowed to cast a string to a number, it can be reduced to:

+atob('MDMuMTQ')

• WOW! That is really clever! Apr 10 '14 at 16:38
• @NateEldredge This is string concatenation, not arithmetic. Apr 10 '14 at 17:48
• is the use of + accepted here? Apr 12 '14 at 6:46
• I find the Javascript solution to be the most elegant and I was about to post mine until I noticed it had a '4' in it: atob('My4xNA=='), lol. Apr 15 '14 at 22:16
• @Dennis you're right, it's still there. It's even there if I copy+paste the result from console. I'm not happy with it so I'll revert to the 16 byte solution. Apr 16 '14 at 20:25

When is pi day in Australia?

p=(\(a:n:y:t:i:m:e:_)->[m,a,t,e])['.'..]


Edited to add: like the other Haskell answer, that outputs a string, so has quotes. You can score 33, and be strictly within the rules, but it's less fun:

p=putStr$map([','..]!!)[7,2,5,8]  • Haha, wow. This is good! – Ry- Apr 11 '14 at 14:17 • Can you explain why this works? Apr 16 '14 at 0:33 • I like this one a lot, +1. But I think technically ".." is invoking the increment operator, which was forbidden. (Actually upvoting also invokes an increment operation, so it should be forbidden as well;-) Apr 16 '14 at 12:35 • I actually did think about that - I had an even shorter answer: p=[2.9,2.92..]!!([0,6..]!!2)...and in this one .. does look wrong. But Haskell is strongly typed, and the increment in the answer I gave is of Char not Num and isn't arithmetic-the thing that's actually banned. I'd need a fromEnum to convert the chars back to numbers to use them that way. Anyway, my conscience is clear :) Apr 16 '14 at 14:10 • @yhager: ['.'..] is the char sequence "./0123456..." (etc). (a:n:y:t:i:m:e:_) is a pattern match pulling out the first 7 characters, [m,a,t,e] reorders 4 of them into "3.14". Knowing that this is what you want to do, it's a matter of searching the dictionary to find words with the right structure. Which I did...eg another pair was "ugliest suit". Then you come up with a joke :) Apr 16 '14 at 14:18 Morse Code, 33 Bytes morse -d ...-- .-.-.- .---- ....-  Technically speaking, the information only takes 21 bytes. • We definitely need more golf solutions in morse code here... Apr 11 '14 at 14:19 • Technically speaking, I'd say 21 bits, not bytes Apr 11 '14 at 19:17 • @Michael since Morse code can be either a dot, dash, or space, wouldn't it require more than 1 bit to store 1 operation? – smcg Apr 11 '14 at 19:42 • @smcg ButSpacesAreForWeakPeopleWhoCan'tReadThingsLikeThis Apr 13 '14 at 0:26 • As much as I like morse code (and hence this solution) - but isn't that against rule 3: "You may not use any predefined variables/constants that your language may have as a replacement for 3, 1, and 4." or even rule 2: "The digits 3, 1, and 4 may not appear anywhere in your code."? Apr 14 '14 at 16:17 J - 1511 10 char Made it shorter, using the fantastic i:. 6{i:6.28j8  Explained: • 6.28j8 - The complex number 6.28 + 8i. • i: - This is where the magic happens. i: on a complex number A+Bi, B≠0 takes B+1 equally spaced points from the interval [-A,A]. • 6{ - Take element at index 6, which just so happens to be 3.14. Previously, we had the following nonsense: |.'.'2}":22b8d5  Explained by explosion:  22b8d5 NB. 8d5 in base 22 = 4163 ": NB. convert to string '.'2} NB. change character at index 2 to '.' |. NB. reverse to make '3.14'  • Instead of |.'.'2}":22b8d5, you can save 2 characters by picking a number that doesn't require reversal: '.'1}":16bbc6 Apr 15 '14 at 2:16 • @epicwisdom The digits 3, 1, and 4 are banned. I have to use a number in the form 41X3 because I am only allowed to modify the characters at indices 0 and 2. Apr 15 '14 at 2:39 CJam - 8 6 S,'.E  CJam is a new language I am developing, similar to GolfScript - http://sf.net/p/cjam. Here is the explanation: S is a variable preinitialized to space (" ")  generates the string representation of the last value - which is exactly " " (3 characters) , calculates the string length (3) '. is the dot character E is a variable preinitialized to 14; 3, 1 and 4 are not allowed but it doesn't say anything about 14 :) Credits to Doorknob for the backtick idea • @Brilliand I would accept the use of E. Apr 10 '14 at 20:18 • @kukac67 While I haven't made an official release yet (coming today), I have already made the current CJam code available via hg. If you get the CJam code as of 1 day before you posted the question, it will run my program correctly (so I haven't made changes for solving this problem). But I won't really mind anyway. Apr 10 '14 at 20:23 • time to develop HQ9+π... Apr 11 '14 at 11:34 • @AnonymousPi Everybody is free to vote for the answers they choose. Maybe they're more impressed with other answers even if they're not as short. Apr 21 '14 at 0:53 • Why do you have a variable preinitialized to 14? May 20 '14 at 2:54 Linux command line, 35 bytes This one is nowhere near winning, but here it is for the fun of it: ping -w2 67502862|grep -oP '.\..\d'  Output (after 2 seconds): $ ping -w2 67502862|grep -oP '.\..\d'
3.14
$ • Quote from the question: "No web requests." – Doorknob Apr 11 '14 at 3:02 • @Doorknob ICMP echo request != web request. Regardless, the ping is to 4.6.3.14, from which I get no reply, so its not like I'm getting any useful information back from the internet Apr 11 '14 at 3:39 • @Doorknob But if this still bothers you, we can do this :;ping -t${#?} -qc2 67502862|grep -oP '.\..\d'. This sets the TTL to 1, so the outgoing ICMP echo is dropped by your router before it ever gets anywhere near the internet Apr 11 '14 at 4:22
• You can save 9 s by using ping -w2 67502862. (Ninja edit because before i was advising w1, almost broke the rules.) Apr 17 '14 at 13:10
• @lolesque Thanks. Doesn't improve the golf score though ;-) Apr 17 '14 at 14:22

GolfScript, 1813 12

'.'.,\.,n,


Old version:

'...','.'.,'....',


It's very artistic, only 3 unique characters! ;)

How it works:

# cmd   # stack
'...',  # 3
'.'.    # 3 '.' '.'
,       # 3 '.' 1
'....', # 3 '.' 1 4


Here's an explanation of the 12-char version (much more interesting, IMO):

'.' # '.'
., # '.' 3
\.  # 3 '.' '.'
,   # 3 '.' 1
n, # 3 '.' 1 4


It works because n, is 4, since n is '"\n"'. Same logic for '.',. It also swaps instead of creating a new string to save a char (thanks @algorithmshark).

Alternate version:

'pi ','.'.,'day!',


or

'Hi ','.'.,'PPCG',


Or anything in those strings, really. :P

• Only 3 different characters, cool :) Apr 10 '14 at 19:17
• Would '.'.,\.,n, work to save a char? Reusing the '.' by duplicating and swapping. Apr 10 '14 at 19:33
• @algorithmshark Yep, thanks!
– Doorknob
Apr 10 '14 at 23:52
• This is very clever! +1 Apr 14 '14 at 13:20
• this does look like the morse code version Apr 18 '14 at 7:13

Mathematica, 2120 18

This feels cheap...

Range[2,5,.06][[20]]


or

Range[2,5,.02][[58]]


Figured out how to make it 18:

Range[-.86,5][[5]]


(48 includes the new line character)

p=[-6..]
r=concat$show(p!!9):".":(show$p!!20):[]


Run it by calling the r function.

Using the method as with Mathematica above, you can type this into GHCi:

[-2.86..]!!6


Java, 87 86

class P{public static void main(String[]a){System.out.print("tri".length()+"."+0xe);}}


Scala, 27

Translated from Java as suggested by Score_Under. (I don't really know Scala)

print("tri".length+"."+0xe)

• Oh, 0xe is clever. Do you mind if I improve my JS answer with that? ;) Apr 10 '14 at 16:21
• Most java can be directly converted to scala, which is usually shorter if you really want to save on the bytes - if you're using it as a script, the entire thing is just: print("tri".length+"."+0xe) Apr 10 '14 at 20:25
• Yeah I thought of the same thing, which is shorter in Julia as [-.86:5][5] for 11, but figured it didn't count since ranges are just math.
– gggg
Apr 16 '14 at 23:53

Bash, 50

Help/error messages aren't external resources, they're a command's output!

If anything is rule abuse, this is it. It uses the coincidence that the help message given to STDERR by the exact command

ping6


is 314 characters long (at least on Ubuntu 13.10 with iputils-121221). EDIT: I appreciate that this is much less portable than I expected...

m=ping6|&wc -c
false
3.14
$ • only guaranteed to work this year It's fine.. :) Apr 10 '14 at 19:33 Bash, 17 bytes tr 0 .<<<$[62#MC]


Dash, 41 bytes

perl -pechomp,s/./$./<<$

..

.

• Bash would complain loudly about the missing ending marker while Dash doesn't, so a Bash solution without error messages would require two extra bytes. Apr 11 '14 at 0:21
• +1 as well. I guess @TheDoctor forgot to click the upvote button? Apr 11 '14 at 4:28
• @DigitalTrauma oops... fixed Apr 11 '14 at 14:11

Bash 23 22

seq 2 .02 5|sed -n 58p


Shell (without history expansion) 20

seq 2 .02 5|sed 58!d

• seq 2 .06 5|sed -n 20p also works as given in OP's Mathematica answer. Apr 11 '14 at 14:55
• sed 58!d is slightly shorter. Apr 11 '14 at 18:30
• @Dennis Weird, gnome-terminal substitutes !d with dpkg ... which was the last command that I ran that began with d. Works fine in ksh but zsh says zsh: event not found: d so that's definitely getting substituted for something. Apr 11 '14 at 18:44
• That's because of Bash's history expansion. It's disabled by default for scripts, so if you save the same command in pi.sh and execute bash pi.sh, it will work fine. Apr 11 '14 at 18:46

Python, 39 or 28 47 or 34

(Thank you Score_Under for golfing it down so much)

print"%s.%s%c"%(len('aaa'),len('a'),52)


+ here is the string concatenation operator, not addition.

Copying the 0xe trick from the OP, here's 28 chars with help from @psal

print('%X.%s'%(675,0xE))[2:]


Old versions:

print str(len('aaa'))+'.'+str(len('a'))+chr(52)

print str(len('aaa'))+'.'+str(0xe)

print"%s.%s"%(len('aaa'),0xe)

• The second one can be shortened by removing the space after the print and using a "%s" format instead of str(). c.f. print"%s.%s"%(len('aaa'),0xe) at 29 bytes. You can do the same to the first one too: print"%s.%s%c"%(len('aaa'),len('a'),52) at 39 bytes. Apr 10 '14 at 20:31
• @Score_Under Oh, I thought using %s still requires str. Thanks for the tip :) Apr 10 '14 at 20:50
• another 29 bytes solution : print('%s,%s'%(0xE9,0xE))[2:] Apr 17 '14 at 7:05
• ... and in 28 bytes : print('%X.%s'%(675,0xE))[2:] Apr 17 '14 at 7:28

GolfScript, 8 bytes

'??.'(\(


This program contains non-printable ASCII characters, so it cannot be directly pasted here. Instead, the question marks in the code above should be replaced with the bytes 03 and 0E in hexadecimal.

For convenience, here's a hex dump of the actual program. Unix / Linux users can run this hex dump through xxd -r to reconstruct the program:

0000000: 2703 0e2e 2728 5c28                      '...'(\(


Explanation:

• The first five bytes of the program form a single-quoted string literal encoding the three characters with the ASCII codes 3, 14 and 46 (= ASCII period).

• ( chops the first character off the string and pushes its ASCII code (3) on the stack. \ swaps the top two items on the stack, pulling the string back to the top, and the second ( chops another character off the string, again pushing its ASCII code (14) on the stack.

At the end of the program, the stack (excluding the empty input string) therefore looks like this:

3 "." 14


At the end of the program, the default behavior of the GolfScript interpreter is to stringify and print everything on the stack, yielding the desired output.

• Snap! I had exactly the same idea, just 12 hours too late. I guess I should read page 2 before posting... Oh well, rolling back, +1 to you. Apr 12 '14 at 4:14

bash -- 39

We all know that the first few digits of Pi can be computed from the following statement:

May I have a large container of coffee

We need only 2 decimal places, right?

m=May
i=I
h=have
3.14

C, 36

main(){printf("%d\b.%o\b",' ','');}

• If we combine it with my answer, we can save 2 bytes to get a score of 34 : main(){printf("%d\b.%d",' ',0xe);}
– vsz
Apr 11 '14 at 19:28

JavaScript - 23 bytes

atob('Mw')+atob('LjE0')


Thanks to nderscore (see his comments)!

• Nice :D I didn't even know about this Apr 10 '14 at 16:46
• It contains 4 though ;) Apr 10 '14 at 16:48
• @nderscore Ah! You're right! Would btoa() work to get 3.14 somehow? Apr 10 '14 at 16:52
• @kukac67 The base64 character set doesn't contain a period :( Apr 10 '14 at 16:54
• 23 byte "legal" version: atob('Mw')+atob('LjE0') and 14 byte "illegal" version: atob("My4xNA") Apr 10 '14 at 17:41

C#

 ((int)'ĺ').ToString("#\\.##");


ĺ is character 314, not a pre-defined variable or constant. the .ToString formats the output into a digit, a period, and then 2 more digits.

• Nice, but it currently doesn't output, and it's not compilable. Apr 16 '14 at 9:27
• Console.Write(((int)'ĺ').ToString("#\\.##")); Apr 16 '14 at 11:24

GolfScript, 13 10

{xy}.*'.'\


Thanks to Ilmari Karonen for the improved version!

x and y are non-printable characters (see http://www.asciitable.com/ for more info), respectively ETX and SO, which decode to 3 and 14.

What this code does should be pretty clear.

Note: The following rule

You may not use any predefined variables/constants that your language may have as a replacement for 3, 1, and 4. (Author's discretion)

is not broken, since ASCII characters are neither variables, nor constants.

• The digits 3, 1, and 4 may not appear anywhere in your code. , Your program cannot require input of any kind (besides running it). Apr 11 '14 at 10:41
• The digits do not appear anywhere. There's no input. Apr 11 '14 at 10:41
• And your final code is...? Apr 11 '14 at 10:42
• {}.*.{}.*, and inside the brackets there are 2 non-printable characters, respectively (asciitable.com) ETX and SO Apr 11 '14 at 10:43
• You could shorten this to {xy}.*'.'\  (10 chars). Mine's still shorter, though. ;-) But +1 for teaching me about { }.* (and { }.%), I'll have to keep that trick in mind. Apr 11 '14 at 17:51

New to the site and can't write comments yet, so I'll post it here. Just wondering about this Javascript solution:

'...'.length+'.'+0xe


and this rule:

3.14 is the only thing that you may output, it cannot be part of some longer/larger output..

If I run this code in my browser console, I get:

"3.14"


or using Node at the command line:

'3.14'


but if I run this code:

parseFloat('...'.length+'.'+0xe)


I get:

3.14


Using Node.js to run a .js file, I can write:

console.log('...'.length+'.'+0xe)


and get:

3.14

• There was never a limitation on what type the resulting value can be. So, string or number doesn't matter. The quotes around the string are not considered part of the output. Apr 14 '14 at 19:53
• Welcome to CodeGolf and Programming Puzzles @RobertMunn! This answer is valid as it obeys all the rules... Bear in mind that console tends to wrap strings in quotes when displaying them... You have used + but not arithmetically, it's being used as a concatenator, so that's valid... Also you could have made this smaller using alert rather than console.log... But otherwise, great effort. We hope you like it here... Apr 15 '14 at 2:58
• That's pretty nice! You should consider adding the name of the language and byte count (e.g. Javascript, 20) Apr 15 '14 at 7:32
• Thanks Ace, I'm just learning the rules here. I couldn't leave a comment so I posted separately, I guess this was more of a clarification on the rules kind of post, which nderscore and WallyWest clarified for me. I appreciate everyone's patience with a newb. Apr 15 '14 at 18:43

Mathematica, 56 bytes

(l=ToString@StringLength@#&)@"..."<>"."<>l@"."<>l@"...."


Not much to say. I'm generating the digits as string lengths and concatenate them.