Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Polygons are named after the number of sides that they have. A pentagon has 5 sides, an octagon has 8 sides. But how are they named? What's the name for a 248-sided polygon?

All polygons are suffixed with -gon. There are specific prefixes for each polygon depending on the number of sides. Here are the prefixes for the lower numbers:

3 - tri
4 - tetra
5 - penta
6 - hexa
7 - hepta
8 - octa
9 - nona
10 - deca
11 - undeca
12 - dodeca
13 - triskaideca
14 - tetradeca
15 - pentadeca
16 - hexadeca
17 - heptadeca
18 - octadeca
19 - nonadeca
20 - icosa

Polygons with 21 to 99 sides have a different system. Take the prefix for the tens digit (found on the left column), the ones digit (right column below), and then stick a "kai" between them to get (tens)kai(ones)gon.

10 - deca        | 1 - hena
20 - icosi       | 2 - di
30 - triaconta   | 3 - tri
40 - tetraconta  | 4 - tetra
50 - pentaconta  | 5 - penta
60 - hexaconta   | 6 - hexa
70 - heptaconta  | 7 - hepta
80 - octaconta   | 8 - octa
90 - nonaconta   | 9 - nona

The 3-digit sided polygons are named in a similar fashion. A 100-sided polygon is called a hectogon. Take the hundreds digit, find it on the column for ones digits, then stick a "hecta" to its right. Now number off the tens and ones like above: (hundreds)hecta(tens)kai(ones)gon. If the hundreds place digit is a 1, don't put the prefix behind "hecta"; if the ones place is 0, then omit the (ones) section.

So, given an integer (3 <= n <= 999), return the name of an n-sided polygon. n-gon is not a valid answer :P

As with all code golf, shortest code wins.

share|improve this question
    
100-sided is hectogon or hectagon? The tens digit above 100 is also unclear. I guess 110 and 120 are hectahenakaigon and hectadikaigon (or hectaicosikaigon?) –  ugoren Feb 17 '13 at 10:29
    
Also, 101-sided is "hectakaihenagon" (as I understand from your rules) or "hectahenagon"? –  ugoren Feb 17 '13 at 11:39
    
@beary605 I think nona- is wrong. That would be from latin and the others, hepta-, hexa- are from greek. It should be ennea-. I know the wikipedia says nonagon is OK! –  felipa Feb 17 '13 at 20:41
    
@felipa: I was debating whether to have it be ennea-, but I've heard "nonagon" more often than "enneagon", so I went with that. –  beary605 Feb 17 '13 at 21:49
    
@ugoren: 100 - hectagon, and I didn't see that I didn't have deca, thanks for pointing that out. 110 - hectadecagon, 120 - hectaicosigon. 101 - hectakaihenagon. –  beary605 Feb 17 '13 at 21:52

4 Answers 4

C, 401, 391

Lets put something out there so there is at least one answer for reference :-)

char*s[]={"","hena","di","tri","tetra","penta","hexa","hepta","octa","nona","un","do","triskai","deca","icosi","tria","conta","kai","icosa","hecto","hecta","gon\n"};
p(i){printf(s[i]);}
t;n(i){
    if(i<10)
        p(i);   // ones
    else if(i<20){
        i-=10; 
        p(i+(i&&i<4)*9); // teens
        p(13); // "deca"
    }else if(i==20)
        p(18); // "icosa"
    else if(i==100)
        p(19); // "hecto"
    else{
        t=i/100;
        p(t>1?t:0); // hundreds
        p(t?20:0);  // "hecta"
        i%=100;
        t=i/10;
        p(t+(t&&t<4)*12); // tens
        p(t>2?16:0);      // "conta"
        i%=10;
        p(i?17:0); // "kai"
        p(i);      // ones
    }
    p(21); // "gon\n"
}

Test with:

main(){
    for(int i=3;i<=999;i++){
        printf("%3d: ",i);n(i);
    }
}

Subset of output:

  3: trigon
  4: tetragon
  5: pentagon
  6: hexagon
  7: heptagon
  8: octagon
  9: nonagon
 10: decagon
 11: undecagon
 12: dodecagon
 13: triskaidecagon
 14: tetradecagon
 15: pentadecagon
 16: hexadecagon
 17: heptadecagon
 18: octadecagon
 19: nonadecagon
 20: icosagon
 21: icosikaihenagon
 22: icosikaidigon
 23: icosikaitrigon
...
 99: nonacontakainonagon
100: hectogon
101: hectakaihenagon
102: hectakaidigon
103: hectakaitrigon
104: hectakaitetragon
105: hectakaipentagon
106: hectakaihexagon
107: hectakaiheptagon
108: hectakaioctagon
109: hectakainonagon
110: hectadecagon
111: hectadecakaihenagon
...
997: nonahectanonacontakaiheptagon
998: nonahectanonacontakaioctagon
999: nonahectanonacontakainonagon
share|improve this answer
1  
Nice. You can replace if..else with ?: (and ; with ,) to save some characters. Also t>1&&p(t) and similar changes can help. And I'm not sure, but perhaps you could rearrange s so that the constants you use would be single digit. –  ugoren Feb 20 '13 at 9:12

JavaScript - 405 386

Version 1: (405 chars)

i=prompt(),o='0hena0di0tri0tetra0penta0hexa0hepta0octa0nona'.split(0),d='0W0icosi0triaQ0tetraQ0pentaQ0hexaQ0heptaQ0octaQ0nonaQ'.split(0),t='W0unW0doW0triskaiW0tetraW0pentaW0hexaW0heptaW0octaW0nonaW0icosa'.split(0),x=('000'+i).substr(-3).split('');alert((i<10?o[i]:i<21?t[i-10]:i==100?'hecto':(i<100?'':i<200?'hecta':o[x[0]]+'hecta')+d[x[1]]+'kai'+o[x[2]]).replace(/Q/g,'conta').replace(/W/g,'deca')+'gon')

Version 2: (386 chars)

i=prompt(),p='0tetra0penta0hexa0hepta0octa0nona',o='0hena0di0tri'+p,t=('W0unW0doW0triskai'+p.replace(/0/g,'W0')+'W0icosa').split(0),x=('000'+i).substr(-3).split(''),o=o.split(0);alert((i<10?o[i]:i<21?t[i-10]:i==100?'hecto':(i<100?'':i<200?'hecta':o[x[0]]+'hecta')+('0W0icosi0tria'+p.replace(/0/g,'Q0')+'Q').split(0)[x[1]]+'kai'+o[x[2]]).replace(/Q/g,'conta').replace(/W/g,'deca')+'gon')

Output example:

3: trigon
4: tetragon
5: pentagon
6: hexagon
7: heptagon
8: octagon
9: nonagon
10: decagon
11: undecagon
12: dodecagon
13: triskaidecagon
14: tetradecagon
15: pentadecagon
16: hexadecagon
17: heptadecagon
18: octadecagon
19: nonadecagon
20: icosagon
21: icosikaihenagon
22: icosikaidigon
23: icosikaitrigon
99: nonacontakainonagon
...
100: hectogon
101: hectakaihenagon
102: hectakaidigon
103: hectakaitrigon
104: hectakaitetragon
105: hectakaipentagon
106: hectakaihexagon
107: hectakaiheptagon
108: hectakaioctagon
109: hectakainonagon
110: hectadecakaigon
111: hectadecakaihenagon
...
997: nonahectanonacontakaiheptagon
998: nonahectanonacontakaioctagon
999: nonahectanonacontakainonagon 
share|improve this answer

Cobra - 370

This is currently a function, but if that isn't allowed then I'll change it.

def f(x) as String
    o='. hena di tri tetra penta hexa hepta octa nona'.split
    o[0],t,h,d='',['','deca','icosci','triconta'],['','hecta','dihecta','trihecta'],['undeca','dodeca','triskaideca']
    for z in 4:10,t,h,d=t+[o[z]+'conta'],h+[o[z]+'hecta'],d+[o[z]+'deca']
    for z in 10,t[z]+='kai'
    return if(x-100,h[x//100]+if(10<x<20,d[x-11],t[x%100//10]+o[x%10]),'hecto')+'gon'

And for some reason the cobra highlighter has // set as a comment, which is wrong.

share|improve this answer

Mathematica 271 257

Note: I implemented the higher alkaline convention for naming polygons. It is somewhat more regular and easier to learn than the alternatives.

I'm not absolutely certain about the convention for polygons with more than 100 edges, so I'm open for critiques.

o = {"", "hen", "do", "tri", "tetra", "penta", "hexa", "hepta", "octa", "nona"}; p = "hecto";
d@n_ := Switch[n, 0, "", 1, "deca", 2, "cosa", 3, "tria", _, o[[n + 1]] <> "conta"];

g@n_ := IntegerDigits[n, 10, 3] /. {h_, t_, u_} :>(Which[n == 100, p, 
  True, {o[[u + 1]], d@t} <> If[n < 100, "", o[[h + 1]] <> p]] <> "gon")

Names for n-gons where where n ranges from 3 through 100.

polys

Here are the names for polygons with {100, 213,326,439,552,665,778,891} edges.

Table[g[k], {k, 100, 1000, 113}

{"hectogon", "tridecadohectogon", "hexacosatrihectogon","nonatriatetrahectogon", "dopentacontapentahectogon","pentahexacontahexahectogon", "octaheptacontaheptahectogon", "hennonacontaoctahectogon"}

share|improve this answer
2  
This is not as the requirenment. If I choose to implement my convension for naming polygons where the name is N-gon where N is the number my code will be ~20 characters... –  Дамян Станчев Jun 24 at 8:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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