N is a 7-digit positive integer whose product of the digits is 20160.
Determine the probability that the sum of the digits of N is a perfect square.
Program output:
5 28560 0.00017507002801120448
The required probability is 0.00017507002801120448
The required squares are [1763584, 5184729, 5948721, 7458361, 8317456]
Their square roots are [1328, 2277, 2439, 2731, 2884]
------ code -------
def isSquare(n):
""" Input an integer, Returns True iff it is a perfect square. """
if round(n**0.5)**2 == n:
return True
else:
return False
def pod(n):
""" Input an integer. Returns the Product of the Digits """
aList = list(str(n))
ans = 1
for c in aList:
ans = ans * int(c)
return ans
answers = []
podcount = 0
sqrcount = 0
for i in range(1000000,10000000):
if pod(i) != 20160:
continue
podcount += 1
if isSquare(i):
sqrcount += 1
answers.append(i)
roots = []
for i in answers:
roots.append(int(i**.5))
print(sqrcount, podcount, sqrcount/podcount)
print('The required probability is', sqrcount/podcount)
print('The required squares are', answers)
print('Their square roots are', roots)
|
Posted by Larry
on 2022-08-19 06:48:43 |