27
\$\begingroup\$

To celebrate Rounded Pi Day, you must take advantage of today's date to make a program that takes in a circle's diameter and outputs its circumference by multiplying the diameter by 3.1416, where 3.1416 must be obtained using today's date.

Input 3 Output 9.4248 etc. 

Does not take the date as input. You do not have to use all components of the date but the digits of pi must come from formatting a Date object or using a date object to obtain it. The answers so far look good.

Not sure what else to specify. All the answers so far meet what I was expecting.

My constraint was that you must use components of the date to come up with Pi. You can of course use the components, multiply by 0 then add 3.1416, but that's boring and wastes precious chars!

Shortest code wins!

\$\endgroup\$
28
  • 62
    \$\begingroup\$ What's so special about 14/3/16? \$\endgroup\$ Commented Mar 14, 2016 at 13:01
  • 5
    \$\begingroup\$ define using today's date. I could get the date as a number, divide by itself and multiply by a predefined constant for pi \$\endgroup\$ Commented Mar 14, 2016 at 13:02
  • 7
    \$\begingroup\$ @Neil: Because 3/14/16 and 31/4/16 are not valid actual dates in D/M/Y format. There aren't 14 months, and April only has 30 days. I personally think we should wait until 6/28/32 (M/D/Y) or maybe 6/2/83 (D/M/Y), but that's a whole other holy war. \$\endgroup\$ Commented Mar 14, 2016 at 14:38
  • 8
    \$\begingroup\$ @DarrelHoffman You're a Tau man I see. \$\endgroup\$ Commented Mar 14, 2016 at 14:46
  • 17
    \$\begingroup\$ Sadly, its only PI day in the US cultural area. Europeans, with their silly lexigraphcially-sensible date ordering, don't get to have yearly PI days. Spare a sad thought for them, as you're eating your pi(e) today. \$\endgroup\$ Commented Mar 14, 2016 at 18:03

37 Answers 37

19
\$\begingroup\$

C, 32 bytes

#define f(d)d*time(0)/464083315. 

If losing a little more accuracy is fine, I can get it down to about 29 bytes being still accurate to 4 digits past the decimal (as of the time of this posting):

#define f(d)d*time(0)/46408e4 
\$\endgroup\$
7
  • \$\begingroup\$ #include<time.h>? \$\endgroup\$ Commented Mar 14, 2016 at 15:08
  • \$\begingroup\$ Also, where's the executable code? this is just something for the preprocessor to copy/paste; a snippet. We like runnable implementations here. \$\endgroup\$ Commented Mar 14, 2016 at 15:09
  • 3
    \$\begingroup\$ @tac most C compilers do not require you to #include standard library headers. Including them manually is almost always a good idea, but omitting them makes code golfing in C a lot easier. \$\endgroup\$ Commented Mar 14, 2016 at 15:21
  • 12
    \$\begingroup\$ @tac the answer behaves mostly the same as a function. Several answers on here are standalone functions as well. I don't see the point in providing a sample main function to demonstrate how to call this single argument macro. \$\endgroup\$ Commented Mar 14, 2016 at 15:25
  • 1
    \$\begingroup\$ @tac I think the standard [code-golf] rules is that unless forbidden functions are ok. \$\endgroup\$ Commented Mar 14, 2016 at 19:32
14
\$\begingroup\$

AppleScript, 122 120 bytes

set a to current date (display dialog""default answer"")'s text returned*(month of a+day of a*.01+year of a*1e-4 mod.01) 

Variable a

Variable a is a date object. I call all of my date information off of it.

Month, day, and year

The month, day, and year calls actually return an object that normally returns a string. To properly use it as a number, I have surrounded it on both sides with mathematical operations to automatically cast it to a number.

1e-4

1e-4 is a byte shorter than .0001.

mod.01

. acts as a separator to the AppleScript autocorrect. By using this, I can use modulo and still keep it a byte for myself.

No return statement/log

The last value calculated automatically is returned by the program. I output the number calculated via the return box.

And here's a gif of it running!

pi day gif

\$\endgroup\$
3
  • 8
    \$\begingroup\$ Forget Java, this has got to be the most verbose language \$\endgroup\$ Commented Mar 15, 2016 at 14:52
  • \$\begingroup\$ @Downgoat I guess that it's supposed to be "readable like English" or something... we use it a bit at one of my jobs and I really dislike whenever I have to make changes to such scripts. \$\endgroup\$ Commented Mar 15, 2016 at 15:49
  • \$\begingroup\$ @ChrisCirefice Yeah, no - this language has some annoying pieces to it. i.e. volume system volume. \$\endgroup\$ Commented Mar 15, 2016 at 16:00
13
\$\begingroup\$

Mathematica + coreutils, 20 bytes

<<"!date +%m.%d%y"#& 

Luckily, Mathematica interprets the output of an external command as an expression. In this case the output is a valid float, so that multiplication with the function argument # is implied.

\$\endgroup\$
1
  • \$\begingroup\$ That's a combination I didn't know existed. \$\endgroup\$ Commented Apr 5, 2016 at 7:07
12
\$\begingroup\$

Lua, 30 27 Bytes

print(...*os.date"%m.%d%y") 

Multiply the first command-line argument by the current date in format mm.ddyy, which is actually 03.1416.

\$\endgroup\$
9
\$\begingroup\$

Bash + bc, 25 20 bytes

date +%m.%d%y\*$1|bc 

Thanks to manatwork for saving five bytes.

Usage:

$ ./pi-round.sh 3 9.4248 
\$\endgroup\$
2
  • \$\begingroup\$ That here-string notation is so long: date +%m.%d%y\*$1|bc \$\endgroup\$ Commented Mar 14, 2016 at 14:28
  • \$\begingroup\$ @manatwork you are correct! I was playing around with the same myself but couldn't figure out how to combine it. Thanks! \$\endgroup\$ Commented Mar 14, 2016 at 14:31
8
\$\begingroup\$

05AB1E, 16 13 bytes

žfžežg¦¦J*4°/ 

Try it online.

Unfortunately a bug with floats makes this a byte longer :/

Thanks to Adnan for golfing off 3 bytes.

Explanation

žfžežg¦¦J*4°/ žfžežg push month day year ¦¦ slice off the first two chars from the year (2016 -> 16) J join them together into a number * multiply by input 4°/ divide by 1e4 
\$\endgroup\$
5
  • \$\begingroup\$ ¦¦ instead of 2000- is three bytes shorter ;) \$\endgroup\$ Commented Mar 14, 2016 at 16:11
  • \$\begingroup\$ In UTF-8, this totals 19 bytes. \$\endgroup\$ Commented Mar 16, 2016 at 21:21
  • 3
    \$\begingroup\$ @OldBunny2800 05AB1E uses CP1252 for encoding. \$\endgroup\$ Commented Mar 16, 2016 at 21:21
  • \$\begingroup\$ Bug with floats? \$\endgroup\$ Commented Apr 7, 2016 at 5:18
  • \$\begingroup\$ @CatsAreFluffy I should be able to do žf'.žežg¦¦J* (which is a byte shorter), but strings weren't auto-coercing to floats properly. \$\endgroup\$ Commented Apr 7, 2016 at 16:58
6
\$\begingroup\$

PowerShell v2+, 46 28 25 bytes

$args[0]*(Date -F "M.dy") 

Pretty straightforward. Takes input $args[0] and multiplies it by the date formatted as M.dy (the Get- is implied). Note that this may take a long time to run on v2 as it iterates possible substitutions for Date (e.g., checking your %PATH% environment variable, etc.) before settling on Get-Date.

\$\endgroup\$
3
  • \$\begingroup\$ Can you cut the space after -f? \$\endgroup\$ Commented Mar 15, 2016 at 4:02
  • 1
    \$\begingroup\$ @briantist Sadly, not here. We're abusing PowerShell's pattern recognition, since the actual flag is -Format. With flags like this, you just need to be unambiguous (for example, if you had -Debug and -Delimiter as potential options, you would need at least three letters to differentiate). Removing the space means PowerShell tries to parse the flag -F"M.dy" but can't find an argument that matches, and so throws an error. \$\endgroup\$ Commented Mar 15, 2016 at 12:23
  • \$\begingroup\$ ahhh I see it now. That's what I get for reading this on a train after a long day. I was interpreting it as the -f operator. Makes total sense now. \$\endgroup\$ Commented Mar 15, 2016 at 13:27
6
\$\begingroup\$

R 3.2.4, 55 51 47 bytes

edit I realized I could use scan thanks @FryAmTheEggMan. Reduced 4 bytes thanks to @MickyT.

scan()*as.numeric(format(Sys.Date(),'%m.%d%y')) 

First attempt at a golf. Happy pi day!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG :) I'm no R programmer, but you don't need to supply a named function, just an expression that evaluates to a function. So I'm pretty sure you can drop the f=. \$\endgroup\$ Commented Mar 14, 2016 at 16:11
  • 2
    \$\begingroup\$ You can go even further and do scan()*as.numeric(format(Sys.Date(),'%m.%d%y')) \$\endgroup\$ Commented Mar 14, 2016 at 16:17
  • 2
    \$\begingroup\$ as.double instead of as.numeric saves an additional byte \$\endgroup\$ Commented Mar 15, 2016 at 4:09
5
\$\begingroup\$

GNU Awk, 23 characters

$0*=strftime("%m.%d%y") 

Sample run:

bash-4.3$ awk '$0*=strftime("%m.%d%y")' <<< 3 9.4248 
\$\endgroup\$
5
\$\begingroup\$

Pyth, 19 bytes

*Qvs[.d4\..d5>2`.d3 

Try it here!
Only works at 2016-03-14 of course.

Explanation

 *Qvs[.d4\..d5>2`.d3 # Q = input .d4 # current month \. # dot between month and day .d5 # current day of the month >2`.d3 # last 2 digits of the year s[ # concat everything into a string v # eval -> convert to float *Q # multiply with input to get the circumference 
\$\endgroup\$
5
\$\begingroup\$

PHP, 45 26 25 24 bytes

Uses Windows-1252 encoding

<?=$argv[1]*date(~‘Ñ•†); 

Run like this:

echo '<?=$argv[1]*date(~‘Ñ•†);' | php -- 3 
  • Saved 19 bytes by using date() instead of DateTime::format
  • Saved a byte by using the echo tag
  • Saved a byte by using ISO-8859 encoding and negating the format string, so no need for double quotes. Might mess up your terminal a bit when running it from CLI, but works.
\$\endgroup\$
2
  • \$\begingroup\$ If you assume register_globals to true, you can use $n via /?n=3 in the url. \$\endgroup\$ Commented Mar 15, 2016 at 9:23
  • \$\begingroup\$ @Martijn That requires PHP 4.1 or setting it using the php.ini file. aross said before that wants to steer away from such method. \$\endgroup\$ Commented Mar 15, 2016 at 9:28
4
\$\begingroup\$

Python 2, 58 chars

import time print float(time.strftime("%m.%d%y"))*input() 

Try it!

Explanation:

import time # Import time time module print # Print the following float( # Cast to float time.strftime("%m.%d%y")) # Format the time as MM.DDYY *input() # Multiply by input 
\$\endgroup\$
4
\$\begingroup\$

JavaScript, 41 39 characters

This uses a proprietary Firefox-only method.

d=>new Date().toLocaleFormat`%m.%d%y`*d 

Thanks to:

  • Ismael Miguel for the template string suggestion (-2 characters).

Sample run (Firefox Web Console):

> (d=>new Date().toLocaleFormat`%m.%d%y`*d)(3) 9.4248 

Note: this will fail in Firebug Console. Seems that Firebug performs some expansion on the template string, transforming `%m.%d%y` into `%__fb_scopedVars(m).d%y` before passing it to the code.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ -2 bytes: d=>new Date().toLocaleFormat`%m.%d%y`*d \$\endgroup\$ Commented Mar 15, 2016 at 9:29
  • \$\begingroup\$ Oh. [facepalm] No idea why, I usually forget that trick. Although I use the template strings regularly. \$\endgroup\$ Commented Mar 15, 2016 at 9:33
  • \$\begingroup\$ I know. I really don't get either. What I'm trying to get is a way to remove that new from there. \$\endgroup\$ Commented Mar 15, 2016 at 9:35
  • \$\begingroup\$ I tried it on Firefox and it worked fine. I copy-pasted from the console. Since I've replaced ('...') with 2 backticks, that's 2 bytes. \$\endgroup\$ Commented Mar 15, 2016 at 9:41
3
\$\begingroup\$

Ruby, 40 bytes

->n{n*Time.new.strftime('%m.%d%y').to_f} 
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 54

.0001FromDigits[Now@{"Month","Day","YearShort"},100]#& 
\$\endgroup\$
2
\$\begingroup\$

Vitsy + coreutils, 19 bytes

'y%d%.m%+ etad',W*N 

Explanation:

'y%d%.m%+ etad',W*N 'y%d%.m%+ etad' Push 'date +%m.%d%y' to the stack. , Execute as shell. W Grab input from STDIN and eval it. * Multiply the top two items N Output as a number. 

Cannot be run in safe mode, as this uses the Runtime's exec method.

\$\endgroup\$
2
\$\begingroup\$

SpecBAS, 39 bytes

1 INPUT n: ?n*VAL DATE$(TIME,"mm.ddyy") 

Nothing out of the ordinary. Formats date as a string then multiplies the input by the VAL (value) of that string.

\$\endgroup\$
3
  • \$\begingroup\$ Interpreter / compiler link? \$\endgroup\$ Commented Mar 14, 2016 at 15:06
  • \$\begingroup\$ sites.google.com/site/pauldunn \$\endgroup\$ Commented Mar 14, 2016 at 16:06
  • \$\begingroup\$ I've edited the link into your answer \$\endgroup\$ Commented Mar 14, 2016 at 16:07
2
\$\begingroup\$

Oracle 11g, 50 49 bytes

SELECT &n*to_char(sysdate,'MM.DDYY')FROM dual; 

one less byte, thanks to @MickyT

\$\endgroup\$
1
  • \$\begingroup\$ you can drop the space before the FROM \$\endgroup\$ Commented Mar 14, 2016 at 18:05
2
\$\begingroup\$

Python 3, 74 54 bytes

using the time module instead of datetime like Loovjo's Answer

import time lambda n:n*float(time.strftime('%m.%d%y')) 

previous solution

from datetime import* lambda n:n*float(datetime.now().strftime('%m.%d%y')) 
\$\endgroup\$
1
  • 2
    \$\begingroup\$ @Zenadix The meta consensus is that unnamed functions are OK. \$\endgroup\$ Commented Mar 14, 2016 at 16:09
2
\$\begingroup\$

Google Sheets, 13 bytes

Bytes are calculated with one byte per character, zero bytes per cell except for the designated input cell, two bytes. The equals sign to start a formula is not counted. (I don't know if this is standard, please correct me if I am wrong.)

Run snippet to see the code.

table { empty-cells: show; } table, th, td { border: 1px solid black; }
<table border="1"> <tr> <th></th> <th>1</th> <th>2</th> <th>3</th> </tr> <tr> <td>A</td> <td>&lt;input></td> <td style="text-align:right">3.1416</td> <td style="font-family:'Ubuntu Mono',monospace">=<span style="color:rgb(257,142,29)">A1</span>*<span style="color:rgb(126,55,148)">A2</span> </td> </tr> </table>

This works because you can define your own date formats in Google Sheets. I made it so that it is <month>.<day><year>.

\$\endgroup\$
2
  • \$\begingroup\$ You might want to ask on meta about this scoring system. ;P \$\endgroup\$ Commented Mar 16, 2016 at 21:27
  • \$\begingroup\$ Will do when I have time (maybe tonight in Maryland) \$\endgroup\$ Commented Mar 16, 2016 at 21:29
2
\$\begingroup\$

Pyke, 16 15 bytes, ASCII encoding

C867wä%J"bT4^/* 

Explanation:

C867 - b,c,d = year, day, month wä - a = base_96("ä") - 100 % - a = a%b J" - a = "".join([a,c,d]) b - b = base(a, 10) T4^ - a = 10^4 / - a = a/b * - imp_print(a*eval_or_not(input)) 

or 11 bytes (non-competitive), (adds string singles, 2 digit year, string sumables)

C856\.R3sb* 

Explanation:

C856 - a,b,c = 2d_year, day, month \. - d = "." R3 - a,b,c = b,c,a s - a = sum(a,b,c) b - a = base(a, 10) * - imp_print(a*eval_or_not(input)) 
\$\endgroup\$
4
  • \$\begingroup\$ I'm guessing this is a different Pyke than pyke.sourceforge.net ? Can you post a link to the language spec? \$\endgroup\$ Commented Mar 15, 2016 at 4:08
  • \$\begingroup\$ github.com/muddyfish/PYKE/tree/master \$\endgroup\$ Commented Mar 15, 2016 at 7:19
  • \$\begingroup\$ Just wondering, what encoding does Pyke use? If it's UTF-8, this is 16 bytes. \$\endgroup\$ Commented Mar 16, 2016 at 21:30
  • 1
    \$\begingroup\$ @OldBunny2800 It doesn't matter; in this case it uses good old ascii as all code points are below 255. According to meta.codegolf.stackexchange.com/a/5879/32686, the answer can define it's own encoding at no cost \$\endgroup\$ Commented Mar 16, 2016 at 21:42
2
\$\begingroup\$

APL, 19 bytes

⎕×0.01⊥⌽⍎2⌽8↑2↓⍕⎕TS 

⎕TS is 2016 3 14 12 34 56 789 i.e. March 14rd, 2016 right before 12:35 pm
make into string, i.e. '2016 3 14 12 34 56 789'
8↑2↓ drop first two ('20') then take next eight ('16 3 14 ')
2⌽ rotate two characters, giving ' 3 14 16'
make into numbers (3 14 16)
reverse the list, giving 16 14 3
0.01⊥ evaluate in base ¹⁄₁₀₀, = 16 × 0.01² + 15 × 0.01¹ + 3 × 0.01⁰ = 0.0016 + 0.15 + 3 = 3.1416
⎕× multiply with input

or

⎕×1E¯4⊥⌽⍎2⌽7↑2↓⍕⎕TS 

⎕TS is 2016 3 14 12 34 56 789, i.e. March 14rd, 2016 right before 12:35 pm
make into string, i.e. '2016 3 14 12 34 56 789'
7↑2↓ drop first two ('20') then take next seven ('16 3 14')
2⌽ rotate two characters, giving ' 3 1416'
make into numbers (3 1416)
reverse the list, giving 1416 3
1E¯4⊥ evaluate in base ¹⁄₁₀₀₀₀, = 1416 × 0.01¹ + 3 × 0.01⁰ = 0.1416 + 3 = 3.1416
⎕× multiply with input

\$\endgroup\$
5
  • \$\begingroup\$ How does this work? \$\endgroup\$ Commented Mar 16, 2016 at 20:49
  • 1
    \$\begingroup\$ @OldBunny2800 Clear? \$\endgroup\$ Commented Mar 16, 2016 at 21:09
  • \$\begingroup\$ @OldBunny2800 meta.codegolf.stackexchange.com/questions/5878/… \$\endgroup\$ Commented Mar 16, 2016 at 21:21
  • \$\begingroup\$ What encoding is APL in? \$\endgroup\$ Commented Mar 16, 2016 at 21:24
  • 1
    \$\begingroup\$ @OldBunny2800 APL uses its own code page -- each of these characters are one byte. (This was before ASCII mind you...) \$\endgroup\$ Commented Mar 16, 2016 at 21:26
1
\$\begingroup\$

JavaScript ES6, 68 66 bytes

Saved 2 bytes thanks to dev-null!

x=>x*((a=new Date).getMonth()+1+"."+a.getDate()+(a.getYear()-100)) 

Anonymous function. Some uses:

f(1) == 3.1416 f(5) == 15.708 f(f(2)) == 19.73930112 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You should try using with \$\endgroup\$ Commented Mar 15, 2016 at 9:36
1
\$\begingroup\$

Tcl/Tk, 58 bytes

puts [expr {[gets stdin]*[clock f [clock se] -f %N.%d%g]}] 

(That's a complete program. If you want to cheat and remove the explicit puts statement then it's only 51 bytes -- you'd have to start tclsh and type/paste the following in directly, though:)

expr {[gets stdin]*[clock f [clock se] -f %N.%d%g]} 
\$\endgroup\$
1
\$\begingroup\$

Java 114 bytes

public double p(double d){ return (d*Double.parseDouble(new SimpleDateFormat("MM.ddyy").format(new Date()))); } 
\$\endgroup\$
1
  • \$\begingroup\$ 96 bytes: float p(float d){return d*Float.parseFloat(new SimpleDateFormat("MM.ddyy").format(new Date()));} \$\endgroup\$ Commented Mar 14, 2016 at 18:04
1
\$\begingroup\$

Racket, 112 characters

(define d(seconds->date(current-seconds)))(*(read)(+(date-month d)(*(date-day d).01)(*(-(date-year d)2e3)1e-4))) 

Reads the number from input in standard reader syntax.

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

TI-84 Basic, 30 bytes

Works on TI-83/84 calculators; E is the scientific notation token and ~ is the negative token.

Prompt D:getDate:D(Ans(2)+E~4Ans(1)-.2+.01Ans(3 

Test Case

D=?3 9.4248 
\$\endgroup\$
1
\$\begingroup\$

R 48 bytes

d*as.double(format(Sys.Date(),format="%m.%d%y")) 
\$\endgroup\$
1
\$\begingroup\$

MATL, 17 bytes

Z'2$'mm.ddyy'XOU* 

Try it online!

Z' % get current date and time as float 2$'mm.ddyy'XO % format as a string with custom format U % convert to number * % multiply by implicit input 
\$\endgroup\$
1
\$\begingroup\$

TI-BASIC, 16 13 9 bytes

Xround(π,1+min(getDate 

We round π to a number of decimal places equal to the minimum of {month,day,year}, and then multiply it by the input.

This is a function that takes input through X. Store it to one of the Y-variables, for example Y1, and then call like Y1([number]) on the homescreen.

\$\endgroup\$
4
  • \$\begingroup\$ There seems to be some disagreement that taking input via Ans is allowed. \$\endgroup\$ Commented Mar 15, 2016 at 15:55
  • \$\begingroup\$ How is this only 13 bytes? Not counting Input A, I count 22 characters. \$\endgroup\$ Commented Mar 16, 2016 at 21:15
  • 1
    \$\begingroup\$ @OldBunny2800 TI-BASIC is token based, each atom is represented as one or two bytes. \$\endgroup\$ Commented Mar 16, 2016 at 21:22
  • \$\begingroup\$ @OldBunny2800 The code page is here. \$\endgroup\$ Commented Mar 16, 2016 at 22:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.