Calculate the area of a regular polygon

Given an integer, n, where 3 <= n < 2^32, calculate the area of a regular n-gon, with an apothem of 1, using the formula A = n * tan(π / n).

The apothem of a regular polygon is a line segment from the center to the midpoint of one of its sides.

Output the area of this regular polygon as a floating point value showing no fewer than eight decimal places (i.e. 1.24217000, 3.14159265).

Tests

In: 3
Out: 5.1961524227

In: 6
Out: 3.4641016151

In: 10
Out: 3.2491969623

In: 20
Out: 3.1676888065

In: 99
Out: 3.1426476062

In: 1697
Out: 3.1415962425

In: 15000
Out: 3.1415926995


Note: The output values in the sample cases above each show ten decimal places -- two more than required.

Mathematica, 16 bytes

N[Tan[Pi/#]#,9]&


Try it online!

of course mathematica has built-ins for this

Area@*RegularPolygon

• Area@RegularPolygon should be Area@*RegularPolygon; as it is now, it cannot be captured in a variable. That is, f = Area@RegularPolygon; f[3] does not work. Relevant meta discussion Sep 5, 2017 at 0:30
• @JungHwanMin ok, I fixed it.(although I didn't post it as an answer.I was just showing the built-ins for fun) Sep 5, 2017 at 8:10

Java (OpenJDK 9), 24 bytes

i->i*Math.tan(Math.PI/i)


Try it online!

Actually, 5 bytes

╦/Tß*


Try it online!

How?

╦/Tß*   Full program.

╦       Push Pi.
/      Divide ^ by the input.
T     Tangent.
ß*   Multiply by the input.
Output implicitly.


Alternative: ß╦/T*. o_O Actually actually beats Jelly!!!

• 2-byte builtin names... Sep 4, 2017 at 8:25
• yes, I know... @EriktheOutgolfer 3 byte built-ins in Pyth though >.< Sep 4, 2017 at 8:25
• +1 for "Actually actually beats Jelly!!!" That pun never gets old. ;) Sep 4, 2017 at 13:59

x87 Machine Code, 11 bytes

D9 EB
DA 31
D9 F2
DD D8
DA 09
C3


The above bytes of code define a function that calculates the area of a regular n-gon with an apothem of 1. It uses x87 FPU instructions (the classic floating-point unit on the x86 processors) to do this computation.

Following a standard x86 register-based calling convention (in this case, __fastcall), the function's argument is a pointer to the integer, passed in the ECX register. The function's result is a floating-point value, returned at the top of the x87 floating-point stack (register ST0).

Try it online!

Ungolfed assembly mnemonics:

D9 EB  fldpi                  ; load constant PI at top of FPU stack
DA 31  fidiv DWORD PTR [ecx]  ; divide PI by integer input (loaded from pointer
;   in ECX), leaving result at top of FPU stack
D9 F2  fptan                  ; compute tangent of value at top of FPU stack
DD D8  fstp  st0              ; pop junk value (FPTAN pushes 1.0 onto stack)
DA 09  fimul DWORD PTR [ecx]  ; multiply by integer input (again, loaded via ECX)
C3     ret                    ; return control to caller


As you can see, this is basically just a straightforward computation of the given formula,
result = n * tan(π / n)
Only a couple of interesting things bear pointing out:

• The x87 FPU has a dedicated instruction for loading the constant value PI (FLDPI). This was rarely used, even back in the day (and obviously much less so now), but it's shorter in size than embedding a constant into your binary and loading that.
• The x87 FPU instruction to calculate tangent, FPTAN, replaces the value of the input register (the top of the FPU stack) with the result, but also pushes a constant 1.0 onto the top of the FPU stack. This is done for backwards compatibility with the 8087 (I have no idea why this was done on the 8087; probably a bug). That means we need to pop this unneeded value off of the stack. The fastest and shortest way to do that is a simple FSTP st0, like we use here. We could have also done a multiply-and-pop, since multiplying by 1.0 won't change the result, but this is also 2 bytes (so no win in code size), will probably execute more slowly, and may introduce unnecessary indeterminacy into the result.

Although a modern programmer or compiler would use the SSE (and later) instruction set, rather than the aging x87, this would require more code to implement, as there's no single instruction to compute a tangent in these newer ISAs.

Jelly, 6 bytes

ØP÷ÆT×


Try it online!

Jelly's π builtin has >8 decimal places.

• Nice. I was trying to figure this out (and Jelly in its entirety), right now. :-) Sep 4, 2017 at 8:12

Brachylog, 9 bytes

;π/₍*₄;?×


Try it online!

Sakura, 4 bytes

*ĳ/π


This is expanded to *ĳ/π⓪⓪, which is

*              *
ĳ     tan(   )
/         /
π       π
⓪        n
⓪          n

• Non-competing isn't a thing any more. Sep 4, 2017 at 13:20
• @Shaggy what do you mean? Since when? Sep 4, 2017 at 20:32
• See this Meta, Tux. Sep 4, 2017 at 20:36

R, 25 bytes

cat((n=scan())*tan(pi/n))


Input from stdin, output to stdout.

Try it online!

• Works without cat(). 5 bytes less. Sep 4, 2017 at 19:21

JavaScript (ES6), 24 bytes

x=>x*Math.tan(Math.PI/x)


Try it

o.innerText=(f=
x=>x*Math.tan(Math.PI/x)
)(+i.value);oninput=_=>o.innerText=f(+i.value)
<input id=i min=3 type=number value=3><pre id=o>

MATL, 7 bytes

YPy/Z,*


Try it online!

Japt, 7 bytes

*MtMP/U


Test it

Explanation

Just implements the forumla, where Mt is tan, MP is pi and U is the input.

Ohm v2, 7 bytes

απ/ÆT³*


Try it online!

How?

απ/ÆT³*   Full Program

απ        Push Pi.
/       Divided by the input.
ÆT     Tangent.
³*   Multiplied by the input.
Implicitly output.


Perl, 14 + 16 = 30

perl -MMath::Trig -ple'$_*=tan(pi/$_)'


14 bytes for the program proper, and 16 for the command line switches

var'aq, 51 bytes

'Ij latlh HeHmI' tam boqHa''egh qojmI' boq'egh cha'


Explanation

'Ij        - read from STDIN
latlh      - duplicate top of stack
HeHmI'     - push PI onto stack
tam        - swap first 2 elements on stack
boqHa''egh - divide
qojmI'     - take tangent
boq'egh    - multiply
cha'       - print


Common Lisp, 29 bytes

(lambda(n)(* n(tan(/ pi n))))


Try it online!

Python 2, 45 bytes

from math import*
n=input()
print n*tan(pi/n)


Try it online!

• 38 bytes Sep 4, 2017 at 8:08
• In challanges like this I really dislike using lambda to save bytes by just putting the print in the footer... Therefore: 44 bytes Sep 4, 2017 at 9:48
• @Simon why? Functions are a valid submission - you can either output a value, or return it from a function. The print in the footer isn't required. Sep 4, 2017 at 13:48

Pyth, 9 bytes

*.tc.n0Q2


Test suite.

How?

*.tc.n0Q2    Full program. Q means input.

.n0      Pi.
c         Divided by:
Q     The input.
.t     2    Tangent.
*        Q   Multiply by the input.
Output implicitly.


Gaia, 5 bytes

₵P÷ṫ×


Try it online!

How?

₵P÷ṫ×   Full program.

₵P      Push Pi.
÷     Divided by the input.
ṫ    Tangent.
×   Multiply by the input.


Swift, 35 bytes

With compiler warnings:

import Foundation
{tan(M_PI/$0)*$0}


Try it here!

Without compiler warnings, 40 bytes:

import Foundation
{tan(Double.pi/$0)*$0}


Excel, 16 bytes

=A1*TAN(PI()/A1)


Prolog (SWI), 25 bytes

f(X,Y):-X is Y*tan(pi/Y).


Try it online!

This is my first submission to codegolf, hope I'm doing alright. Written as a function.

IBM/Lotus Notes Formula Language, 13 bytes

a*@Tan(@Pi/a)


Input taken via a field named a on the same form as the field containing the formula. No TIO available so screenshot of all test cases shown below:

PowerShell, 38 bytes

param($n)$n*[math]::tan([math]::pi/\$n)


Try it online!

Does exactly what it says on the tin, just takes a little longer due to the lengthy [math]:: .NET calls.

Ruby, 27 bytes

->n{n*Math.tan(Math::PI/n)}


Try it online!

Pari/GP, 14 bytes

n->tan(Pi/n)*n


Try it online!

C# (Mono C# compiler), 24 bytes


=>n*Math.Tan(Math.PI/n)


Try it online!

• Unfortunately, this is neither a complete program, not a function. Instead it is a snippet, which isn't allowed here. However, I think that you can add n=> to the start to make this into an arrow function (take this with a pinch of salt, I don't know C#) which is valid. Sep 5, 2017 at 18:33
• You can place the snippet into a System.Func<T, T>, which would take a float as input and another one as output. The declaration would look like this: System.Func<float, float> f = n=>n*Math.Tan(Math.PI/n);, where the bytecount would start at n=>. In my example i omitted two of your brackets to save 2 bytes ;) Sep 6, 2017 at 12:12

RPNGolf 0.6/0.7, 12 bytes

tbp-1mBsdmcc


My first post using RPNGolf, my new stack-based language!

This is a full program that reads an integer from standard input and prints the output to standard output (without a trailing newline).

Explanation:

tb              # push user input from STDIN as int
p             # duplicate top of stack
-1           # push -1
mB         # pop i, push inverse cosine of i
s        # swap top two items on the stack
d       # pop b, pop a, push a/b
mc     # pop i, push tangent of i
c    # pop b, pop a, push a*b
# RPNGolf implicity prints the stack upon normal exit


Factor, 19 bytes

[ pi over / tan * ]


Try it online!

05AB1E, 7 bytes

žqI/Å¼*


Explanation:

žq       # Push PI (3.141592653589793)
I/     # Divide it by the input
Å¼   # Get the tangent of this
*  # Multiply it by the (implicit) input
# (after which the result is output implicitly)


Pyt, 6 bytes

Đπ/⅟ť*


Try it online!

Đ            implicit input (n); duplicate on stack
π           push pi
/          divide n by pi
⅟         take the reciprocal
ť        tan(pi/n)
*       multiply by n; implicit print