Skip to content
Advertisement

Split number into the primes inside it

So I have a list of numbers that look like “3723311723” and “13617172343”. As you can see these numbers can be split into prime numbers e.g “3723311723” could be split into 37, 23, 31 ect. How could I write a python program that takes a number like this as an input and returns a list of the primes inside it (in the order they are found)?

Thanks.

Advertisement

Answer

If you know that the numbers are definitely gonna be prime, then you just have to break up the string into substrings of length 2, right?

JavaScript

Output:

JavaScript

EDIT:

This does a recursive search to find non-overlapping primes. If there’s no valid split, it returns False. The usual caveats of recursion apply: if the input is too long, you’ll recurse too deep and the program will blow up. But you can use a stack instead of recursing if that’s an issue.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement