algorithm@ Sieve of Eratosthenes (Prime selection algorithm) & related problem (Return to prime numbers)

Source: Internet
Author: User

Sieve of Eratosthenes (prime number screening algorithm)

Given a number n, print all primes smaller than or equal to N. It is also given, N is a small number.
For example, if N is ten, the output should be "2, 3, 5, 7″. If N is a, the output should be "2, 3, 5, 7, 11, 13, 17, 19″.

The Sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than mi Llion or so (Ref Wiki).

Following is the algorithm to find all the prime numbers less than or equal to a given integer NBy Eratosthenes ' method:
    1. Create a list of consecutive integers from 2 to n: (2, 3, 4, ..., n).
    2. Initially, let p equal 2, the first prime number.
    3. Starting from P, count on increments of p and mark each of these numbers greater than p Itse LF in the list. These numbers would be 2p, 3p, 4p, etc.; Note that some of them may have already been marked.
    4. Find the first number greater than p in the list so is not marked. If There was no such number, stop. Otherwise, let p now equal this number (which are the next prime), and repeat from step 3.

When the algorithm is terminates, all of the numbers in the list is not marked is prime.

Explanation with Example:
Let us take a example when n = 50. So we need to print all print numbers smaller than or equal to 50.

We Create a list of all numbers from 2 to 50.

According to the algorithm we'll mark all the numbers which is divisible by 2.

Now we move to our next unmarked number 3 and mark all the numbers which is multiples of 3.

We move to our next unmarked number 5 and mark all multiples of 5.

We continue this process and our final table would look like below:

So the prime numbers is the unmarked Ones:2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47.

Related practice problem

http://www.practice.geeksforgeeks.org/problem-page.php?pid=425

Return to prime numbers

Given an even number (greater than 2), return the prime numbers whose sum would be equal to Given number. There is several combinations possible. Print only first such pair.

Note: A solution would always exist, read Goldbach ' s conjecture.

Also, solve the problem in linear time complexity, i.e., O (n).

Input:

The first line contains T, the number of test cases. The following T lines consist of a number each, for which we ll find the prime numbers.

Note:the number would always is an even number.


Output:

For every test case print-numbers space separated, such that the smaller number appears first. Answer for the test case must is in a new line.


Constraints:

1≤t≤70
1≤n≤10000


Example:

Input:

5
74
1024
66
8
9990

Output:

3 71
3 1021
5 61
3 5
17 9973

ImportJava.util.*;Importjava.lang.*;ImportJava.io.*;classGFG { Public Static voidFuncintN) {Boolean[] prime =New Boolean[N+1];  for(inti=2; i<=n; ++i) {Prime[i]=true; }                 for(intp=2; p*p<=n; ++p) {if(Prime[p]) { for(intK=2*p; k<=n; k+=p) {Prime[k]=false; } }} ArrayList<Integer> rs =NewArraylist<integer> ();  for(inti=2; i<=n; ++i) {if(Prime[i]) {rs.add (i); }        }                 for(inti=0; I<rs.size (); ++i) {intFirst =Rs.get (i); intSecond = n-First ; if(Prime[first] &&Prime[second]) {System.out.println ( first+ " " +second);  Break; }        }    }         Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intt =In.nextint ();  for(inti=0; i<t; ++i) {intn =In.nextint ();        Func (n); }    }}
View Code

[email protected] Sieve of Eratosthenes (Prime selection algorithm) & related problem (Return of prime numbers)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.