[Question]
  [
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.
You have to:
* 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 cannot use random numbers **unless you make sure each shade is unique.**
* You **cannot connect to any service over any network**
* You **cannot read any files in your program.**
* You cannot use any libraries out of the standard libraries of your language of choice.
**This is code golf so shortest code wins.** 
  
[Answer]
# Mathematica, 30 bytes
Here is another Mathematica approach:
```
ArrayPlot[#+5#2&~Array~{5,10}]
```
![enter image description here](https://i.stack.imgur.com/Vra27.png)
or
```
ArrayPlot[5#+#2&~Array~{10,5}]
```
![enter image description here](https://i.stack.imgur.com/mtElK.png)
The first one simply creates an array
```
{{6, 11, 16, 21, 26, 31, 36, 41, 46, 51}, 
 {7, 12, 17, 22, 27, 32, 37, 42, 47, 52}, 
 {8, 13, 18, 23, 28, 33, 38, 43, 48, 53}, 
 {9, 14, 19, 24, 29, 34, 39, 44, 49, 54}, 
 {10, 15, 20, 25, 30, 35, 40, 45, 50, 55}}
```
and the second one
```
{{6, 7, 8, 9, 10},
 {11, 12, 13, 14, 15},
 {16, 17, 18, 19, 20},
 {21, 22, 23, 24, 25},
 {26, 27, 28, 29, 30},
 {31, 32, 33, 34, 35},
 {36, 37, 38, 39, 40},
 {41, 42, 43, 44, 45},
 {46, 47, 48, 49, 50},
 {51, 52, 53, 54, 55}}
```
Then, `ArrayPlot` plots them as a grid and, by default, uses greyscale to visualise the values.
[Answer]
# CJam - 23 (no actual graphical output)
Since CJam can't draw on the screen (yet?), I wrote a program that outputs an image in plain [pgm](http://en.wikipedia.org/wiki/Netpbm_format) format.  
Save the output to a file called 50.pgm and open with an image viewer/editor.
```
"P2"1e3K51_,1>K*$K*~]N*
```
[Try it online](http://cjam.aditsu.net/#code=%22P2%221e3K51_%2C1%3EK*%24K*~%5DN*)
The result looks like this:
![50.png](https://i.stack.imgur.com/jV4d1.png)
**Explanation:**
```
"P2"     push "P2" (netpbm magic number)
1e3K     push 1000 and K=20 (image size)
51       push 51 (value of white)
_,       duplicate 51 and convert to an array [0 1 2 ... 50]
1>       remove 0 (slice from 1)
K*       repeat the array 20 times
$        sort, to get [(20 1's) (20 2's) ... (20 50's)]
K*       repeat the array 20 times
~        dump the elements on the stack
]        put everything from the stack in an array
N*       join with newlines
```
[Answer]
# Mathematica ~~72 61 59 43 35~~ 34 bytes
**Current version** (34 bytes)
```
GrayLevel[#/50]~Style~50 &~Array~50    
```
![grid2](https://i.stack.imgur.com/to2fi.png)
---
**Earlier version** (59 bytes), thanks to Martin Büttner.
```
Graphics[{GrayLevel[#/50],Cuboid@{#~Mod~9,#/9}}&/@Range@50]
```
Blue borders added to highlight position of squares.
```
Graphics[{EdgeForm[Blue], GrayLevel[#/50], Cuboid@{#~Mod~9, #/9}} & /@Range@50]
```
![blue](https://i.stack.imgur.com/LeYje.png)
Number of squares:
```
    Length[%[[1]]]
```
> 
> 50
> 
> 
> 
---
**First attempt (72 bytes)**
If the squares can overlap.
As Zgarb notes, there is a remote possibility that one square would hide another.
```
  Graphics@Table[{GrayLevel@k,Cuboid[{0,15}~RandomReal~2]},{k,.4,.89,.01}]
```
![enter image description here](https://i.stack.imgur.com/3h1Xp.png)
[Answer]
# Sage - ~~26~~ ~~24~~ ~~35~~ 29 characters
```
matrix(5,10,range(50)).plot()
```
It looks like this:
## 5x10 shades of grey
*Previous attempts*: First I had `Matrix([range(50)]).plot()`, then `matrix_plot([range(50)])`. These looked like this:
![50 shades of grey](https://i.stack.imgur.com/Jx2nO.png)
User @flawr pointed out that the squares produced with default settings are too small. Adding a `figsize` options fixes it, but it is 6 characters longer than the 5x10 version at the top of this answer:
```
matrix_plot([range(50)],figsize=20)
```
[Answer]
# MATLAB, 24 bytes
Second attempt, now actually conforming to the rules.
```
imshow(0:1/49:1,'I',2e3)
```
The 'I' stands for 'InitialMagnification', but apparently Matlab will autocomplete names in name/value pairs. Sure is a good golfing trick!
![enter image description here](https://i.stack.imgur.com/XBjeB.png)
[Answer]
## R, ~~47 42 39 38 37 36~~ 35 bytes
Assuming they're allowed to overlap, this works. `Symbols` requires a vector that holds the size for each square, and I'm saving space by reusing the same vector for all function parameters. The largest square is always 1 inch wide. With 50:99 as the range of integers, the smallest square will be around half an inch wide, so 36 pixels at 72 dpi.
```
symbols(x,,,x<-50:99,bg=gray(x/99))
```
Thanks to everyone for the comments and suggestions, I'm pretty new to R so this is all very educational.
![new and improved?](https://i.stack.imgur.com/vsEuP.png)
Previous versions:
```
symbols(x,sq=x<-50:99,bg=gray(x/99))            #36
x=50:99;symbols(x,sq=x,bg=gray(x/99))           #37
x<-50:99;symbols(x,sq=x,bg=gray(x/99))          #38
x<-1:50;symbols(x,sq=x/x,bg=gray(x/50))         #39
x<-1:50/50;symbols(x,sq=x/x,bg=rgb(x,x,x))      #42
x<-1:50/50;symbols(x,squares=x/x,bg=rgb(x,x,x)) #47
```
Previous image:
![50 shades of R](https://i.stack.imgur.com/S1m47.png)
[Answer]
# Java - 180
Pretty straightforward, just drawing boxes.
```
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();}
```
![enter image description here](https://i.stack.imgur.com/xDGG2.png)
With line breaks:
```
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();
    }
```
[Answer]
# BBC BASIC, 57
```
FORn=51TO2STEP-1VDU19;-1,n,n,n:RECTANGLEFILL0,0,n*20NEXT
```
The squares have to be *at least* 20 pixels but there's nothing to say they have to be the same size, so I guess this qualifies.
It may be possible to shorten this by using more low-level VDU codes. But changing from this pattern to a less interesting one is unlikely to make it shorter.
![enter image description here](https://i.stack.imgur.com/IVbRk.png)
[Answer]
# ipython 2, 54 bytes
The best I could do was with an ipython program. Starting ipython with `ipython -pylab` and executing this program will show a graph with 50 shades of grey in 50 squares:
```
t=arange(50);c=t/50.;scatter(t%5,t,500,zip(c,c,c),'s')
```
Output:
![grey50](https://i.stack.imgur.com/3Mnga.png)
### Solution 2:
Here is another program (61 bytes):
```
imshow(arange(50).reshape(5,10)*.02,'Greys',None,1,'nearest')
```
that displays the following window:
![grey50b](https://i.stack.imgur.com/NpIkc.png)
[Answer]
# JavaScript (72, 70, 67 bytes)
72: original (borrowing the clever `&#9632;` usage from [Sphinxxx's answer](https://codegolf.stackexchange.com/a/45743/36910)):
```
for(i=90;i>40;)document.write('<font color=#'+i+i+i--+' size=7>&#9632;')
```
70: stealing shamelessly from [Ismael Miguel](https://codegolf.stackexchange.com/a/45767/36910) (who deserves the votes), a shorter for loop and allowing the browser to correct for a lack of # proceeding the colour code...
```
for(i=60;--i>9;)document.write('<font color='+i+i+i+' size=7>&#9632;')
```
67 bytes (65 chars): swapping the entity for the unicode character:
```
for(i=60;--i>9;)document.write('<font color='+i+i+i+' size=7>■')
```
[Answer]
## C++, ~~98~~ 83 characters
Okay so, you are going to need a terminal emulator that supports true colors. For mac os there are the nightly builds of iTerm2, for example. For unix I believe konsole supports it. Here's a list, just in case: <https://gist.github.com/XVilka/8346728#now-supporting-truecolor>
Compile with g++ (\e is compiler dependent).
For the squares to have enough area, you will have to change your terminal font size.
```
#include <cstdio>
int main(){for(int i=51;i--;)printf("\e[48;2;%i;%i;%im ",i,i,i);}
```
Using C libraries instead of C++ allows for shaving some characters.
![](https://i.stack.imgur.com/IC4rE.png)
## C, ~~61~~ 58 characters
This is the same program but in C, which assumes untyped function declarations to return int, and which doesn't need includes for some of the standard library functions, like in this case, printf. Suggested by @cartographer. Three characters less thanks to @user3601420
```
main(i){for(i=51;i--;)printf("\e[48;2;%i;%i;%im ",i,i,i);}
```
Compiles with a couple of warnings (for assuming main's return type to be int and for implicitly including the declaration of printf).
[Answer]
## J (~~43~~ ~~41~~ ~~40~~ ~~38~~ ~~33~~ 29 characters)
After suggestions from [randomra](https://codegolf.stackexchange.com/users/7311/randomra).
```
1!:2&4'P2 ',":(,*:,],*:#?~)50
```
This solution does not display anything as the standard library of J does not contain GUI routines. Instead, I generate an image and write it to standard output. The output is randomized, it ensured that all shades are distinct. Here is a some [sample output](http://fuz.su/~fuz/pic/50sog.pgm) and the image converted to png and rotated by 90° for easier viewing:
![50 shades of gray](https://i.stack.imgur.com/SsO6A.png)
[Answer]
# Piet- 2\*103 = 206
This is unique in that the source code and the output fulfill the challenge:
![piet](https://i.stack.imgur.com/6S1ap.png)
Most of it is decorative, so I only counted the top two rows, which stand alone just fine. Outputs a grayscale ppm like some others have done.
[Answer]
# Python 2, ~~125~~ ~~115~~ 104 bytes
```
from turtle import*
shape("square")
bk(500)
ht()
pu()
exec'color((xcor()/2e3+.5,)*3);stamp();fd(21);'*50
```
**Sample output:**
![enter image description here](https://i.stack.imgur.com/uuKd9.png)
Good old turtle. This was the only method I could think of in Python which prints images to a screen using only the standard libraries.
The square turtle shape just happens to be 21 by 21, which is very convenient. This can be seen by digging through `turtle.py`:
```
"square" : Shape("polygon", ((10,-10), (10,10), (-10,10), (-10,-10)))
```
If you have a high res monitor or don't mind a few squares being off-screen, then this is **93 bytes:**
```
from turtle import*
shape("square")
ht()
pu()
exec'color((xcor()/2e3,)*3);stamp();fd(21);'*50
```
---
## Demo
*(This demo uses [Skulpt](http://www.skulpt.org) by adapting the snippet found [here](https://codegolf.stackexchange.com/a/40127/21487). It is only included to give an idea as to what the code does — I had to modify a few bits to get it working.)*
```
function out(a){var b=document.getElementById("output");b.innerHTML+=a}function builtinRead(a){if(void 0===Sk.builtinFiles||void 0===Sk.builtinFiles.files[a])throw"File not found: '"+a+"'";return Sk.builtinFiles.files[a]}
$(document).ready(function run(){Sk.canvas="canvas";Sk.configure({output:out,read:builtinRead});try{Sk.importMainWithBody("<stdin>",!1,'import turtle\nt=turtle.Turtle()\nt.speed(9)\nt.shape("square")\n\nt.bk(500)\nt.ht()\nt.pu()\nfor i in range(50):t.color(64+2*i,64+2*i,64+2*i);t.stamp();t.fd(21)')}catch(a){throw Error(a.toString());}})
```
```
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script><script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript"></script><script src="http://www.skulpt.org/static/skulpt-stdlib.js" type="text/javascript"></script>
<canvas height="100" width="1200" id="canvas" style="border:1px solid gray">Your browser does not support HTML5 Canvas!</canvas>
```
[Answer]
# Excel vba, 81
In the immediate window:
```
for i=1 to 9:for j=1 to 9:k=i+j*10:cells(i,j).interior.color=rgb(k,k,k):next:next
```
on the active sheet:
![81 shades is just as much code as 50](https://i.stack.imgur.com/12JEa.png)
{81 shades is just as much code as 50}
[Answer]
# **R**, ~~37~~ 32 characters1:
```
image(t(1:50),c=gray.colors(50))
```
However, the display for this looks … meh (image rotated 90° to fit better into text; compressed, original square side length > 20px):
![boring](https://i.stack.imgur.com/d3i0D.png)
Here’s a nicer version which plots the squares in random order inside a matrix (50 characters):
```
image(matrix(sample(1:50),10),c=gray.colors(50))
```
![exciting!](https://i.stack.imgur.com/mC3pP.png)
(And yes, the three adjacent squares of seemingly identical colours are actually three different shades of grey.)
1 Yes, another R answer, ~~and not even the shortest~~ (now the shortest for R) – apologies. I was missing a *pretty* rendering in R.
[Answer]
**Matlab 35 thanks to sanchises**
```
imshow(int8(ones(20,1)*(0:.05:51)))
```
**Matlab 36**
```
imshow(uint8(ones(20,1)*(0:.05:51)))
```
Displays 50 squares filled with gray values 1 to 50 and 2 additional rectangles filled with gray value 0 and 51:
![enter image description here](https://i.stack.imgur.com/lZbmX.png)
**Matlab, 45**
```
imshow(uint8(ones(20,1)*floor(1:0.05:50.95)))
```
[Answer]
# JavaScript, 87 90 bytes
```
for(i=90;i>40;)document.write('<p style=background:#'+i+i+i--+';height:9ex;width:9ex>')
```
This outputs squares to the HTML, because it is really JavaScript's only method of graphical output. Thanks to edc65 for shaving off 3 bytes.
[Answer]
# PHP, ~~96~~ ~~88~~ 74 bytes
Thanks to Ismael Miguel for saving 22 bytes!
```
for($i=60;--$i>9;)echo"<p style=background:#$i$i$i;height:9mm;width:9mm>";
```
Writes 50 `<p>`s with 9mm as height and width (which should be bigger than 20px on the majority of screens), stacked on each other, all with a minor difference in shade.
Generated result (the image is zoomed out and 90° rotated):
![Result](https://i.stack.imgur.com/3K4oE.png)
[Answer]
# Matlab - 33 bytes
```
surf(meshgrid(1:7)),colormap gray
```
Despite the verbose commands, the result is still quite compact. It looks a bit better if you replace `surf` with `surface` but that costs 3 chars.
If you don't notice the 50th gray square that is because it is a bit bigger than the others!
[Answer]
# Javascript (~~77~~ 71 bytes):
This answer is no longer IE11 based. It works because the browser converts the bad color codes into usable ones (by adding `#` to the number).
Here is the code:
```
for(i=59;i-->9;)document.write("<hr color="+i+i+i+" size=20 width=20>")
```
Firefox still has the quirk of making circles.
(Small edit: Changed `for(i=61;i-->9;)` to `for(i=59;i-->9;)` because it was producing 52 squares)
---
**Old answer:**
This is based on [my PHP answer](https://codegolf.stackexchange.com/a/45752/14732), and uses almost the same ways to make the squares:
```
for(i=51;i--;)document.write("<hr color=rgb("+[i,i,i]+") size=20 width=20>");
```
Not the shortest answer, but it's worth adding here.
---
This solution **isn't compatible** with Chrome (produces multiple green colors) and Firefox (same result, but produces circles? o.O WTH?)
Since this is code-golf, a solution working with a specific version or a specific software is acceptable.
[Answer]
# JavaScript - 70 bytes
```
for(i=51;i-=1;)document.write('<a style=opacity:'+i/50+'>&#9632;</a>')
```
[Answer]
**Processing, 66 characters:**
```
size(1000,20);for(int i=0;i<981;i+=20){fill(i/4);rect(i,0,20,20);}
```
![gray shades in Processing](https://i.stack.imgur.com/khctc.png)
[Answer]
# PHP (~~62~~ ~~60~~ ~~63~~ ~~72~~ ~~71~~ ~~62~~ 59 bytes):
I've made the shortest I could find:
```
for($i=59;$i-->9;)echo"<hr color=$i$i$i size=20 width=20>";
```
This is **exactly** the same code as [my Javascript answer (with 71 bytes)](https://codegolf.stackexchange.com/a/45767/14732).
But **SO MUCH** shorter!
This have a quirk in Firefox: instead of squares, Firefox makes **circles**! (Go insanity!)  
Othen than that, all the browsers produce the right color (even with the missing `#` on the `color` attribute).
---
**Invalid submits and changelog**:
Old answer, with 62 bytes.
```
while($i++<51)echo"<hr color=rgb($i,$i,$i) size=20 width=20>";
```
All shades are perfectly aligned.
This solution won't work for Chrome (will produce squares with multiple colors) and Firefox (which makes circles o.O Yes, **circles** with `<hr>`! Horizontal ruler is a circle on Firefox!).  
You can test it on IE11 and it should be fine.
---
(simply removed from above)
I've noticed something awful: It was outputting `#111` instead of `#010101` for some colors.
This meant that some colors were repeating.
Therefore, I had to sacrifice a few bytes:
```
<?while(f>$c=dechex(65-$i++))echo"<hr color=#$c$c$c size=20 width=20>";
```
Outputs the 50 shades in perfect order, from lighter to darker.
Due to a correct point made by [@edc65](https://codegolf.stackexchange.com/users/21348/edc65), I had to add `width=20`, to produce **perfect squares**.
Reduced one more byte by replacing `f!=$c` with `f>$c`, which works perfectly.
---
Final version, without `width=20`:
```
<?while(f!=$c=dechex(65-$i++))echo"<hr color=#$c$c$c size=20>";
```
This is invalid because it is required to output squares.  
This outputs the shades from lighter to darker.
---
Reduced 1 byte by switching to a `while` loop, like this:
```
<?while($c=dechex(50-$i++))echo"<hr color=#$c$c$c size=20>";
```
First solution:
```
<?for($i=50;$c=dechex($i--);)echo"<hr color=#$c$c$c size=20>";
```
This doesn't have the shades neatly ordered, but there are 50 different ones.
There is nothing stating that they must be ordered, but there is a requirement on size.
I hope I can reduce it a lot more.
You can test it on <http://writecodeonline.com/php/> (click on "Display as HTML").
[Answer]
# PostScript, 34 bytes
In ISO-Latin-1 encoding:
```
20’›‡üˆì0{’81e3’6’–10’k0ˆ                   (note, this is a Line Feed)
’c’§}’H
```
This is the binary equivalent to:
```
20 setlinewidth 1020 -20 0 { dup 1e3 div setgray 10 moveto 0 10 lineto stroke } for
```
(This assumes that you are using it on a device that is at least 1000 points [= 10 inches = 25.4 cm] wide.)
[Answer]
# CSS (87 chars)
Uses animation ;)
```
:after{content:'\25A0';font:9em z;animation:g 8s steps(50)}@keyframes g{to{color:#fff}}
```
The runnable version is longer since Webkit still needs the vendor prefix. :(
```
:after{content:'\25A0';font:9em z;-webkit-animation:g 8s steps(50);animation:g 8s steps(50)}@-webkit-keyframes g{to{color:#fff}}@keyframes g{to{color:#fff}}
```
If we're allowed a random, empty `<a>` element, we can get it down to just 72 characters:
```
a{padding:3em;animation:g 8s steps(50)}@keyframes g{to{background:#000}}
```
See? CSS is a legit programming language!
[Answer]
# R, 36 bytes
I can make freekvd's answer shorter trivially.
```
x=9:59/59;symbols(x,sq=x,bg=gray(x))
```
![Output](https://i.stack.imgur.com/dWDah.png)
(Sorry for not making a comment under his solution. Stackexchange does not allow me to make comments, vote etc yet).
[Answer]
# DrRacket - 83
Just for the sake of firing up DrRacket 1 year after "Introduction to Systematic Program Design".
**EDIT**:
```
(require htdp/image)(map(λ(i)(rectangle 50 50'solid(make-color i i i)))(range 50))
```
[Answer]
# C# - 173
**Using LINQPAD**
```
var b=new Bitmap(1000,20);var g=Graphics.FromImage(b);for(int i=0;i<50;i++)g.FillRectangle(new SolidBrush(Color.FromArgb(i*5,i*5,i*5)),new Rectangle(i*20,0,20,20));b.Dump();
```
**Expanded**:
```
var b = new Bitmap(1000, 20);
var g = Graphics.FromImage(b);
for (int i = 0; i < 50; i++)
    g.FillRectangle(new SolidBrush(Color.FromArgb(i * 5, i * 5, i * 5)), new Rectangle(i * 20, 0, 20, 20));
b.Dump();
```
**Output**:
![output](https://i.stack.imgur.com/u2oX7.png)
[Answer]
#  JavaScript, 152 bytes.
Okay, so maybe this isn't the best solution but here is mine.
```
function b(a){c=document
e=c.createElement('p')
with(e.style){background="rgb("+[a,a,a]+")"
width=height="20px"}c.body.appendChild(e)
a<245&&b(a+5)}b(0)
```
Here is the output with css float left (just so it is easier to count the squares) and without: 
![with css float](https://i.stack.imgur.com/RB6YH.png)
![without css float](https://i.stack.imgur.com/DjmPK.png)
]