Академический Документы
Профессиональный Документы
Культура Документы
Background
Sieve theory is a set of general techniques in number theory, designed to count, or more realistically to
estimate the size of, sifted sets of integers. The primordial example of a sifted set is the set of prime
numbers up to some prescribed limit X. Correspondingly, the primordial example of a sieve is the sieve of
Eratosthenes, or the more general Legendre sieve. The direct attack on prime numbers using these
methods soon reaches apparently insuperable obstacles, in the way of the accumulation of error terms. In
one of the major strands of number theory in the twentieth century, ways were found of avoiding some of
the difficulties of a frontal attack with a naive idea of what sieving should be.
One successful approach is to approximate a specific sifted set of numbers (e.g. the set of prime
numbers) by another, simpler set (e.g. the set of almost prime numbers), which is typically somewhat
larger than the original set, and easier to analyze. More sophisticated sieves also do not work directly with
sets per se, but instead count them according to carefully chosen weight functions on these sets (options
for giving some elements of these sets more "weight" than others). Furthermore, in some modern
applications, sieves are used not to estimate the size of a sifted set, but to produce a function that is large
on the set and mostly small outside it, while being easier to analyze than the characteristic function of the
set.
Examples:
Sieve of Eratosthenes
Procedure:
• To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:
• Strike from the list all multiples of p less than or equal to n. (2p, 3p, 4p, etc.)
• Find the first number remaining on the list after p (this number is the next prime); replace p with
this number.
int currentPrime;
int currentNumber;
do {
currentPrime = primes.get(currentPrimeIndex);
try {
int i = currentPrimeIndex + 1;
currentNumber = primes.get(i);
if(currentNumber % currentPrime == 0) {
primes.remove(i);
continue;
i++;
}
} catch(IndexOutOfBoundsException iobe) {
currentPrimeIndex++;
return primes;
Analysis
The time complexity of this algorithm is O(nlog(log(n))), which is the sum of reciprocals of primes
up to n.
Woman and Eggs Medieval Puzzle
There is a medieval puzzle about an old woman and a basket of eggs. On her way to market, a horseman
knocks down the old woman and all the eggs are broken. The horseman will pay for the eggs, but the
woman does not remember the exact number she had, only that when she took the eggs in pair, there
was one left over; similarly, there was one left over when she took them three, four, five, or six at a
time. When she took them seven at a time, however, none were left.
Java code
int eggs = 7;
while(!(eggs % 2 == 1
&& eggs % 3 == 1
&& eggs % 4 == 1
&& eggs % 5 == 1
return eggs;
Analysis:
The time complexity of this algorithm is O(1) because there is no input n, and performs the same
calculations every time it is run.