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.
-
1\$\begingroup\$ Degrees, radians, grads, or user discretion? \$\endgroup\$Jeff Zeitlin– Jeff Zeitlin2019年10月22日 15:12:55 +00:00Commented Oct 22, 2019 at 15:12
-
\$\begingroup\$ @JeffZeitlin user discretion, will edit to clarify \$\endgroup\$Jachdich– Jachdich2019年10月22日 15:13:51 +00:00Commented Oct 22, 2019 at 15:13
-
\$\begingroup\$ Do you mean the sum of the internal angles or the value of one of the angles? \$\endgroup\$HiddenBabel– HiddenBabel2019年10月22日 15:16:42 +00:00Commented Oct 22, 2019 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– Alex Robinson2019年10月24日 09:39:57 +00:00Commented Oct 24, 2019 at 9:39
-
1\$\begingroup\$ @AlexRobinson yeah that's fine, just specify in your answer. \$\endgroup\$Jachdich– Jachdich2019年10月24日 10:00:21 +00:00Commented Oct 24, 2019 at 10:00
27 Answers 27
Perl 6, 8 bytes
π- τ/*
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.)
Shakespeare Programming Language, (削除) 242 (削除ここまで) (削除) 226 (削除ここまで) 203 bytes
(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.
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.
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)
-
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\$the default.– the default.2019年10月22日 15:39:17 +00:00Commented Oct 22, 2019 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– Kevin Cruijssen2019年10月22日 15:45:34 +00:00Commented Oct 22, 2019 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
Í*-# ́/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÷⊢
The result is in radians. It implements pi * (1 - 2 / x). The big circle is the "pi times" function.
R, (削除) 21 (削除ここまで) (削除) 20 (削除ここまで) 14 bytes
-7 thanks to Robin Ryder. Outputs in radians
pi-2*pi/scan()
-
1\$\begingroup\$ 20 bytes \$\endgroup\$Robin Ryder– Robin Ryder2019年10月22日 17:10:00 +00:00Commented Oct 22, 2019 at 17:10
-
1\$\begingroup\$ You can make it 14 bytes with
scan(). \$\endgroup\$Robin Ryder– Robin Ryder2019年10月23日 19:23:40 +00:00Commented Oct 23, 2019 at 19:23 -
\$\begingroup\$ @RobinRyder Nice. Not sure why I forgot about
scan(). \$\endgroup\$Robert S.– Robert S.2019年10月23日 20:25:41 +00:00Commented Oct 23, 2019 at 20:25
-
\$\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– ElPedro2019年10月22日 18:52:07 +00:00Commented Oct 22, 2019 at 18:52
-
\$\begingroup\$ You can save 2 bytes by expanding to get
lambda n:180-360/n\$\endgroup\$Matthew Jensen– Matthew Jensen2019年10月22日 22:58:22 +00:00Commented Oct 22, 2019 at 22:58 -
\$\begingroup\$ @MatthewJensen thanks for the improvment but there is someone else who commit this \$\endgroup\$Paul-B98– Paul-B982019年10月23日 06:22:46 +00:00Commented Oct 23, 2019 at 6:22
C (gcc), 18 bytes
z(n){n=180-360/n;}
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;}
-
\$\begingroup\$ 26 bytes \$\endgroup\$ceilingcat– ceilingcat2019年10月23日 00:12:17 +00:00Commented Oct 23, 2019 at 0:12
-
2\$\begingroup\$ You can remove the space after the
return. \$\endgroup\$S.S. Anne– S.S. Anne2019年10月23日 00:32:42 +00:00Commented Oct 23, 2019 at 0:32 -
1\$\begingroup\$ Instead of return, use
n=tio.run/##S9ZNT07@/79KI0@zOs9WI0/… \$\endgroup\$girobuz– girobuz2019年10月23日 00:34:10 +00:00Commented Oct 23, 2019 at 0:34 -
1\$\begingroup\$ Sorry, wasn’t referring to the #define; was referring to the fact that the first float parameter register
%xmm0is also used to return float values. \$\endgroup\$ceilingcat– ceilingcat2019年10月23日 08:19:58 +00:00Commented Oct 23, 2019 at 8:19 -
1\$\begingroup\$ 18 bytes unless there's some reason that
180-(360/n)produces incorrect results. \$\endgroup\$Draco18s no longer trusts SE– Draco18s no longer trusts SE2019年10月23日 14:50:39 +00:00Commented Oct 23, 2019 at 14:50
APL (Dyalog Unicode), (削除) 11 (削除ここまで) 9 bytes
180-360÷⊢
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
_×ばつØP
A monadic Link accepting an integer which outputs a float.
How?
_×ばつØ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;[email protected]' ́*p,O;%u//'O;oS@!
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 @ ! . . .
. . . . . . . . . . . .
. . .
. . .
. . .
I2-' ́*Get n input, take away 2, push 180 and multiplyp,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;Opush 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 outputo;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
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*$' $&
-
1\$\begingroup\$ No floats in Bash?
echo $[180-360/1ドル]does the same (except for rounding) for 18. \$\endgroup\$Jonathan Allan– Jonathan Allan2019年10月22日 17:16:51 +00:00Commented Oct 22, 2019 at 17:16 -
1\$\begingroup\$ No, Bash does not do floating point. \$\endgroup\$spuck– spuck2019年10月22日 17:31:11 +00:00Commented Oct 22, 2019 at 17:31
-
1\$\begingroup\$ Like my Python answer
fn($n)=>180-360/$nfor 18. \$\endgroup\$Jonathan Allan– Jonathan Allan2019年10月22日 17:13:52 +00:00Commented Oct 22, 2019 at 17:13 -
\$\begingroup\$ @JonathanAllan, thanks for the more compact formula. \$\endgroup\$Night2– Night22019年10月22日 17:20:34 +00:00Commented Oct 22, 2019 at 17:20
Forth (gforth), 25 bytes
: f 180e 360e s>f f/ f- ;
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ドル]
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ドル))
Runic Enchantments, 8 bytes
PPi2,,-@
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").
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 inAthe result of-360/argv.
argvis a variable that holds all passed arguments (in a function or when running the code).
Ais now an array withargcelements (argcholds the number of aguments passed).{@inc by 180 A}- Increments all values ofAby 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)));