python计算两个正整数的和差积商_已知两个正整数的和与积求这两个数

假设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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值