美文网首页
已知两个正整数的和与积求这两个数

已知两个正整数的和与积求这两个数

作者: 破旧的大卡车 | 来源:发表于2019-02-09 19:22 被阅读8次

    假设m,n是两个大于等于2的正整数, A知道和m+n, B知道乘积m*n; 一开始:
    B: A你知道这两个数吗?
    A: 不知. 你知道吗?
    B: 不知.
    A: 现在我知道了.
    B: 现在我也知道了.

    试问, m,n分别为多少?

    #!/usr/bin/python
    # coding=utf-8
    
    import math
    # check n is a prime or not
    def is_prime(n):
        if n % 2 == 0 and n > 2: 
            return False
        return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
    #print is_prime(10)
    #print is_prime(11)
    
    # factors of an integer
    def factors(n):
        return [ i for i in range(2, n+1) if not n%i]
        #since no factors between n/2 and n
        #return [i for i in range (2, n//2+1) if not n%i]+[n]
    #print factors(12)
    
    # decompose an integer by product of two number
    def two_factors_decompose(n):
        return [[i,n/i] for i in range(2, int(math.sqrt(n))+1) if not n%i ]
    #print two_factors_decompose(12)
    
    # prime factors decomposition of an integer
    def prime_factors_decompose(n):
        factors_list=[]
        for i in range(2, n+1):
            if is_prime(i):
                nn=n
                count=0
                while(not nn%i):
                    count+=1
                    nn=nn/i
                if count>0:
                    factors_list.append([i,count])
        return factors_list
    #print prime_factors_decompose(15)
    #print prime_factors_decompose(100)
    #print len(prime_factors_decompose(27))
    
    # Summation decompose of an integer
    def sumation_decompose(n):
        return [ [i,n-i] for i in range(2, n//2+1)]
    #print sumation_decompose(10)
    #print sumation_decompose(13)
    
    # Condition A is not known
    # This condition is equal to say the prime decompose of a is not of the form
    #   p*q, p*p, p*p*p
    def condition_A_unknown(prime_factors_list):
        if len(prime_factors_list)==1:
            if prime_factors_list[0][1]==2 or prime_factors_list[0][1]==3:
                return False
        if len(prime_factors_list)==2:
            if prime_factors_list[0][1]==1 and prime_factors_list[1][1]==1:
                return False
        return True
    # Condition B is known
    def condition_B_known(summation_decompose_list):
        # This condition means all but one of the summation decompose
        #   bm,bn with bm+bn=b must satisfy "A is not known" on the first ground
        for bm,bn in summation_decompose_list:
            bmn=bm*bn
            #print bmn,"factors decomposition", prime_factors_decompose(bmn)
            if not condition_A_unknown(prime_factors_decompose(bmn)):
               summation_decompose_list.remove([bm,bn])
        #print summation_decompose_list
        if len(summation_decompose_list)==1:
            return True
        else:
            return False
    
    # given two positive number m and n
    # assume that m and n are less than maxmn
    maxmn=10
    for m in range(2, maxmn+1):
        for n in range(m, maxmn+1):
            a=m*n
            b=m+n
            # B is not known
            # This condition is equal to say the length of summation decompose of b>=2
            if len(sumation_decompose(b))<2:
                #print m,n,1
                continue
            # A is not known
            prime_factors_list=prime_factors_decompose(a)
            if not condition_A_unknown(prime_factors_list):
                #print m,n,2
                continue
            #   and since B is not known, the number of possible pairs of two-integer
            #   product decompose are >=2
            if len(factors(a))<2:
                #print m,n,3
                continue
            # Now we continue the second round
            # B is known
            summation_decompose_list=sumation_decompose(b)
            if not condition_B_known(summation_decompose_list):
                #print m,n,4
                continue
            # A is known
            two_factors_list=two_factors_decompose(a)
            #print prime_factors_list
            for am,an in two_factors_list:
                amn=am+an
                if not condition_B_known(sumation_decompose(amn)):
                    #print m,n,5
                    continue
            print m,n,"OK"
    

    相关文章

      网友评论

          本文标题:已知两个正整数的和与积求这两个数

          本文链接:https://www.haomeiwen.com/subject/oarzsqtx.html