Implement countLuckyNumbers(int) method. Also perform some refactoring.