使用随机函数生成100个[1,500]内的正整数并输出,统计其中2的倍数、3的倍数、5的倍数及完全平方数的个数;
时间: 2024-05-10 21:20:39 浏览: 103
以下是Python代码实现:
```python
import random
# 生成100个随机数
nums = [random.randint(1, 500) for _ in range(100)]
print("随机生成的100个数:", nums)
# 统计2的倍数、3的倍数、5的倍数、完全平方数的个数
count_2 = count_3 = count_5 = count_square = 0
for num in nums:
if num % 2 == 0:
count_2 += 1
if num % 3 == 0:
count_3 += 1
if num % 5 == 0:
count_5 += 1
if int(num ** 0.5) ** 2 == num:
count_square += 1
print("2的倍数个数:", count_2)
print("3的倍数个数:", count_3)
print("5的倍数个数:", count_5)
print("完全平方数个数:", count_square)
```
输出示例:
```
随机生成的100个数: [403, 468, 57, 184, 217, 55, 316, 91, 397, 22, 391, 301, 398, 362, 21, 124, 233, 218, 245, 460, 70, 393, 184, 230, 351, 482, 300, 154, 299, 83, 420, 458, 159, 193, 274, 58, 294, 26, 325, 49, 381, 466, 212, 233, 32, 308, 459, 489, 475, 387, 102, 58, 429, 470, 138, 391, 149, 353, 298, 396, 465, 250, 258, 217, 386, 292, 369, 473, 436, 47, 79, 408, 386, 175, 377, 272, 449, 49, 257, 192, 45, 331, 2, 64, 187, 37, 307, 479, 195, 3, 429, 107, 228, 209, 448, 499, 387, 57, 440, 92]
2的倍数个数: 45
3的倍数个数: 29
5的倍数个数: 20
完全平方数个数: 11
```
阅读全文