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

假设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"
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 8,978评论 0 2
  • 一、实验目的 学习使用 weka 中的常用分类器,完成数据分类任务。 二、实验内容 了解 weka 中 explo...
    yigoh阅读 12,774评论 5 4
  • [幸福路人 宋艳俊 平顶山郏县 坚持分享第136天 2017.12.26] 最近这段时间确实有太多工作要...
    简单男孩阅读 2,849评论 0 0
  • 太多的路 充满回忆 抓不住流走沙 拼命地 拼命地 拿着仅剩的青春 独自舔伤 远方的风景 抱着写诗的心 踏实现在的生活
    萧臻彬阅读 1,450评论 0 0
  • 看了一下午书,原本阴郁的心情开始慢慢好转。伸个懒腰,舒展下双臂,手机里刚好播放到薛之谦的意外,那句:明知道这只是一...
    11点姑娘阅读 6,156评论 12 18

友情链接更多精彩内容