简体   繁体   中英

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 eg "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.

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?

s = "37233141712331719337"
print([int(s[i:i+2]) for i in range(0, len(s), 2)])

Output:

[37, 23, 31, 41, 71, 23, 31, 71, 93, 37]

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.

def is_prime(n):
    for k in range(2, n):
            if n % k == 0:
                    return False
    return True
def primesplit(s, i=0):
    if i+1 >= len(s):
        return []
    if i+2 <= len(s):
        two_digits = int(s[i:i+2])
        if is_prime(two_digits):
            remaining_digits = primesplit(s, i+2)
            if isinstance(remaining_digits, list):
                return [two_digits] + remaining_digits
    one_digit = int(s[i:i+1])
    if not is_prime(one_digit):
        return False
    remaining_digits = primesplit(s, i+1)
    if isinstance(remaining_digits, list):
        return [one_digit] + remaining_digits
    return False
s = "37233141712331719337"
result = primesplit(s)
print(result)
print("".join(map(str, result)) == s)

Output:

[37, 23, 31, 41, 71, 23, 31, 7, 19, 3, 37]
True

This is what you are looking for: The loop finds both 1 and 2 digit prime numbers in the order they appear in the original number and there will be no duplicates too.

In [126]: n
Out[126]: '37233141712331719337'

In [127]: primes = []
     ...: for i in range(len(n)-1):
     ...:     n1 = int(n[i])
     ...:     n2 = int(n[i:i+2])
     ...:     if isPrime(n1):
     ...:         if n1 not in primes:
     ...:             primes.append(n1)
     ...:     if isPrime(n2):
     ...:         if n2 not in primes:
     ...:             primes.append(n2)
     ...:

In [128]: primes
Out[128]: [3, 37, 7, 2, 23, 31, 1, 41, 17, 71, 19]

It's a bit complicated what you are asking for, but here's what I thought you meant: Given a number, look for any two digit prime within that number(I assume they ARE allowed to overlap) A easy way to do that is like this:

def isPrime(number):
    if number>1:
        for i in range(2,number):
            if (number%i)==0:
                return False
        return True
    else:
        return False

    

def getPrimes(number):
    number=str(number)
    retList=[]
    for i in range(len(number)-1):
        temp=int(number[i:i+2])
        if isPrime(temp):
            retList+=[temp]
    return retList

This does NOT check for single digit primes, but that could easily be added if you needed it (simply check every digit in the original number if they are prime).

Is this what you wanted?

Here is how you can use the isprime method from the sympy module, and zip :

from sympy import isprime

n = "37233141712331719337"
lst = [int(i + j) for i, j in zip(n, n[1:]) if isprime(int(i + j))]
print(lst)

Output:

[37, 23, 31, 41, 17, 71, 23, 31, 17, 71, 19, 37]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM