The rule is as follows: 

1. If the numeral ends in _один/одна/одно_, the noun complement is in the singular nominative: _одно яблоко, двадцать одно яблоко_
1. If the numeral ends in _два/две, три, четыре_, the noun complement is in the singular genitive: _два яблока, тридцать три яблока_
1. Otherwise, the noun complement is in the plural genitive: _восемь яблок, пятнадцать яблок, сорок яблок_.

Here's the algorithm: 

* If the last two digits of the number are between 11 to 19, inclusive, then __pl. gen.__, else
* If the last digit of the number is 1, then __sg. nom.__, else
* If the last digit of the number is from 2 to 4, inclusive, then __sg. gen.__, else
* __Pl. gen.__