Calculate the internal angles of a regular polygon with N sides

18

1

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.

Jachdich

Posted 2019-10-22T15:06:45.183

Reputation: 319

1Degrees, radians, grads, or user discretion? – Jeff Zeitlin – 2019-10-22T15:12:55.477

@JeffZeitlin user discretion, will edit to clarify – Jachdich – 2019-10-22T15:13:51.003

Do you mean the sum of the internal angles or the value of one of the angles? – HiddenBabel – 2019-10-22T15:16:42.383

@HiddenBabel value of one. I'll edit to clarify – Jachdich – 2019-10-22T15:18:14.627

2Is 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 – 2019-10-24T09:39:57.647

1@AlexRobinson yeah that's fine, just specify in your answer. – Jachdich – 2019-10-24T10:00:21.100

Answers

6

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)

Kevin Cruijssen

Posted 2019-10-22T15:06:45.183

Reputation: 67 575

1I 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 – 2019-10-22T15:39:17.583

@someone Thanks. But I just realized that simply outputting in radians is even shorter than gradians or degrees. – Kevin Cruijssen – 2019-10-22T15:45:34.393

15

Perl 6, 8 bytes

π- τ/*

Try it online!

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

nwellnhof

Posted 2019-10-22T15:06:45.183

Reputation: 10 037

9

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!

Jonathan Allan

Posted 2019-10-22T15:06:45.183

Reputation: 67 804

8

JavaScript, 12 bytes

n=>180-360/n

Try it Online!

Shaggy

Posted 2019-10-22T15:06:45.183

Reputation: 24 623

7

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.

Hello Goodbye

Posted 2019-10-22T15:06:45.183

Reputation: 442

6

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.

Kevin Cruijssen

Posted 2019-10-22T15:06:45.183

Reputation: 67 575

6

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.

Shaggy

Posted 2019-10-22T15:06:45.183

Reputation: 24 623

6

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.

randomdude999

Posted 2019-10-22T15:06:45.183

Reputation: 789

6

R, 21 20 14 bytes

-7 thanks to Robin Ryder. Outputs in radians

pi-2*pi/scan()

Try it online!

Robert S.

Posted 2019-10-22T15:06:45.183

Reputation: 1 253

120 bytes – Robin Ryder – 2019-10-22T17:10:00.410

1

You can make it 14 bytes with scan().

– Robin Ryder – 2019-10-23T19:23:40.213

@RobinRyder Nice. Not sure why I forgot about scan(). – Robert S. – 2019-10-23T20:25:41.340

6

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.

Bubbler

Posted 2019-10-22T15:06:45.183

Reputation: 16 616

5

Python 3, 20 bytes

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

Try it online!

Delta

Posted 2019-10-22T15:06:45.183

Reputation: 543

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 – 2019-10-22T18:52:07.660

You can save 2 bytes by expanding to get lambda n:180-360/n – Matthew Jensen – 2019-10-22T22:58:22.590

@MatthewJensen thanks for the improvment but there is someone else who commit this – Delta – 2019-10-23T06:22:46.047

5

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!

foreverska

Posted 2019-10-22T15:06:45.183

Reputation: 181

26 bytes – ceilingcat – 2019-10-23T00:12:17.853

2You can remove the space after the return. – S.S. Anne – 2019-10-23T00:32:42.283

@girobuz How does that work? Does the compiler store the intermediate results in eax? – S.S. Anne – 2019-10-23T01:08:21.277

@JL2210 I don't remember, but I think it has to do with the fact that the return value uses the same register as last function parameter. I don't remember anything else. Definitely look it up. – girobuz – 2019-10-23T01:49:13.400

@ceilingcat You can go a bit farther with that define, I did at one point but wondered if it was against the spirit. – foreverska – 2019-10-23T03:47:43.243

@girobuz what an interesting fact. Obviously not portable but truly interesting. Updated. – foreverska – 2019-10-23T03:49:38.707

@foreverska same trick works for float arguments. – ceilingcat – 2019-10-23T04:00:41.760

@ceilingcat yes, that's correct. What you can also do is access the local variable in main() from the define directly and skip the function-like invocation.

My problem comes that the define itself is not compileable code so it feels cheese. Am I being too self-restrictive? – foreverska – 2019-10-23T04:14:59.420

1Sorry, 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 – 2019-10-23T08:19:58.623

@ceilingcat oh, interesting. Thanks. Updated. – foreverska – 2019-10-23T13:14:56.523

118 bytes unless there's some reason that 180-(360/n) produces incorrect results. – Draco18s no longer trusts SE – 2019-10-23T14:50:39.940

@Draco18s I went down a deep dark hole trying to find clarification on this yesterday without any luck. But it seems to be accepted math in more than one answer so who am I to gatekeep. Updated. Thanks. – foreverska – 2019-10-23T18:25:43.363

5

Wolfram Language (Mathematica), 9 bytes

Pi-2Pi/#&

Try it online!

Returns the angle, in radians.

attinat

Posted 2019-10-22T15:06:45.183

Reputation: 3 495

4

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.

J. Sallé

Posted 2019-10-22T15:06:45.183

Reputation: 3 233

4

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

Wernisch

Posted 2019-10-22T15:06:45.183

Reputation: 2 534

3

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

Jonathan Allan

Posted 2019-10-22T15:06:45.183

Reputation: 67 804

3

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

MickyT

Posted 2019-10-22T15:06:45.183

Reputation: 11 735

2

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*$' $&

Neil

Posted 2019-10-22T15:06:45.183

Reputation: 95 035

2

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!

Bart-Jan van Rossum

Posted 2019-10-22T15:06:45.183

Reputation: 111

1

PHP (7.4), 21 18 bytes

-3 bytes thanks to Jonathan Allan.

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

Try it online!

Night2

Posted 2019-10-22T15:06:45.183

Reputation: 5 484

1Like my Python answer fn($n)=>180-360/$n for 18. – Jonathan Allan – 2019-10-22T17:13:52.880

@JonathanAllan, thanks for the more compact formula. – Night2 – 2019-10-22T17:20:34.087

1

Bash, 21 bytes

Same answer as everyone else, but in Bash :)

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

Try it online!

spuck

Posted 2019-10-22T15:06:45.183

Reputation: 649

1No floats in Bash? echo $[180-360/$1] does the same (except for rounding) for 18. – Jonathan Allan – 2019-10-22T17:16:51.120

1No, Bash does not do floating point. – spuck – 2019-10-22T17:31:11.747

1

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!

Galen Ivanov

Posted 2019-10-22T15:06:45.183

Reputation: 13 815

1

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

Bubbler

Posted 2019-10-22T15:06:45.183

Reputation: 16 616

1

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  

reffu

Posted 2019-10-22T15:06:45.183

Reputation: 1 361

1

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!

GammaFunction

Posted 2019-10-22T15:06:45.183

Reputation: 2 838

1

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").

Draco18s no longer trusts SE

Posted 2019-10-22T15:06:45.183

Reputation: 3 053

1

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

Ismael Miguel

Posted 2019-10-22T15:06:45.183

Reputation: 6 797