简体   繁体   中英

Cannot find truncation prime by using python

I tired to use python to find the smallest right-truncable prime in specific digits. The code work well when I tried to find the smallest right-truncable prime in 5 digits. However, when I tried to find that in 6 digits, python exit unexpectedly.

Here is my code:

    def findRTP(digits, currNum = 2):
    if digits == len(str(currNum)):return currNum
    else:
        # if digit % 2 == 0 or digit == 5:
        #     return findRTP(digits, currNum, digit+1)
        # elif (digitSum(currNum)+digit)%3 == 0:
        #     return findRTP(digits, currNum, digit+1)
        for digit in range(1, 10):
            currNum = currNum * 10 + digit
            if isPrime(currNum):
                result = findRTP(digits, currNum)
                if result != None: return result
                else: currNum //=10
            else:
                if digit >= 9:
                    currNum //= 10
                    return findRTP(digits, currNum//10)
                currNum //= 10
        return None

# def digitSum(num):
#     if num == 0:
#         return 0
#     else:
#         return num % 10 + digitSum(num // 10)

def isPrime(n):
    if (n < 2):
        return False
    if (n == 2):
        return True
    if (n % 2 == 0):
        return False
    maxFactor = round(n**0.5)
    for factor in range(3,maxFactor+1,2):
        if (n % factor == 0):
            return False
    return True

def callWithLargeStack(f,*args):
    import sys
    import threading
    threading.stack_size(2**27)  # 64MB stack
    sys.setrecursionlimit(2**27) # will hit 64MB stack limit first
    # need new thread to get the redefined stack size
    def wrappedFn(resultWrapper): resultWrapper[0] = f(*args)
    resultWrapper = [None]
    #thread = threading.Thread(target=f, args=args)
    thread = threading.Thread(target=wrappedFn, args=[resultWrapper])
    thread.start()
    thread.join()
    return resultWrapper[0]

print(callWithLargeStack(findRTP, 6))

Please help me with it.

When this program is run, it terminates with an exit status of 138, which means that it was terminated by a SIGBUS signal (128 + 10 for signal #10).

In this case, that is because it exceeded the thread's stack size. You can reproduce this with the 5-digit version, too: change the stack size to 2**16.

threading.stack_size(2**16)  # 64MB stack
...
print(callWithLargeStack(findRTP, 5))

On my system, modifying the program this way yielded Process finished with exit code 138 , just like the 6-digit version.

The short answer here is that recursion may simply not be the best approach to what you are trying to do. It's trivial to replace isPrime by a non-recursive version, and with some work you may be able to do the same with findRTP also.

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