56
\$\begingroup\$

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

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

63 Answers 63

113
\$\begingroup\$

PHP - 22 bytes

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

Pi Day in year 1998 was really cool!

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

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

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

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
$ 
\$\endgroup\$
5
  • \$\begingroup\$ So "use input base" is an operator? It sort of smells like an arithmetic operation, but probably still valid. \$\endgroup\$
    – Brilliand
    Apr 10, 2014 at 20:18
  • \$\begingroup\$ @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 \$\endgroup\$ Apr 10, 2014 at 20:27
  • \$\begingroup\$ 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. \$\endgroup\$
    – Brilliand
    Apr 10, 2014 at 20:54
  • \$\begingroup\$ I think this should be 15 bytes? \$\endgroup\$ Apr 11, 2014 at 13:21
  • 9
    \$\begingroup\$ @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. \$\endgroup\$ Apr 11, 2014 at 14:33
42
\$\begingroup\$

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')
\$\endgroup\$
16
  • \$\begingroup\$ WOW! That is really clever! \$\endgroup\$
    – kukac67
    Apr 10, 2014 at 16:38
  • 9
    \$\begingroup\$ @NateEldredge This is string concatenation, not arithmetic. \$\endgroup\$
    – tomsmeding
    Apr 10, 2014 at 17:48
  • \$\begingroup\$ is the use of + accepted here? \$\endgroup\$
    – phuclv
    Apr 12, 2014 at 6:46
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Matt Borja
    Apr 15, 2014 at 22:16
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – nderscore
    Apr 16, 2014 at 20:25
39
\$\begingroup\$

Haskell, 41 bytes

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]
\$\endgroup\$
6
  • \$\begingroup\$ Haha, wow. This is good! \$\endgroup\$
    – Ry-
    Apr 11, 2014 at 14:17
  • \$\begingroup\$ Can you explain why this works? \$\endgroup\$
    – yhager
    Apr 16, 2014 at 0:33
  • 5
    \$\begingroup\$ 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;-) \$\endgroup\$ Apr 16, 2014 at 12:35
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – bazzargh
    Apr 16, 2014 at 14:10
  • 6
    \$\begingroup\$ @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 :) \$\endgroup\$
    – bazzargh
    Apr 16, 2014 at 14:18
32
\$\begingroup\$

Morse Code, 33 Bytes

morse -d ...-- .-.-.- .---- ....-

Technically speaking, the information only takes 21 bytes.

\$\endgroup\$
17
  • 11
    \$\begingroup\$ We definitely need more golf solutions in morse code here... \$\endgroup\$ Apr 11, 2014 at 14:19
  • 26
    \$\begingroup\$ Technically speaking, I'd say 21 bits, not bytes \$\endgroup\$
    – Michael M.
    Apr 11, 2014 at 19:17
  • 14
    \$\begingroup\$ @Michael since Morse code can be either a dot, dash, or space, wouldn't it require more than 1 bit to store 1 operation? \$\endgroup\$
    – smcg
    Apr 11, 2014 at 19:42
  • 5
    \$\begingroup\$ @smcg ButSpacesAreForWeakPeopleWhoCan'tReadThingsLikeThis \$\endgroup\$
    – Cole Tobin
    Apr 13, 2014 at 0:26
  • 8
    \$\begingroup\$ 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."? \$\endgroup\$
    – kratenko
    Apr 14, 2014 at 16:17
27
\$\begingroup\$

J - 15 11 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'
\$\endgroup\$
2
  • \$\begingroup\$ Instead of |.'.'2}":22b8d5, you can save 2 characters by picking a number that doesn't require reversal: '.'1}":16bbc6 \$\endgroup\$
    – rationalis
    Apr 15, 2014 at 2:16
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ Apr 15, 2014 at 2:39
25
\$\begingroup\$

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

\$\endgroup\$
11
  • 1
    \$\begingroup\$ @Brilliand I would accept the use of E. \$\endgroup\$
    – kukac67
    Apr 10, 2014 at 20:18
  • 6
    \$\begingroup\$ @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. \$\endgroup\$ Apr 10, 2014 at 20:23
  • 33
    \$\begingroup\$ time to develop HQ9+π... \$\endgroup\$
    – Nzall
    Apr 11, 2014 at 11:34
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ Apr 21, 2014 at 0:53
  • 2
    \$\begingroup\$ Why do you have a variable preinitialized to 14? \$\endgroup\$ May 20, 2014 at 2:54
20
\$\begingroup\$

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
$
\$\endgroup\$
5
  • \$\begingroup\$ Quote from the question: "No web requests." \$\endgroup\$
    – Doorknob
    Apr 11, 2014 at 3:02
  • 9
    \$\begingroup\$ @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 \$\endgroup\$ Apr 11, 2014 at 3:39
  • 10
    \$\begingroup\$ @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 \$\endgroup\$ Apr 11, 2014 at 4:22
  • 1
    \$\begingroup\$ You can save 9 s by using ping -w2 67502862. (Ninja edit because before i was advising w1, almost broke the rules.) \$\endgroup\$
    – lolesque
    Apr 17, 2014 at 13:10
  • \$\begingroup\$ @lolesque Thanks. Doesn't improve the golf score though ;-) \$\endgroup\$ Apr 17, 2014 at 14:22
16
\$\begingroup\$

GolfScript, 18 13 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

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

OP's answer.

Mathematica, 21 20 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]]

Haskell, 49 48 12

(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)
\$\endgroup\$
3
  • \$\begingroup\$ Oh, 0xe is clever. Do you mind if I improve my JS answer with that? ;) \$\endgroup\$ Apr 10, 2014 at 16:21
  • \$\begingroup\$ 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) \$\endgroup\$ Apr 10, 2014 at 20:25
  • \$\begingroup\$ 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. \$\endgroup\$
    – gggg
    Apr 16, 2014 at 23:53
12
\$\begingroup\$

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
o=$?
echo ${m:0:o}.${m:o:2}

The first line takes the character length of the help message and stores it in a variable.

As in my other answers, the next few lines obtain the number 1 by taking the exit status of false (not very portable but this is after all!), then use that value to insert a decimal point in the last line

Output: 3.14

\$\endgroup\$
7
  • \$\begingroup\$ you sir are genius \$\endgroup\$
    – Joshua
    Apr 11, 2014 at 12:52
  • 1
    \$\begingroup\$ Uh oh, "2.81" on Ubuntu 12.04 (iputils-101006). +1 all the same ;-) \$\endgroup\$ Apr 12, 2014 at 1:29
  • \$\begingroup\$ @DigitalTrauma according to info ping6 I have iputils-121221 \$\endgroup\$
    – user16402
    Apr 13, 2014 at 6:26
  • \$\begingroup\$ also check out my chmod-abusing answer to this question at codegolf.stackexchange.com/a/25724/16402 \$\endgroup\$
    – user16402
    Apr 14, 2014 at 16:48
  • 2
    \$\begingroup\$ even shorter: ping6|&wc -c|sed 's/./&./' \$\endgroup\$
    – malkaroee
    Oct 19, 2014 at 10:01
11
\$\begingroup\$

Bash+TeX 28 bytes

If you have TeX installed:

tex -v|head -c8|cut -d\  -f2

The version number of TeX asymptotically approaches pi as the software improves. So, as time goes on, you can use this (nonconforming) implementation to print more and more digits!

tex -v|cut -d\  -f2 | head -n1

This printed 3.1415926 back in 2011; as of January 2014, it will print 3.14159265.

\$\endgroup\$
2
  • \$\begingroup\$ I love this one! \$\endgroup\$ Apr 19, 2014 at 15:09
  • \$\begingroup\$ You can use tex -v|grep -oP '.\..\d' to save 4 bytes. (Regex stolen from DigitalTrauma's answer.) Anyway, +1 because I just thought of the same answer. \$\endgroup\$
    – user12205
    Apr 19, 2014 at 22:26
10
\$\begingroup\$

JavaScript, 20 bytes

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

Use it in a console, so that the result is directly printed. Otherwise, alert(...) will add another 7 characters.

Same as my other answer, I just noticed it's a lot shorter in JS.

The idea of using 0xe for 14 is taken from the OP's submission with the OP's kind permission.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Its fine, you can use it :) \$\endgroup\$
    – kukac67
    Apr 10, 2014 at 16:30
  • 1
    \$\begingroup\$ Alternative 20 byte solution: (x='.'+0xE).length+x \$\endgroup\$
    – nderscore
    Apr 10, 2014 at 16:35
  • \$\begingroup\$ @nderscore that's a pretty neat idea. If you post that as another answer, you'll get my upovote ;) \$\endgroup\$ Apr 10, 2014 at 16:36
10
\$\begingroup\$

GNU date, 17 bytes

date -d2/5 +%u.%y

This one only guaranteed to work this year, but I don't see any limitation in the rules about this ;-)

The + is simply indicates the start of the format specifier for date and is not an arithmetic addition. Same with the -, indicating input date.

Output:

$ date -d2/5 +%u.%y
3.14
$
\$\endgroup\$
1
  • \$\begingroup\$ only guaranteed to work this year It's fine.. :) \$\endgroup\$
    – kukac67
    Apr 10, 2014 at 19:33
10
\$\begingroup\$

Bash, 17 bytes

tr 0 .<<<$[62#MC]

Dash, 41 bytes

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


..










.
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Dennis
    Apr 11, 2014 at 0:21
  • \$\begingroup\$ +1 as well. I guess @TheDoctor forgot to click the upvote button? \$\endgroup\$ Apr 11, 2014 at 4:28
  • \$\begingroup\$ @DigitalTrauma oops... fixed \$\endgroup\$
    – TheDoctor
    Apr 11, 2014 at 14:11
7
\$\begingroup\$

Bash 23 22

seq 2 .02 5|sed -n 58p

Shell (without history expansion) 20

seq 2 .02 5|sed 58!d
\$\endgroup\$
5
  • \$\begingroup\$ seq 2 .06 5|sed -n 20p also works as given in OP's Mathematica answer. \$\endgroup\$
    – user80551
    Apr 11, 2014 at 14:55
  • \$\begingroup\$ sed 58!d is slightly shorter. \$\endgroup\$
    – Dennis
    Apr 11, 2014 at 18:30
  • \$\begingroup\$ @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. \$\endgroup\$
    – user80551
    Apr 11, 2014 at 18:44
  • \$\begingroup\$ 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. \$\endgroup\$
    – Dennis
    Apr 11, 2014 at 18:46
  • \$\begingroup\$ @user80551 Thanks for the tip on Aviation about the Chat GPT post - both of that user's "contributions" pegged the detector, so we've addressed that. Much appreciate the good catch there. Cheers! \$\endgroup\$
    – Ralph J
    Jan 4, 2023 at 3:54
6
\$\begingroup\$

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)
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Apr 10, 2014 at 20:31
  • \$\begingroup\$ @Score_Under Oh, I thought using %s still requires str. Thanks for the tip :) \$\endgroup\$
    – user12205
    Apr 10, 2014 at 20:50
  • 1
    \$\begingroup\$ another 29 bytes solution : print('%s,%s'%(0xE9,0xE))[2:] \$\endgroup\$
    – onionpsy
    Apr 17, 2014 at 7:05
  • 1
    \$\begingroup\$ ... and in 28 bytes : print('%X.%s'%(675,0xE))[2:] \$\endgroup\$
    – onionpsy
    Apr 17, 2014 at 7:28
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Dennis
    Apr 12, 2014 at 4:14
5
\$\begingroup\$

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
echo ${#m}.${#i}${#h}
\$\endgroup\$
4
\$\begingroup\$

Mathematica - 78

DateList["March"<>ToString@FromDigits@"E"]~DateString~{"MonthShort",".","Day"}
\$\endgroup\$
1
  • \$\begingroup\$ In the spirit of Pi Day! :D \$\endgroup\$
    – kukac67
    Apr 10, 2014 at 17:01
4
\$\begingroup\$

Perl, 15 20 bytes

print 05706=~s/0/./r
print 0xbc6=~s/0/./r
\$\endgroup\$
8
  • \$\begingroup\$ Isn't PI__ something of a constant related to Pi? \$\endgroup\$ Apr 10, 2014 at 16:51
  • \$\begingroup\$ It's not a constant. It's two strings being XOR-ed. \$\endgroup\$
    – nderscore
    Apr 10, 2014 at 16:55
  • \$\begingroup\$ Bitwise operators aren't allowed. \$\endgroup\$
    – kukac67
    Apr 10, 2014 at 16:56
  • \$\begingroup\$ Oh, they're not. Well, there goes my hypothetical solution. That said, if bitwise operators aren't allowed, is there even much else to do than count lengths of strings? \$\endgroup\$ Apr 10, 2014 at 16:57
  • \$\begingroup\$ There are several answers that use numbers in different bases. \$\endgroup\$
    – kukac67
    Apr 10, 2014 at 16:58
4
\$\begingroup\$

tr + Bash, 17 bytes

tr a-j 0-9<<<d.be

The - chars here are not arithmetic subtraction operators. They indicate range.

Output:

$ tr a-j 0-9<<<d.be
3.14
$
\$\endgroup\$
3
  • \$\begingroup\$ Clever, although a character translation is really hidden arithmetics. \$\endgroup\$ Apr 11, 2014 at 12:41
  • \$\begingroup\$ @ArlaudPierre True, though I would contend that pretty much all solutions here have hidden arithmetic one way or another. If you look at the actual machine instructions executed for any of these answers, I would think all have some bitwise or arithmetic instructions, which are banned from explicit use by the question. \$\endgroup\$ Apr 11, 2014 at 17:09
  • \$\begingroup\$ @ArlaudPierre The question requires the string "3.14" to be output. Regardless of the language, somewhere under the hood, this will degenerate to a write() call or similar. There will necessarily be some arithmetic under the covers to ensure the correct number of bytes is written, if nowhere else. I think the point of the question is to hide the necessary arithmetic/bitwise computations. \$\endgroup\$ Apr 11, 2014 at 17:14
4
\$\begingroup\$

BASH, 57

This is longer than most answers, but there is nothing bad in trying.

f=`echo aaaaaaaaaaaaa|wc -c`;e=`echo aa|wc -c`;echo $e.$f

gives

3.14
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Sorry for re-golfing everyone's answers :P, In the same vein as this I found: c=wc\ -c;echo `$c<<<..`.`$c<<<''``$c<<<...` \$\endgroup\$ Apr 12, 2014 at 0:07
4
\$\begingroup\$

C, 36

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

JavaScript - 23 bytes

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

Thanks to nderscore (see his comments)!

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

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.

\$\endgroup\$
2
  • \$\begingroup\$ Nice, but it currently doesn't output, and it's not compilable. \$\endgroup\$ Apr 16, 2014 at 9:27
  • \$\begingroup\$ Console.Write(((int)'ĺ').ToString("#\\.##")); \$\endgroup\$
    – Grax32
    Apr 16, 2014 at 11:24
3
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ The digits 3, 1, and 4 may not appear anywhere in your code. , Your program cannot require input of any kind (besides running it). \$\endgroup\$
    – user80551
    Apr 11, 2014 at 10:41
  • \$\begingroup\$ The digits do not appear anywhere. There's no input. \$\endgroup\$
    – Vereos
    Apr 11, 2014 at 10:41
  • \$\begingroup\$ And your final code is...? \$\endgroup\$
    – user80551
    Apr 11, 2014 at 10:42
  • \$\begingroup\$ {}.*.{}.*, and inside the brackets there are 2 non-printable characters, respectively (asciitable.com) ETX and SO \$\endgroup\$
    – Vereos
    Apr 11, 2014 at 10:43
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Apr 11, 2014 at 17:51
3
\$\begingroup\$

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
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – nderscore
    Apr 14, 2014 at 19:53
  • \$\begingroup\$ 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... \$\endgroup\$ Apr 15, 2014 at 2:58
  • \$\begingroup\$ That's pretty nice! You should consider adding the name of the language and byte count (e.g. Javascript, 20) \$\endgroup\$
    – Vereos
    Apr 15, 2014 at 7:32
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Apr 15, 2014 at 18:43
2
\$\begingroup\$

Mathematica, 56 bytes

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

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

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.