(Algebra: perfect square) Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, and 5 in an array list. 2 and 5 appear an odd number of times in the array list. Thus, n is 10.) Here are some sample runs: Enter an integer m: 1500 The smallest number n for m * n to be a perfect square is 15 m * n is 22500
时间: 2023-07-16 22:16:18 浏览: 70
Here's a possible solution in Python:
```python
import math
# Function that returns all the factors of a given number
def factors(n):
result = []
i = 2
while i <= n:
if n % i == 0:
result.append(i)
n = n // i
else:
i += 1
return result
# Prompt the user to enter an integer m
m = int(input("Enter an integer m: "))
# Compute the factors of m
m_factors = factors(m)
# Compute the product of the factors that appear an odd number of times
n = 1
for factor in set(m_factors):
if m_factors.count(factor) % 2 == 1:
n *= factor
# Compute the smallest integer n such that m * n is a perfect square
mn = m * n
sqrt_mn = int(math.sqrt(mn))
while sqrt_mn * sqrt_mn != mn:
mn += m
sqrt_mn = int(math.sqrt(mn))
# Print the result
print("The smallest number n for m * n to be a perfect square is", n)
print("m * n is", mn)
```
Sample output:
```
Enter an integer m: 1500
The smallest number n for m * n to be a perfect square is 15
m * n is 22500
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)