返回题库

偶素数和

Even Prime Sum

专题
Probability / 概率
难度
L3

题目详情

随机均匀地选择两个 1 到 20 之间的不同素数。求它们的和为偶数的概率。

Two distinct prime integers between 1 and 20, inclusive, are selected uniformly at random. Find the probability their sum is even.

解析

我们首先枚举 1 到 20(含)之间的所有素数。我们有 2、3、5、7、11、13、17 和 19。

解决这个问题的主要观察是任何两个素数的和,notnot,包括 2,将是偶数。这是因为除了 2 之外,每个素数都是奇数。任意两个奇数之和将是偶数。

现在我们来求解 P(even)\mathbb{P}(even)。这相当于 1P(odd)1-\mathbb{P}(odd),因为偶数或奇数的结果总和构成了整个结果空间(总和只能是偶数或奇数)。 P(odd)\mathbb{P}(odd) 等于获得奇数和的方法数除以可能的和的总数。

我们可以通过注意到这个特定的和必须包含 2 来计算获得奇数和的方法的数量。因此,我们可以枚举:(2, 3), (2, 5), (2, 7) ... (2, 19)。获得奇数总和的方法一共有 7 种。将其除以可能的和的总数,或 2 个元素的组合,或选择 8 个元素中的 2 个的方式,(82)8 \choose 2 = 28。因此,我们得到 P(odd)=728=14\mathbb{P}(odd) = \frac{7}{28} = \frac{1}{4}。从上面的表达式中减去该值即可得到 P(even)=1P(odd)=114=34\mathbb{P}(even) = 1 - \mathbb{P}(odd) = 1 - \frac{1}{4} = \frac{3}{4}

import random

def simulate_trial():
    primes = [2, 3, 4, 5, 11, 13, 17, 19]
    elts = random.sample(primes, 2)
    if 2 in elts:
        return 0
    return 1

res = []

for i in range(100000):
    res.append(simulate_trial())

print(float(sum(res) / 100000))

Original Explanation

Let's first enumerate all the prime numbers between 1 and 20, inclusive. We have 2, 3, 5, 7, 11, 13, 17, and 19.

The main observation to make to solve this problem is that the sum of any two primes, notnot including 2, is going to be even. This is because each prime, besides 2, is an odd number. And the sum of any two odd numbers is going to be even.

Let's now solve for P(even)\mathbb{P}(even). This is equivalent to 1P(odd)1-\mathbb{P}(odd) since the outcomes sum being even or odd make up the entire outcome space (the sum can only be even or odd). P(odd)\mathbb{P}(odd) is equal to the number of ways to get an odd sum, divided by the total number of possible sums.

We can count the number of ways to get an odd sum by noting that this particular sum must contain 2. Therefore, we can enumerate: (2, 3), (2, 5), (2, 7) ... (2, 19). There are 7 total ways to get an odd sum. Divide this by the total number of possible sums, or combinations of 2 elements, or ways to choose 2 of the 8 elements, (82)8 \choose 2 = 28. Therefore, we get P(odd)=728=14\mathbb{P}(odd) = \frac{7}{28} = \frac{1}{4}. Subtract this from the expression above to get P(even)=1P(odd)=114=34\mathbb{P}(even) = 1 - \mathbb{P}(odd) = 1 - \frac{1}{4} = \frac{3}{4}.

import random

def simulate_trial():
    primes = [2, 3, 4, 5, 11, 13, 17, 19]
    elts = random.sample(primes, 2)
    if 2 in elts:
        return 0
    return 1

res = []

for i in range(100000):
    res.append(simulate_trial())

print(float(sum(res) / 100000))