# 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; the formula for which is n * tan(π / n). For those that don't know what the apothem is:

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 the n-gon as a floating point with no less than 8 decimal places.

### Test cases

3
5.1961524227

6
3.4641016151

10
3.2491969623

20
3.1676888065

99
3.1426476062

1697
3.1415962425

15000
3.1415926995


Note: The test cases above include 2 more digits than you are required to output.

# 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 – JungHwan Min Sep 5 '17 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) – ZaMoC Sep 5 '17 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... – Erik the Outgolfer Sep 4 '17 at 8:25
• yes, I know... @EriktheOutgolfer 3 byte built-ins in Pyth though >.< – Mr. Xcoder Sep 4 '17 at 8:25
• +1 for "Actually actually beats Jelly!!!" That pun never gets old. ;) – Kevin Cruijssen Sep 4 '17 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. :-) – Zach Gates Sep 4 '17 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. – Shaggy Sep 4 '17 at 13:20
• @Shaggy what do you mean? Since when? – shooqie Sep 4 '17 at 20:32
• See this Meta, Tux. – Shaggy Sep 4 '17 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. – Rui Barradas Sep 4 '17 at 19:21

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


# 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!

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

# Python 2, 45 bytes

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


Try it online!

• 38 bytes – Mr. Xcoder Sep 4 '17 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 – Simon Sep 4 '17 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. – Stephen Sep 4 '17 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)


# Perl, 14 + 16 = 30

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


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

# 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. – caird coinheringaahing Sep 5 '17 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 ;) – Ian H. Sep 6 '17 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