# Print cos(2π/17) exactly

One way to construct a regular heptadecagon starts with drawing a horizontal line of length 1 from the center to a vertex. Then the distance along that line from the center to the second vertex is cos(2π/17). All other vertices are easy to get.

The goal of this challenge is to print (or produce in any other conventional way) an ASCII math expression involving the operations +, -, *, /, sqrt() and the natural numbers, which gives cos(2π/17) when evaluated. The power operator is not allowed: e.g. sqrt(17) cannot be written as 17^(1/2).

For example (formula taken from Wikipedia):

1/16*(
-1+
sqrt(17)+
sqrt(34-2*sqrt(17))+
2*sqrt(
17+
3*sqrt(17)-
sqrt(34-2*sqrt(17))-
2*sqrt(34+2*sqrt(17))
)
)


Whitespace doesn't matter. The length and complexity of the expression doesn't matter, as long as it's syntactically correct (as defined above) and numerically equivalent to cos(2π/17).

Built-in functions are allowed.

• What degree of precision do we need? Why can't I just hard code the formula if it's always a constant number? Why would I have to calculate any numbers at all? – Morgan Thrapp Feb 8 '16 at 21:39
• Infinite precision - it's a symbolic expression, not a number. You can hard-code it, though I think you can do better. – anatolyg Feb 8 '16 at 21:40
• I think this question has been misunderstood. It's fairly clear to me. It's a Kolmogorov complexity requiring An ASCII Math expression evaluating to cos(2pi/17) That is, either a straight text compression of the example expression given, or (as an interesting twist) a significant rearrangement of that expression provided it evaluates to the exact same quantity. Is that correct? Minor nitpick: in addition to natural numbers +,-,*,/ and sqrt() you have also used individual parentheses in other ways so I assume they are allowed. Question: no power operator? No decimals? – Level River St Feb 9 '16 at 1:17
• Oops: I missed calculating e.g. cos(π/16) would be OK... That is unclear. We need to know exactly what operators and functions are allowed and what aren't. This question can't be reopened until it's absolutely clear what operators are allowed. I suggest you delete that bit and stick with the square roots. – Level River St Feb 9 '16 at 1:25
• I removed the confusing part about built-in functions. It's actually interesting to see how one could use dedicated functions to solve this. Wolfram-alpha doesn't try to solve this. – anatolyg Feb 9 '16 at 9:41

## CJam (60 bytes)

1-4{"(- +sqrt( * -4*
))/2"S/@a*N/\a*}:F~'-\+WF'-\+W-4FWFF'/2


Online demo

Output (with added newlines to avoid wrapping):

(--(--(-1+sqrt(1*1-4*-4))/2+sqrt(-(-1+sqrt(1*1-4*-4))/2*-(-1+sqrt(1*1-4*-4))/2-4*-1)
)/2+sqrt(-(--(-1+sqrt(1*1-4*-4))/2+sqrt(-(-1+sqrt(1*1-4*-4))/2*-(-1+sqrt(1*1-4*-4))/
2-4*-1))/2*-(--(-1+sqrt(1*1-4*-4))/2+sqrt(-(-1+sqrt(1*1-4*-4))/2*-(-1+sqrt(1*1-4*-4)
)/2-4*-1))/2-4*(-(--1+sqrt(-1*-1-4*-4))/2+sqrt((--1+sqrt(-1*-1-4*-4))/2*(--1+sqrt(-1
*-1-4*-4))/2-4*-1))/2))/2/2


I suspect this is the kind of answer OP was secretly hoping for. Rather than apply general-purpose compression to a string, it uses the field structure which Gauss first exploited to present this value in surds, in his Disquisitiones Arithmeticae.

I worked from this presentation (pages 18 and 19) but adapted it slightly in order to only ever require the positive root of a quadratic. Unrolled and in pseudo-code, the expression breaks down as

# Positive root of x^2 + bx + c = 0
F(b, c) = (-b + sqrt(b*b - 4*c))/2
# (8,1)
p = F(1, -4)
# (4,1)
q = F(-p, -1)
# -(8,3)
r = F(-1, -4)
# (4,3)
s = F(r, -1)
# Result
F(-q, s)/2

• Yes, this is an awesome solution! – anatolyg Mar 3 '16 at 8:03

# JavaScript (ES6), 93

Edit 1 byte saved thx @ETHproductions

An anonymous function.

x=>"(19)+8-29))+29+39)-8-29))-2*8+29)))-1)/16".replace(/8|9/g,n=>["sqrt(34","*sqrt(17"][n-8])


Output:

(1*sqrt(17)+sqrt(34-2*sqrt(17))+2*sqrt(17+3*sqrt(17)-sqrt(34-2*sqrt(17))-2*sqrt(34+2*sqrt(17)))-1)/16


TEST

F=x=>"(19)+8-29))+29+39)-8-29))-2*8+29)))-1)/16".replace(/8|9/g,n=>["sqrt(34","*sqrt(17"][n-8])

// Math check

k=Math.cos(Math.PI*2/17)
x=F()

v=eval(x.replace(/sqrt/g,"Math.sqrt"))

O.textContent=k+' (Math.cos(Math.PI*2/17))\n'+v+' ('+x+')\n'+(v==k)
<pre id=O></pre>

# Mathematica, 26 bytes

FunctionExpand@Cos[2Pi/17]


Output

1/(4 Sqrt[2/(15+Sqrt[17]-Sqrt[2 (17-Sqrt[17])]+Sqrt[2 (34+6 Sqrt[17]+Sqrt[2 (17-Sqrt[17])]-Sqrt[34 (17-Sqrt[17])]+8 Sqrt[2 (17+Sqrt[17])])])])


Alternative form

CForm@FunctionExpand@Cos[2Pi/17]


gives

1/(4.*Sqrt(2/(15 + Sqrt(17) - Sqrt(2*(17 - Sqrt(17))) + Sqrt(2*(34 + 6*Sqrt(17) + Sqrt(2*(17 - Sqrt(17))) - Sqrt(34*(17 - Sqrt(17))) + 8*Sqrt(2*(17 + Sqrt(17))))))))

• I think you should replace each space by the multiplication sign * to make the syntax valid – anatolyg Feb 9 '16 at 12:23
• @anatolyg It is conventional in Mathematica to use space as multiplication. I will add a different form of the same expression soon. – njpipeorgan Feb 9 '16 at 12:30
• I'm trying to find something similar in Sage, but I can't. :( – mbomb007 Mar 2 '16 at 22:58

# Bubblegum, 50 bytes

Hex dump:

78 DA D3 28 2E 2C 2A D1 30 34
D7 D4 06 33 8C 4D 74 61 02 5A
46 50 31 43 73 6D B8 98 B1 2E
16 65 30 31 6D 24 31 30 D2 35
D4 D4 37 34 03 00 AC BC 1B 67


Contains a pesky null byte, so you can’t try it online, but it prints this string:

(sqrt(17)+sqrt(34-sqrt(17)*2)+sqrt(17+sqrt(17)*3-sqrt(34-sqrt(17)*2)-sqrt(34+sqrt(17)*2)*2)*2-1)/16

• Is it possible to remove the null byte? – CalculatorFeline Mar 2 '16 at 20:43
• There's not issue with null bytes. The problem is that this file has zlib headers, which Bubblegum expects raw DEFLATE. If you remove the first two bytes (and optionally the last one), it works. – Dennis Apr 14 '16 at 23:53

# Bubblegum, 39 bytes

0000000: d3 35 d4 37 34 d3 2e 2e 2c 2a d1 30 34 d7 84 b3  .5.74...,*.04...
0000010: 75 8d b4 60 62 da c6 26 98 e2 a8 d2 ba 58 f4 00  u..b..&.....X..
0000020: 31 82 0b 32 da 02 00                             1..2...


Try it online!

### Verification

$base64 -d > exact-cos.bg <<< 0zXUNzTTLi4sKtEwNNeEs3WNtGBi2sYmmOKo0rpY9AAxggsy2gIA$ wc -c exact-cos.bg
39 exact-cos.bg
$bubblegum exact-cos.bg; echo -1/16+sqrt(17)/16+sqrt(-2*sqrt(17)+34)/16+sqrt(-2*sqrt(2*sqrt(17)+34)-sqrt(-2*sqrt(17)+34)+3*sqrt(17)+17)/8$ cat verify-cos
#!/usr/bin/python3

from sympy import *

print(Eq(S(input()), cos(2*pi/17)))
\$ bubblegum exact-cos.bg | ./verify-cos
True


# golflua, 122 chars

f='sqrt('y='(1-'..f..'17))'x='('..y..'/2+'..f..y..'*'..y..'/4+4))'w('(2/'..x..'+'..f..x..'/2*'..f..'17+4*'..f..'17))))/4')


which is pretty damn long :( (mostly because of the wasteful .. for concatenating strings).

Output

(2/((1-sqrt(17))/2+sqrt((1-sqrt(17))*(1-sqrt(17))/4+4))+sqrt(((1-sqrt(17))/2+sqrt((1-sqrt(17))*(1-sqrt(17))/4+4))/2*sqrt(17+4*sqrt(17))))/4


Which, when evaluated, returns 0.93247222940436

# Ruby, 71

I think this could be shorter in another language.

'(s/2)+s-s*2))+s*2+s*72)-4*s-s*2))-8*s+s*2)))-1)/16'.gsub(?s,'sqrt(34')


The above string expression evaluates to the below string. The idea was to rearrange the expression so that a single substitution of s -> sqrt(34 would be the only operation needed to decompress it.

(sqrt(34/2)+sqrt(34-sqrt(34*2))+sqrt(34*2+sqrt(34*72)-4*sqrt(34-sqrt(34*2))-8*sqrt(34+sqrt(34*2)))-1)/16


I assume "produce in any other conventional way" means an expression is enough. If this is not the case: Printing the string in quotation marks would need 2 extra bytes at the beginning of the source code: p . For 5 bytes it can be printed without quotation marks by adding puts. Enclosing it in ->{} yields an anonymous function for 4 extra bytes.

# CJam, 68

"2* 17 sqrt( 23) 243-41) (1+0+423+1+41-0-4243+41))"S/~5{s/\*}/W")/"G


Try it online

# R 68 bytes

s=sqrt;x=s(17);y=s(34-2*x);cat((x+y+2*s(17+3*x-y-2*s(34+2*x))-1)/16)


# Vim, 70 keystrokes

:im a *sqrt(<CR>
:im b a17)<CR>
i(1b-1+1a34-2b)+2b<bsp>+3b-1a34-2b)-2a34+2b)))/16<esc>


# Perl 5, 78 bytes

A subroutine:

{'(1S)+T-2S))+2S+3S)-T-2S))-2*T+2S)))-1)/16'=~s/S/*sqrt(17/gr=~s/T/sqrt(34/gr}


View the returned value by means of print sub{...}->()` or the like.

Hat tip.