– as 8, 9 and 10 are no longer in the list, remove 3 × 11, 3
2
× 11, 3
3
× 11, …., up to 3
r
× 11 for
the largest r with 3
r
× 11 ≤ n,
– …
• …
We stop when the next number in what remains in the list exceeds b
√
nc.
Let us verify that the procedure is correct. At stage k, all proper multiples of the kth prime number
p
k
at most equal to n are removed from the list. is is veried by induction. Indeed, if during stage k, a
number m in {2, 3, . . . , n} with p
k
m ≤ n is not considered then:
• either m is smaller than p
k
, in which case it is a multiple of at least one of p
1
, …, p
k−1
, which implies
that p
k
×m is also a multiple of at least one of p
1
, …, p
k−1
, so by inductive hypothesis, p
k
×m was
removed from the list during one of the previous stages,
• or m is greater than p
k
but no longer belongs to the list (it is a number such as 4, 6, 8 at stage 1, or
a number such as 4, 6, 8, 9, 10 at stage 2), in which case:
– either m was removed during one of the previous stages, hence m is a multiple of at least one
of p
1
, …, p
k−1
, which implies as in the previous case that p
k
× m was also removed from the
list,
– or m is a multiple of p
k
which was removed earlier in the current stage, so m is a number of
the form p
r
k
m
0
for some r ≥ 1 and some number m
0
which was then found in what remained
of the list, therefore p
k
m = p
r+1
k
m
0
was also removed from the list earlier in the current stage.
To observe how, with n set to 25, nonzero powers of 2 times 2, 3, 5, 7, 9 and 11, nonzero powers of 3
times 3, 5 and 7, and nonzero powers of 5 times 5 are eliminated, the following function is successively
called with i set to 0 and k set to 0, 1, 2, 3, 4 and 5 as arguments, then with i set to 1 and k set to 0, 1 and
2 as arguments, then with i set to 2 and k set to 0 as arguments.
In [7]: def eliminate_proper_multiples(i, k):
# We assume that this function will be called in the order
# eliminate_proper_multiples(0, 0)
# eliminate_proper_multiples(0, 1)
# ...
# eliminate_proper_multiples(1, 0)
# eliminate_proper_multiples(1, 1)
# ...
print('Now eliminating multiples of the form '
f'a nonzero power of {sieve[i]} times {sieve[i + k]}'
)
factor = sieve[i] * sieve[i + k]
power = 1
while factor <= n:
print(' Eliminating '
f'{sieve[i]}^{power} x {sieve[i + k]} = {factor}'
)
sieve.remove(factor)
factor *= sieve[i]
power += 1
3
评论0
最新资源