Primes in a number
Another contrived math problem. This one I think is actually pretty hard. It's got detecting primes, string manipulation, and combinations.
Your task is to write a function that takes an integer and finds all primes that are substrings of the decimal digits of that integer.
Examples
(find-primes 2) ;=> [2]
(find-primes 22) ;=> [2 2]
(find-primes 717) ;=> [7 7 17 71]
(find-primes 1) ;=> []
(find-primes 44) ;=> []
Notes:
- Return the primes in ascending order.
- If a prime appears more than once, it should be in the returned sequence that many times.
Thanks to this site for the problem idea, where it is rated Very Hard in Java. The problem has been modified.
Please submit your solutions as comments on this gist.
To subscribe: https://purelyfunctional.tv/newsletter/
Sorry for the long-winded solution - will study the very concise and elegant examples above.
In trying for a speedup I used a sieve upfront, windowing for combining digits, and a sorted-in-place vector.