← Back to challenges

Find All Prime Numbers in Decimal Integer

JavaScriptHardnumbersalgorithmsmathcryptography

Instructions

Create a function that takes an integer argument and returns an array of prime numbers found in the decimal representation of that number (not factors).

For example, extractPrimes(1717) returns [7, 7, 17, 17, 71].

The array should be in ascending order. If a prime number appears more than once, every occurrence should be listed. If no prime numbers are found, return an empty array.

Examples

extractPrimes(1) ➞ []

extractPrimes(7) ➞ [7]

extractPrimes(73) ➞ [3, 7, 73]

extractPrimes(1313) ➞ [3, 3, 13, 13, 31, 131, 313]

Notes

  • All test cases are positive real integers.
  • Some numbers will have leading zeros. For example, the number 103 contains the prime number 3, but also contains 03. These should be treated as the same number, so the result would simply be [3].
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.