Problem of the Day
A new programming or logic puzzle every Mon-Fri

Pot Of Gold

Rainbows tend to be pretty cool to look at. If you're lucky you'll even find a pot of gold at the end of one. Today's problem is to create a program that generates a random RGB hex value (#7A340E) then prints out the next 100 values (#7A340F, #7A3410, etc.) after that. Afterwards it should print out "POT OF GOLD!!" and then exit.

Permalink: http://problemotd.com/problem/pot-of-gold/

Comments:

  • Dan - 10 years, 7 months ago

    Quick javascript ```javascript var r = ~~(Math.random() * 255), g = ~~(Math.random() * 255), b = ~~(Math.random() * 255); var currentDigit = 0; console.log("#" + r.toString(16) + g.toString(16) + b.toString(16)); for(var i = 0; i < 100; i++) { if(b >= 254) currentDigit++; if(g >= 254) currentDigit++; b = (b+1) % 255; if(currentDigit==1) g = (g+1) % 255; if(currentDigit==2) r = (r+1) % 255;

    console.log("#" + r.toString(16) + g.toString(16) + b.toString(16));
    

    } console.log("POT OF GOLD"); ```

    reply permalink

  • Dan Brinkman - 10 years, 7 months ago

    Whoops not logged in, here's the javascript with proper formatting

    var r = ~~(Math.random() * 255), g = ~~(Math.random() * 255), b = ~~(Math.random() * 255);
    var currentDigit = 0;
    console.log("#" + r.toString(16) + g.toString(16) + b.toString(16));
    for(var i = 0; i < 100; i++)
    {
        if(b >= 254)
            currentDigit++;
        if(g >= 254)
            currentDigit++;
        b = (b+1) % 255;
        if(currentDigit==1)
            g = (g+1) % 255;
        if(currentDigit==2)
            r = (r+1) % 255;
    
        console.log("#" + r.toString(16) + g.toString(16) + b.toString(16));
    }
    console.log("POT OF GOLD");
    

    reply permalink

  • Pyro - 10 years, 7 months ago

    Python solution

    import random
    
    RGB=[0,0,0]
    for i in range(3):
        RGB[i]= format(random.randint(0,255), '02X')
    
    for i in range(100):
        print  "#"+RGB[0]+RGB[1]+RGB[2]
        RGB[2]=format(int(RGB[2], 16)+1, '02X')
        if int(RGB[2],16)>255:
            RGB[2]=format(0, '02X')
    
    print "Pot of gold!"
    
    

    reply permalink

  • Anonymous - 10 years, 6 months ago

    #python 2.7
    import random
    x = random.randint(0, 16777215)
    i = 1
    while i <= 10:
        print '#'+ hex(x+i)[2:8].upper()
        i = i + 1
    print "POT OF GOLD!!"
    

    reply permalink

Content curated by @MaxBurstein