17
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Degrees, radians, grads, or user discretion? \$\endgroup\$ – Jeff Zeitlin Oct 22 at 15:12
  • \$\begingroup\$ @JeffZeitlin user discretion, will edit to clarify \$\endgroup\$ – Jachdich Oct 22 at 15:13
  • \$\begingroup\$ Do you mean the sum of the internal angles or the value of one of the angles? \$\endgroup\$ – HiddenBabel Oct 22 at 15:16
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – Alex Robinson Oct 24 at 9:39
  • 1
    \$\begingroup\$ @AlexRobinson yeah that's fine, just specify in your answer. \$\endgroup\$ – Jachdich Oct 24 at 10:00

27 Answers 27

15
\$\begingroup\$

Perl 6, 8 bytes

π- τ/*

Try it online!

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

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

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!

\$\endgroup\$
8
\$\begingroup\$

JavaScript, 12 bytes

n=>180-360/n

Try it Online!

\$\endgroup\$
7
\$\begingroup\$

Shakespeare Programming Language, 242 226 203 bytes

Try it online!

(Whitespace added for readability only)

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.

\$\endgroup\$
6
\$\begingroup\$

MathGolf, 6 5 4 bytes

⌡π*╠

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

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – my pronoun is monicareinstate Oct 22 at 15:39
  • \$\begingroup\$ @someone Thanks. But I just realized that simply outputting in radians is even shorter than gradians or degrees. \$\endgroup\$ – Kevin Cruijssen Oct 22 at 15:45
6
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

R, 21 20 14 bytes

-7 thanks to Robin Ryder. Outputs in radians

pi-2*pi/scan()

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 20 bytes \$\endgroup\$ – Robin Ryder Oct 22 at 17:10
  • 1
    \$\begingroup\$ You can make it 14 bytes with scan(). \$\endgroup\$ – Robin Ryder Oct 23 at 19:23
  • \$\begingroup\$ @RobinRyder Nice. Not sure why I forgot about scan(). \$\endgroup\$ – Robert S. Oct 23 at 20:25
5
\$\begingroup\$

Wolfram Language (Mathematica), 9 bytes

Pi-2Pi/#&

Try it online!

Returns the angle, in radians.

\$\endgroup\$
5
\$\begingroup\$

Python 3, 20 bytes

lambda n:(n-2)*180/n

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – ElPedro Oct 22 at 18:52
  • \$\begingroup\$ You can save 2 bytes by expanding to get lambda n:180-360/n \$\endgroup\$ – Matthew Jensen Oct 22 at 22:58
  • \$\begingroup\$ @MatthewJensen thanks for the improvment but there is someone else who commit this \$\endgroup\$ – Delta Oct 23 at 6:22
5
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 26 bytes \$\endgroup\$ – ceilingcat Oct 23 at 0:12
  • 2
    \$\begingroup\$ You can remove the space after the return. \$\endgroup\$ – JL2210 Oct 23 at 0:32
  • 1
    \$\begingroup\$ Instead of return, use n= tio.run/##S9ZNT07@/79KI0@zOs9WI0/… \$\endgroup\$ – girobuz Oct 23 at 0:34
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – ceilingcat Oct 23 at 8:19
  • 1
    \$\begingroup\$ 18 bytes unless there's some reason that 180-(360/n) produces incorrect results. \$\endgroup\$ – Draco18s Oct 23 at 14:50
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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()

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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!

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

Retina 0.8.2, 44 42 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*$' $&
\$\endgroup\$
1
\$\begingroup\$

Bash, 21 bytes

Same answer as everyone else, but in Bash :)

echo $[($1-2)*180/$1]

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ No floats in Bash? echo $[180-360/$1] does the same (except for rounding) for 18. \$\endgroup\$ – Jonathan Allan Oct 22 at 17:16
  • 1
    \$\begingroup\$ No, Bash does not do floating point. \$\endgroup\$ – spuck Oct 22 at 17:31
1
\$\begingroup\$

PHP (7.4), 21 18 bytes

-3 bytes thanks to Jonathan Allan.

fn($n)=>180-360/$n

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Like my Python answer fn($n)=>180-360/$n for 18. \$\endgroup\$ – Jonathan Allan Oct 22 at 17:13
  • \$\begingroup\$ @JonathanAllan, thanks for the more compact formula. \$\endgroup\$ – Night2 Oct 22 at 17:20
1
\$\begingroup\$

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!

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

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
\$\endgroup\$
1
\$\begingroup\$

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  
\$\endgroup\$
1
\$\begingroup\$

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!

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

Runic Enchantments, 8 bytes

PPi2,,-@

Try it online!

Output is in radians.

Explanation

P           Push Pi
 P          Push Pi
  i         Read input
   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").

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

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)));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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