Reputation: 417
I recently came up with question,
Find the nth number that contains the digit k or is divisible by k. (2 <= k <= 9)
Example –
if n = 15 & k = 3
Answer : 33
(3, 6, 9, 12, 13, 15, 18, 21, 23, 24, 27, 30, 31, 32, 33)
But couldn't come up with a good working idea. what would be the best algorithm to solve this.
Upvotes: 1
Views: 236
Reputation: 67467
with haskell
> filter (\x -> hasDigit 3 x || mod x 3 == 0) [1..100]
[3,6,9,12,13,15,18,21,23,24,27,30,31,32,33,34,35,36,37,38,39,42,43,45,48,51,53,5
4,57,60,63,66,69,72,73,75,78,81,83,84,87,90,93,96,99]
and you can define hasDigit as
> hasDigit k n = elem (head $ show k) $ show n
Upvotes: 2