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


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.

  • \$\begingroup\$ 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? \$\endgroup\$ – Morgan Thrapp Feb 8 '16 at 21:39
  • \$\begingroup\$ Infinite precision - it's a symbolic expression, not a number. You can hard-code it, though I think you can do better. \$\endgroup\$ – anatolyg Feb 8 '16 at 21:40
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ – Level River St Feb 9 '16 at 1:17
  • \$\begingroup\$ 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. \$\endgroup\$ – Level River St Feb 9 '16 at 1:25
  • \$\begingroup\$ 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. \$\endgroup\$ – anatolyg Feb 9 '16 at 9:41

11 Answers 11


CJam (60 bytes)

1-4{"(- +sqrt( * -4*

Online demo

Output (with added newlines to avoid wrapping):


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
  • \$\begingroup\$ Yes, this is an awesome solution! \$\endgroup\$ – anatolyg Mar 3 '16 at 8:03

JavaScript (ES6), 93

Edit 1 byte saved thx @ETHproductions

An anonymous function.






// Math check


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


Mathematica, 26 bytes



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



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))))))))
  • \$\begingroup\$ I think you should replace each space by the multiplication sign * to make the syntax valid \$\endgroup\$ – anatolyg Feb 9 '16 at 12:23
  • \$\begingroup\$ @anatolyg It is conventional in Mathematica to use space as multiplication. I will add a different form of the same expression soon. \$\endgroup\$ – njpipeorgan Feb 9 '16 at 12:30
  • \$\begingroup\$ I'm trying to find something similar in Sage, but I can't. :( \$\endgroup\$ – 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:

  • \$\begingroup\$ Is it possible to remove the null byte? \$\endgroup\$ – CalculatorFeline Mar 2 '16 at 20:43
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – 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!


$ base64 -d > exact-cos.bg <<< 0zXUNzTTLi4sKtEwNNeEs3WNtGBi2sYmmOKo0rpY9AAxggsy2gIA
$ wc -c exact-cos.bg
39 exact-cos.bg
$ bubblegum exact-cos.bg; echo
$ cat verify-cos

from sympy import *

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

golflua, 122 chars

Adapted from this Math.se post.


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



Which, when evaluated, returns 0.93247222940436


Ruby, 71

I think this could be shorter in another language.


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.


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


Vim, 70 keystrokes

:im a *sqrt(<CR>
:im b a17)<CR>

Perl 5, 78 bytes

A subroutine:


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

Hat tip.


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.