When "Fifty shades of grey" comes to programmer. LOL


code golf - Fifty Shades of Grey - Programming Puzzles & Code Golf Stack Exchange

Boys and girls are excited to see Fifty Shades of Grey on the silver screen, we just want to code without being bothered, so here's a challenge to pick our brain.


  • Print on the screen fifty squares filled each with a different shade of grey
  • If your language of choice lacks image processing capabilities, you could output an image file
  • Squares must be visible, at least 20 x 20 pixels
  • You can not use random numbers unless you make sure each shade is unique.
  • You can not connect to any service over any network
  • You can not read any files in your program.
  • You can not use any libraries out of the standard libraries of your language of choice.

目前最多人按讚的是 Mathematica 寫的版本,只花 30 chars



  • CJam - 23 chars
  • Mathematica - 34 chars
GrayLevel[#/50]~Style~50 &~Array~50  
  • Sage - 29 chars
  • Java - 180 chars
import java.awt.*;void f(){new Frame(){public void paint(Graphics g){for(int i=1;i<51;g.setColor(new Color(328965*i)),g.fillRect(i%8*20,i++/8*20,20,20))setSize(600,600);}}.show();}  
  • R - 35 chars
  • ipython 2 - 54 chars
  • ipython 2 - 61 chars
  • BBC BASIC - 57 chars
  • C++ - 93 chars
#include <cstdio>  
int main(){for(int i=51;i--;)printf("\e[48;2;%i;%i;%im ",i,i,i);}  
  • C - 58 chars
main(i){for(i=51;i--;)printf("\e[48;2;%i;%i;%im ",i,i,i);}  
  • JavaScript - 72 chars
for(i=90;i>40;)document.write('<font color=#'+i+i+i--+' size=7>&#9632;')  
  • JavaScript - 70 chars
for(i=60;--i>9;)document.write('<font color='+i+i+i+' size=7>&#9632;')  

一大堆... 剩下懶得看了XDD
是說原來這種比誰寫的 code 短的比賽方式叫 Code Golf 呀



如果覺得這篇文章對你有幫助, 除了留言讓我知道外, 或許也可以考慮請我喝杯咖啡, 不論金額多寡我都會非常感激且能鼓勵我繼續寫出對你有幫助的文章。

If this blog post happens to be helpful to you, besides of leaving a reply, you may consider buy me a cup of coffee to support me. It would help me write more articles helpful to you in the future and I would really appreciate it.

Related Posts