# Calculate the internal angles of a regular polygon with N sides

This is a simple challenge: given n in any way practical (Function arg, stdin, file, constant in code) output the internal angles (NOT the sum of the internal angles, just one) of a regular polygon with n sides. The output can be in degrees, gradians or radians, and can be displayed in any way practical (Function return, stdout, stderr if you feel like it, file etc)

As usual, smallest byte count wins.

• Degrees, radians, grads, or user discretion? – Jeff Zeitlin Oct 22 '19 at 15:12
• @JeffZeitlin user discretion, will edit to clarify – Jachdich Oct 22 '19 at 15:13
• Do you mean the sum of the internal angles or the value of one of the angles? – HiddenBabel Oct 22 '19 at 15:16
• Is there a higher/lower bound on what we have to support? i've got an idea for an answer with a microcontroller language but it can only really support upto 255 – Alex Robinson Oct 24 '19 at 9:39
• @AlexRobinson yeah that's fine, just specify in your answer. – Jachdich Oct 24 '19 at 10:00

# Perl 6, 8 bytes

π- τ/*


Try it online!

Output in radians. Simple function in WhateverCode notation which computes $$\π-τ/n\$$. $$\τ\$$ is the constant tau equaling $$\2π\$$.

# Python 3, 18 bytes

lambda s:180-360/s


An unnamed function which returns a floating point number of degrees. (For gradians swap 180 for 200 and 360 for 400.)

Try it online!

# JavaScript, 12 bytes

n=>180-360/n


Try it Online!

# Shakespeare Programming Language, 242226 203 bytes

Try it online!

N.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]
Ajax:Listen tothy.
You is the quotient betweenthe product ofthe sum ofyou a big pig twice the square oftwice the sum ofa big big cat a cat you.
Open heart


Explanation: I use the formula ((n-2)200)/n. Input in STDIN. Much of this program is the number 200, which I represent as 2*2*2*(1+2*2*2*(2+1)). Saved 16 bytes by switching to gradians, since 180 is harder to represent than 200. Saved 23 bytes by instead representing 200 as 2*(2*(4+1))^2.

# MathGolf, 65 4 bytes

⌡π*╠


-1 byte thanks to @someone nu outputting in gradians instead of degrees.

Try it online.

Outputs in radians by using the formula: $$\A(n) = \frac{(n−2)×\pi}{n}\$$.

Explanation:

⌡     # Decrease the (implicit) float input by 2
π*   # Multiply it by PI
╠  # Then divide it by the (implicit) input (b/a builtin)
# (after which the entire stack joined together is output implicitly as result)

• I think you can use 200 instead of 180 to output in gradians instead of degrees; that should be possible to encode in 2 or 1 bytes. – my pronoun is monicareinstate Oct 22 '19 at 15:39
• @someone Thanks. But I just realized that simply outputting in radians is even shorter than gradians or degrees. – Kevin Cruijssen Oct 22 '19 at 15:45

# 05AB1E, 6 bytes

ÍƵΔ*I/


Try it online or verify some more test cases (output in degrees).

Explanation:

Uses the formula $$\A(n) = \frac{(n-2)×X}{n}\$$ where $$\n\$$ is the amount of sides, and $$\A(n)\$$ is the interior angle of each corner, and $$\X\$$ is a variable depending on whether we want to output in degrees ($$\180\$$), radians ($$\\pi\$$), or gradians ($$\200\$$).

Í       # Decrease the (implicit) input by 2
ƵΔ*    # Multiply it by the compressed integer 180 (degrees output)
žq*    # Multiply it by the builtin PI (radians output)
т·*    # Multiply it by 100 doubled to 200 (gradians output)
I/  # Divide it by the input
# (after which the result is output implicitly)


See this 05AB1E tip of mine (section How to compress large integers?) to understand why ƵΔ is 180.

# WDC 65816 machine code, 20 bytes

Hexdump:

00000000: a2ff ffa9 6801 e838 e500 b0fa 8600 a9b5  ....h..8........
00000010: 00e5 0060


Assembly:

    ; do 360/n (using repeated subtraction... it'll go for at most 120 loops anyways, with sane inputs)
LDX #$FFFF LDA.w #360 loop: INX SEC SBC$00
BCS loop
; quotinent in X now. do 180-X
STX $00 LDA.w #181 ; carry is clear here, so compensate by incrementing accumulator SBC$00
RTS


Input in $00, output in A. Overwrites$00 and X. 16-bit A/X/Y on entry (REP #$30). Apparently I'm the only one using $$\ 180 - \frac{360}{n} \$$ instead of the more conventional formula. Note that this code rounds the division downwards, and thus rounds the result upwards. # Japt, 8 7 bytes Í*-#´/U  Try it Í*-#´/U :Implicit input of integer U Í :Subtract from 2 * :Multiply by -#´ :-180 /U :Divided by U  Taking a page out of Kevin's book, see this Japt tip to find out why #´ = 180. # APL (Dyalog Unicode), 6 bytes ○1-2÷⊢  Try it online! The result is in radians. It implements pi * (1 - 2 / x). The big circle is the "pi times" function. # R, 2120 14 bytes -7 thanks to Robin Ryder. Outputs in radians pi-2*pi/scan()  Try it online! • 20 bytes – Robin Ryder Oct 22 '19 at 17:10 • You can make it 14 bytes with scan(). – Robin Ryder Oct 23 '19 at 19:23 • @RobinRyder Nice. Not sure why I forgot about scan(). – Robert S. Oct 23 '19 at 20:25 # Wolfram Language (Mathematica), 9 bytes Pi-2Pi/#&  Try it online! Returns the angle, in radians. # Python 3, 20 bytes lambda n:(n-2)*180/n  Try it online! • Nice answer. Quick tip - when you use Try it Online you can click on the link icon then choose the option to copy a code golf submission. When you paste that into your answer it will be all nicely formatted without you having to do the hard work :) – ElPedro Oct 22 '19 at 18:52 • You can save 2 bytes by expanding to get lambda n:180-360/n – Matthew Jensen Oct 22 '19 at 22:58 • @MatthewJensen thanks for the improvment but there is someone else who commit this – Delta Oct 23 '19 at 6:22 # C (gcc), 18 bytes z(n){n=180-360/n;}  Try it online! The above has accuracy issues on some inputs, below does not within the constraints of a float. The same could be said about slightly longer code which uses doubles... it's data types of ever increasing width all the way down. # C (gcc), 30 bytes float z(float n){n=180-360/n;}  Try it online! • 26 bytes – ceilingcat Oct 23 '19 at 0:12 • You can remove the space after the return. – S.S. Anne Oct 23 '19 at 0:32 • Instead of return, use n= tio.run/##S9ZNT07@/79KI0@zOs9WI0/… – girobuz Oct 23 '19 at 0:34 • Sorry, wasn’t referring to the #define; was referring to the fact that the first float parameter register %xmm0 is also used to return float values. – ceilingcat Oct 23 '19 at 8:19 • 18 bytes unless there's some reason that 180-(360/n) produces incorrect results. – Draco18s no longer trusts SE Oct 23 '19 at 14:50 # APL (Dyalog Unicode), 11 9 bytes 180-360÷⊢  Try it online! Train that returns the value of each angle in degrees. Shaved a couple bytes off by switching to a smaller formula. # Excel, 11 bytes =180-360/A1  Result in Degrees. For Degrees (and Gradians), 3 bytes can be saved by simplifying =(A1-2)*180/A1. The Radians version though remains the same length: =(A1-2)*PI()/A1 vs =PI()-2*PI()/A1. Shortest Radians answer is 14 bytes: =(1-2/A1)*PI() # Jelly, 6 bytes _2÷×ØP  A monadic Link accepting an integer which outputs a float. Try it online! ### How? _2÷×ØP - Link: integer, sides 2 - literal two _ - (sides) subtract ÷ - divided by (sides) ØP - literal pi (well, a float representation of it) × - multiply  # Cubix, 31 bytes U;o;O@...I2-'´*p,O;%u//'O;oS@!  Try it online! Outputs degrees as a integer and a fraction (if needed). This was interesting to do as, there is no floats in Cubix. I hope the output format is OK for the challenge. Wrapped onto a cube  U ; o ; O @ . . . I 2 - ' ´ * p , O ; % u / / ' O ; o S @ ! . . . . . . . . . . . . . . . . . . . . . . . .  Watch It Run • I2-'´* Get n input, take away 2, push 180 and multiply • p,O; Bring initial input to the TOS, integer divide, output integer and pop • %u! Do modulo, u-Turn to the right, test for 0 • @ if zero halt • So;O push 32 (space) onto stack, output as char and pop. Output modulo result • '// push / to stack and reflect around the cube. This will end up on the top face after jumping an output • o;U;O@ output the /, pop, u-Turn to the left, pop and output the input # R, 18 bytes Hardly a new answer, but since I cannot comment I'll post it anyway. Output is in radians. n=scan();pi-2*pi/n  Try it online! # Retina 0.8.2, 4442 39 bytes crossed out 44 is still regular 44 .+$*
^11
$'$&
\G1
180$* (?=1+ (1+))\1  Try it online! Explanation: .+$*


Convert to unary.

^11
$'$&


Make a copy that is two less than the input.

\G1
180$*  Multiply that copy by 180. (?=1+ (1+))\1  Divide by the original input and convert to decimal. In Retina 1 you would obviously replace the $* with * and hence the 1 with _ but you could then save a further 5 bytes by replacing the middle two stages with this stage:

^__
180*$'$&


# Bash, 21 bytes

Same answer as everyone else, but in Bash :)

echo $[($1-2)*180/$1]  Try it online! • No floats in Bash? echo$[180-360/$1] does the same (except for rounding) for 18. – Jonathan Allan Oct 22 '19 at 17:16 • No, Bash does not do floating point. – spuck Oct 22 '19 at 17:31 # PHP (7.4), 21 18 bytes -3 bytes thanks to Jonathan Allan. fn($n)=>180-360/$n  Try it online! • Like my Python answer fn($n)=>180-360/$n for 18. – Jonathan Allan Oct 22 '19 at 17:13 • @JonathanAllan, thanks for the more compact formula. – Night2 Oct 22 '19 at 17:20 # J, 9 bytes %~180*-&2  Try it online! or # J, 9 bytes 180-360%]  Try it online! # K (oK), 8 bytes 180-360%  Try it online! # J, 8 bytes %o.@*-&2  Try it online! Implements pi * (x - 2) / x. Just like APL, J has the "Pi times" built-in o.. ### How it works %o.@*-&2 -&2 x - 2 % *-&2 (1/x) * (x - 2) o.@ Pi times the above  # Forth (gforth), 25 bytes : f 180e 360e s>f f/ f- ;  Try it online! Output is in degrees ### Code Explanation : f \ start a new word definition 180e \ put 180 on the floating point stack 360e \ put 360 on the floating point stack s>f f/ \ move n to the floating point stack and divide 360 by n f- \ subtract result from 180 ; \ end word definition  # Zsh, 17 bytes <<<$[180-360./$1]  Try it online! Pending consensus, the following may be a valid 15 byte solution, or more likely a 17 byte tie with () declaring it a function: ((180-360./$1))


Try it online!

# Runic Enchantments, 8 bytes

PPi2,,-@


Try it online!

### Explanation

P           Push Pi
P          Push Pi
2        Push 2
,       Divide
,      Divide
-     Subtract
@    Output and terminate


Works out to Pi-(Pi/(i/2)) which is equivalent to Pi-(2Pi/i) (PP2*i,-@, same length), I just liked the "push all the parts, then do all the math" arrangement ("it looked prettier").

# SimpleTemplate, 37 bytes

Just uses the simple formule 180-360/n used on other answers.
Due to ... sub-optimal ... math support, the formule was adapted to (-360/$n)+180 (it's almost the same, calculated in a different order). {@set/A-360 argv}{@incby180A}{@echoA}  You can try it on: http://sandbox.onlinephpfunctions.com/code/00b314dee3c10139928928d124be9fc1c59ef4bf On line 918, you can change between golfed, ungolfed and fn, to try the variants below. Ungolfed: {@set/ A -360 argv} {@inc by 180 A} {@echo A}  Yeah, there's not much to ungolf... Explanation: • {@set/ A -360 argv} - Stores in A the result of -360/argv. argv is a variable that holds all passed arguments (in a function or when running the code). A is now an array with argc elements (argc holds the number of aguments passed). • {@inc by 180 A} - Increments all values of A by 180 (A+180, basically) • {@echo A} - Outputs the values of A, without delimiter. One could use {@return A} if inside a function, to get an usable array. Function alternative: Converting to a function to get an usable array is easy: {@fn N} {@set/ A -360 argv} {@inc by 180 A} {@return A} {@/}  Creates a function N that takes multiple arguments and returns an array. Just call it as {@call N into <variable> <argument, arguments...>}. If you are curious, this code compiles to the following: // {@set/A-360 argv}$DATA['A'] = array_map(function($value)use(&$DATA){return (-360 / $value);},$FN['array_flat']((isset($DATA['argv'])?$DATA['argv']:null)));

// {@incby180A}
$DATA['A'] =$FN['inc'](isset($DATA['A'])?$DATA['A']:0, 180);

// {@echoA}
echo implode('', $FN['array_flat']((isset($DATA['A'])?\$DATA['A']:null)));