Sieve of Eratosthenes (素数筛选算法)

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, 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 10 million or so (Ref Wiki).

Following is the algorithm to find all the prime numbers less than or equal to a given integer n by 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 up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will 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 that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

When the algorithm terminates, all the numbers in the list that are not marked are prime.

Explanation with Example:
Let us take an 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 will mark all the numbers which are divisible by 2.

Now we move to our next unmarked number 3 and mark all the numbers which are 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 will look like below:

So the prime numbers are 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 two prime numbers

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

NOTE: A solution will 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 two prime numbers.

Note: The number would always be an even number.

Output:

For every test case print two prime numbers space separated, such that the smaller number appears first. Answer for each test case must be 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

import java.util.*;
import java.lang.*;
import java.io.*; class GFG { public static void func(int n) { boolean[] prime = new boolean[n+1];
for(int i=2; i<=n; ++i) {
prime[i] = true;
} for(int p=2; p*p<=n; ++p) {
if(prime[p]) {
for(int k=2*p; k<=n; k+=p) {
prime[k] = false;
}
}
} ArrayList<Integer> rs = new ArrayList<Integer> ();
for(int i=2; i<=n; ++i) {
if(prime[i]) {
rs.add(i);
}
} for(int i=0; i<rs.size(); ++i) {
int first = rs.get(i);
int second = n - first;
if(prime[first] && prime[second]) {
System.out.println(first + " " + second);
break;
}
}
} public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt(); for(int i=0; i<t; ++i) {
int n = in.nextInt();
func(n);
}
}
}

algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )的更多相关文章

  1. Algorithm: Sieve of Eratosthenes

    寻找比n小的所有质数的方法. 2是质数, 2*i都是质数,同样3是质数,3*i也都是质数 代码如下 int n; vector<, true); prime[] = prime[] = fals ...

  2. UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers

    题意: 给出n,求把n写成若干个连续素数之和的方案数. 分析: 这道题非常类似大白书P48的例21,上面详细讲了如何从一个O(n3)的算法优化到O(n2)再到O(nlogn),最后到O(n)的神一般的 ...

  3. 使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp)

    Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime ...

  4. 埃拉托色尼筛法(Sieve of Eratosthenes)求素数。

    埃拉托色尼筛法(Sieve of Eratosthenes)是一种用来求所有小于N的素数的方法.从建立一个整数2~N的表着手,寻找i? 的整数,编程实现此算法,并讨论运算时间. 由于是通过删除来实现, ...

  5. [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】

    拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...

  6. [Algorithm] Finding Prime numbers - Sieve of Eratosthenes

    Given a number N, the output should be the all the prime numbers which is less than N. The solution ...

  7. 素数筛选法(prime seive)

    素数筛选法比较有名的,较常用的是Sieve of Eratosthenes,为古希腊数学家埃拉托色尼(Eratosthenes 274B.C.-194B.C.)提出的一种筛选法.详细步骤及图示讲解,还 ...

  8. 新疆大学(新大)OJ xju 1009: 一带一路 prim求最短路径+O(n)素数筛选

    1009: 一带一路 时间限制: 1 Sec  内存限制: 128 MB 题目描述 一带一路是去去年习大大提出来的建设“新丝绸之路经济带”和“21世纪海上丝绸之路”的战略构想.其中就包括我们新疆乌鲁木 ...

  9. “计数质数”问题的常规思路和Sieve of Eratosthenes算法分析

    题目描述 题目来源于 LeetCode 204.计数质数,简单来讲就是求"不超过整数 n 的所有素数个数". 常规思路 一般来讲,我们会先写一个判断 a 是否为素数的 isPrim ...

随机推荐

  1. 甲骨文推出MySQL Fabric,简化MySQL的高可用性与可扩展性

    北京,2014年5月28日——为了满足当下对Web及云应用需求,甲骨文宣布推出MySQL Fabric.MySQL Fabric是一款可简化管理MySQL数据库群的整合式系统.该产品通过故障检测和故障 ...

  2. itnesse实现api接口自动化测试学习

    上午在园子里乱逛,看了不少小伙伴们分享的接口测试方面的知识,大家所叙述到的一些经验或多或少,我也曾遇到过,突然意识到知识的点滴积累是多么的重要,我记得我最早接触接口测试的时候,就是只在浏览器里人工测试 ...

  3. 单元测试之道(使用NUnit)

    首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试……终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而 又忐忑的心情点击界面上 ...

  4. 3D volume texture和cube map

    cube map texture可以理解为6个面的纸盒, sample的时候使用vector射线型的sample. volume texture可以理解是一摞2D texture,sample的时候用 ...

  5. gulp some tips

    gulp作为替代grunt的task runner后起之秀,基于nodejs的stream操作模型,大大减少了对磁盘的操作因此大大提高了性能. gulp error handling var gulp ...

  6. nginx日志切割并使用flume-ng收集日志

    nginx的日志文件没有rotate功能.如果你不处理,日志文件将变得越来越大,还好我们可以写一个nginx日志切割脚本来自动切割日志文件.第一步就是重命名日志文件,不用担心重命名后nginx找不到日 ...

  7. IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)

    一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...

  8. OGNL valueStack StackContext(ActionContext)深入分析(转+个人理解)

    //还会补充 首先要有一个意识 ,为什么要了解这个?: struts2中的表单是怎么通过表达式(EL or OGNL)来传给Action 和 拿到Action的值的. 值栈(根)对象也可以直接使用EL ...

  9. ecshop 分类树全部显示

    1.在page_header.lbi文件中加入 $GLOBALS['smarty']->assign('categories',get_categories_tree()); // 保证首页页面 ...

  10. typedef 深入剖析

    typedef是一个我们常常会用到的关键字,而这个关键字有许多陷阱或者说许多不为我们深入理解的地方.很多书上都是很简单地一笔代过,并没有真正地让我们理解这个关键字.本文对其进行详细地说明.综合网络上找 ...