Вы находитесь на странице: 1из 3

Sieves

Background

In short, sieving is a technique for counting or filtering sets of numbers.

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:

• Create a list of consecutive integers from two to n:


(2, 3, 4, ..., n).

• Initially, let p equal 2, the first prime number.

• 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.

• Repeat steps 3 and 4 until p2 is greater than n.

• All the remaining numbers in the list are prime


Java code

public static ArrayList<Integer> getPrimes(ArrayList<Integer> integers) {

ArrayList<Integer> primes = integers;

int currentPrime;

int currentNumber;

int currentPrimeIndex = primes.indexOf(2); // start from 2

int last = primes.get(primes.size()-1); // biggest number in the list

do {

currentPrime = primes.get(currentPrimeIndex);

try {

int i = currentPrimeIndex + 1;

while(i < primes.size()) {

currentNumber = primes.get(i);

if(currentNumber % currentPrime == 0) {

primes.remove(i);

continue;

i++;

}
} catch(IndexOutOfBoundsException iobe) {

// exception is used to break out of loop.

currentPrimeIndex++;

} while(Math.pow(currentPrime,2) <= last);

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

public static int findEggs() {

int eggs = 7;

while(!(eggs % 2 == 1
&& eggs % 3 == 1

&& eggs % 4 == 1

&& eggs % 5 == 1

&& eggs % 6 == 1))

eggs += 7; // because we know that eggs%7 == 0

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.

Вам также может понравиться