Huawei OD-product of prime numbers

 Question description

The RSA encryption algorithm is ubiquitous in the world of network security. It takes advantage of the difficulty of extremely large integer factorization. The larger the data, the higher the security factor. Given a 32-bit positive integer, please factor it and find out is the product of two prime numbers.

Enter description

1. A positive integer num
2. 0 < num <= 2147483647

Output description

1. If found successfully, split by a single space and output two prime numbers from small to large. If the decomposition fails, please output -1 -1

Example 1

enter

15

output

3 5

Code

# coding:utf-8
# 素数之积
import math


class Solution:
    def primeProdcut(self, n):
        s = int(math.sqrt(n))
        res = ''
        flag = False
        for i in range(2, s + 1):
            if n % i == 0:
                if self.is_prime(i) and self.is_prime(n // i):
                    flag = True
                    if i < n // i:
                        res = f"{i} {n // i}"
                    else:
                        res = f"{n // i} {n}"
        if flag:
            return res
        else:
            return "-1 -1"

    def is_prime(self, n):
        if n < 2:
            return False
        for i in range(2, int(math.sqrt(n)) + 1):
            if n % i == 0:
                return False
        return True


if __name__ == '__main__':
    s = int(input("input:"))
    solution = Solution()
    print(solution.primeProdcut(s))

Guess you like

Origin blog.csdn.net/SD_JZZ/article/details/132444945