Joshua bought a box of pens. 1/8 of the pens were black, 1/4 were blue and 2/5 of the remaining were red. If there were 18 red pens, how many did ahe buy?

Let X be the number of pens Joshua bought.

There were 18*5/2 = <<18*5/2=45>>45 pens remaining after the black and blue pens.
1/8*X + 1/4*X + 45 = X - 18
1/8*X + 1/4*X -X = -45 + 18
1/8*X - 3/4*X = -27
-5/8*X = -27
X = <<54=54>>54. Answer: \boxed{54}.