Go: package main

import (
	"fmt"
	"math"
	"strconv"
)

// ColorCode is a string like #rrggbb
type ColorCode string

// ColorName is a name of color
type ColorName string

// Color has colorname and colorcode
type Color struct {
	name ColorName
	code ColorCode
}

// ColorTable has default colors
type ColorTable struct {
	colors []Color
}

var (
	colorTable = ColorTable{
		[]Color{
			Color{"black", "#000000"},
			Color{"blue", "#0000ff"},
			Color{"lime", "#00ff00"},
			Color{"aqua", "#00ffff"},
			Color{"red", "#ff0000"},
			Color{"fuchsia", "#ff00ff"},
			Color{"yellow", "#ffff00"},
			Color{"white", "#ffffff"},
		},
	}
)

func main() {
	var code ColorCode
	fmt.Scanf("%s", &code)
	for {
		if code == "0" {
			break
		}
		name := colorTable.getNearestColor(code)
		fmt.Println(name)
		fmt.Scanf("%s", &code)
	}
}

func (colorstr ColorCode) parseColorValue() (float64, float64, float64) {
	rr, gg, bb := colorstr[1:3], colorstr[3:5], colorstr[5:7]
	rri, err1 := strconv.ParseUint(string(rr), 16, 8)
	ggi, err2 := strconv.ParseUint(string(gg), 16, 8)
	bbi, err3 := strconv.ParseUint(string(bb), 16, 8)
	if err1 != nil || err2 != nil || err3 != nil {
		err := fmt.Errorf(
			"Failed to read color strings, color code:%s, err: %v %v %v",
			colorstr, err1, err2, err3)
		panic(err)
	}
	return float64(rri), float64(ggi), float64(bbi)
}

func (table *ColorTable) getNearestColor(targetcode ColorCode) ColorName {
	var nearestColor ColorName
	var nearestValue = 1e5
	for _, v := range table.colors {
		trr, tgg, tbb := targetcode.parseColorValue()
		prr, pgg, pbb := v.code.parseColorValue()
		// diff := math.Sqrt(
		// 	math.Pow(prr-trr, 2) + math.Pow(pgg-tgg, 2) + math.Pow(pbb-tbb, 2))
		diff := math.Pow(prr-trr, 2) +
			math.Pow(pgg-tgg, 2) +
			math.Pow(pbb-tbb, 2)
		if nearestValue > diff {
			nearestValue = diff
			nearestColor = v.name
		}
	}
	return nearestColor
}

C++: #include <cstdio>
#include <cstring>
#include <cmath>
struct Color {char s[8]; int R, G, B; double d;};
Color t[8] = {{"black", 0, 0, 0}, {"blue", 0, 0, 255}, {"lime", 0, 255, 0}, {"aqua", 0, 255, 255}, {"red", 255, 0, 0}, {"fuchsia", 255, 0, 255}, {"yellow", 255, 255, 0}, {"white", 255, 255, 255}};
int main()
{
	char s[8];
	int R, G, B, i, r;
	double rd;
	while (scanf("%8s", s))
	{
		if (strcmp(s, "0") == 0) break;
		sscanf(s, "#%2x%2x%2x", &R, &G, &B);
		rd = 500.0;
		for (i = 0; i < 8; i++)
		{
			t[i].d = sqrt((double)(R - t[i].R)*(R - t[i].R) + (double)(G - t[i].G)*(G - t[i].G) + (double)(B - t[i].B)*(B - t[i].B));
			if (rd >= t[i].d)
				rd = t[i].d, r = i;
		}
		printf("%s\n", t[r].s);
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
using namespace std;

#define R 0
#define G 1
#define B 2

void print_col(int r, int g, int b)
{
  string col[] = {"black", "blue", "lime", "aqua", "red", "fuchsia", "yellow", "white"};
  int c[][3] = {{0, 0, 0}, {0, 0, 255}, {0, 255, 0}, {0, 255, 255},
               {255, 0, 0}, {255, 0, 255}, {255, 255, 0}, {255, 255, 255}};
  double d = 1000000;
  double dd;
  int a = -1;

  for(int i = 0; i < 8; i++)
  {
    dd = pow(r - c[i][R], 2) + pow(g - c[i][G], 2) + pow(b - c[i][B], 2);
    if(dd < d)
    {
      d = dd;
      a = i;
    }
  }
  cout << col[a] << endl;
}

int to_num(char n)
{
  return n >= 'a' ? n - 'a' + 10 : n - '0';
}

int main()
{
  string s;
  int r, g, b;
  while(true)
  {
    cin >> s;
    if(s[0] == '0')
      break;
    r = to_num(s[1]) * 16 + to_num(s[2]);
    g = to_num(s[3]) * 16 + to_num(s[4]);
    b = to_num(s[5]) * 16 + to_num(s[6]);
    print_col(r, g, b);
  }
}

C++: #include <string>
#include <vector>
#include <iostream>

using namespace std;

int hextoi(string S)
{
    int sum = 0;
    
    if(isdigit(S[0]))
    {
        sum += (S[0] - 48) * 16;
    }
    else
    {
        sum += (S[0] - 87) * 16;
    }
    if(isdigit(S[1]))
    {
        sum += S[1] - 48;
    }
    else
    {
        sum += S[1] - 97;
    }
    
    return sum;
}

const vector<string> color = { "black", "blue", "lime", "aqua", "red", "fuchsia", "yellow", "white" };

int main()
{
    string S;
    
    while(true)
    {
        cin >> S;
        
        if(S == "0") { break; }
        
        int R = hextoi(S.substr(1, 2));
        int G = hextoi(S.substr(3, 2));
        int B = hextoi(S.substr(5, 2));
        
        int near = 999999999, c = -1;
        
        for(int i = 0; i < 8; i++)
        {
            int R2 = ((i / 4) % 2 ? 255 : 0);
            int G2 = ((i / 2) % 2 ? 255 : 0);
            int B2 = ((i / 1) % 2 ? 255 : 0);
            
            int N2 = (R - R2) * (R - R2) + (B - B2) * (B - B2) + (G - G2) * (G - G2);
            
            if(N2 < near)
            {
                near = N2; c = i;
            }
        }
        
        cout << color[c] << endl;
    }
    
    return 0;
}

C++: #include<cstdlib>
char nm[][9]={"black","blue","lime","aqua","red","fuchsia","yellow","white"};
int cl[][3]={{0,0,0},{0,0,255},{0,255,0},{0,255,255},{255,0,0},{255,0,255},{255,255,0},{255,255,255}};
char rgb[3][3];
int main(){
	while(scanf(" #%2s%2s%2s",rgb,rgb+1,rgb+2)==3){
		int mn=99999999,mi,d2,d;
		for(int i=0;i<8;i++){
			d2=0;
			for(int j=0;j<3;j++)d=cl[i][j]-strtol(rgb[j],NULL,16),d*=d,d2+=d;
			if(d2<mn)mn=d2,mi=i;
		}
		printf("%s\n",nm[mi]);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<21

using namespace std;

int change(char c)
{
	if('a' <= c && c <= 'f') return int(c-'a') + 10;
	else return int(c-'0');
}

int main()
{
	string N[8] = {"black","blue","lime","aqua","red","fuchsia","yellow","white"};

	int c[8][3] = {{0,0,0},{0,0,256},{0,256,0},{0,256,256},{256,0,0},{256,0,256},{256,256,0,},{256,256,256}};

	string s;
	while(cin >> s)
	{
		if(s == "0") break;
		
		int r = 0,g = 0,b = 0;

		r += change(s[1])*16 + change(s[2]);
		g += change(s[3])*16 + change(s[4]);
		b += change(s[5])*16 + change(s[6]);
		
		double ans = INF;
		rep(i,8)
		{
			double temp = sqrt(	(c[i][0]-r)*(c[i][0]-r) + (c[i][1]-g)*(c[i][1]-g) + (c[i][2]-b)*(c[i][2]-b));
			ans = min(ans,temp);
		}

		rep(i,8)
		{
			double temp = sqrt(	(c[i][0]-r)*(c[i][0]-r) + (c[i][1]-g)*(c[i][1]-g) + (c[i][2]-b)*(c[i][2]-b));
			if(temp == ans)
			{
				cout << N[i] << endl;
			}
		}
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

const int rgb[8][3] = {{0, 0, 0},
                       {0, 0, 255},
                       {0, 255, 0},
                       {0, 255, 255},
                       {255, 0, 0},
                       {255, 0, 255},
                       {255, 255, 0},
                       {255, 255, 255}};
const string name[8] = {"black", "blue", "lime",
                        "aqua", "red", "fuchsia",
                        "yellow", "white"};

int to_decimal(string s) 
{
    int d[2];
    int ret = 0;

    for (int i = 0; i < 2; i++) {
        if ('a' <= s[i] && s[i] <= 'f') 
            d[i] = s[i] - 'a' + 10;
        else
            d[i] = s[i] - '0';
    }

    return d[0] * 16 + d[1];
}

int main()
{
    string inp;

    while (cin >> inp, inp != "0") {
        int r, g, b;
        int d = 200000;
        int idx = 0;
        
        r = to_decimal(inp.substr(1, 2));
        g = to_decimal(inp.substr(3, 2));
        b = to_decimal(inp.substr(5, 2));

        for (int i = 0; i < 8; i++) {
            int tmp;
            tmp = (r - rgb[i][0]) * (r - rgb[i][0]) + 
                (g - rgb[i][1]) * (g - rgb[i][1]) +
                (b - rgb[i][2]) * (b - rgb[i][2]);

            if (tmp < d) {
                d = tmp;
                idx = i;
            }
        }
        cout << name[idx] << endl;
    }

    return 0;
}