Leetcode_204_Count Primes

本文是在学习中的总结。欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46366207

Description:

Count the number of prime numbers less than a non-negative number, n.

思路:

(1)题意为给定整数n,求解n以内的整数中有多少个素数(质数)。

(2)该题涉及到数学相关的知识。首先。看下素数的定义:质数(prime number)又称素数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其它自然数(质数)整除,换句话说就是该数除了1和它本身以外不再有其它的因数;否则称为合数(来自百度百科);其次,对于100以内的数字。发现一下规律:不被2、3、5整数的数是质数,可是超过100就不成立了,比如像11*11=121这种数,就须要对其能否开方进行判定。最后。这里通过排除法进行操作,将n以内大于2的数所有存入布尔数组,并将其布尔值值为true,然后进行遍历,在遍历过程中,假设遍历的数值为false。则continue;否则,则从当前下标開始。对当前下标值的平方是否在n范围内进行判定,假设在则将当前下标值置为false,直到遍历结束。最后所得数值中值为true的个数即为素数的个数。

(3)详情见下方代码。

希望本文对你有所帮助。该代码为Lettcode官网上的代码,非本人所写。但有必要学习下。

算法代码实现例如以下:

/**
 *
 * @author liqqc
 *
 */
public class Count_Primes {
	public int countPrimes(int n) {
		boolean[] isPrime = new boolean[n];

		for (int i = 2; i < n; i++) {
			isPrime[i] = true;
		}
		// Loop‘s ending condition is i * i < n instead of i < sqrt(n)
		// to avoid repeatedly calling an expensive function sqrt().
		for (int i = 2; i * i < n; i++) {
			if (!isPrime[i])
				continue;
			for (int j = i * i; j < n; j += i) {
				isPrime[j] = false;
			}
		}
		int count = 0;
		for (int i = 2; i < n; i++) {
			if (isPrime[i]){
				count++;
			}
		}
		return count;
	}
}
时间: 2025-01-02 19:09:11

Leetcode_204_Count Primes的相关文章

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

projecteuler Summation of primes

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 译文: 10以下的素数之和为17,求出2000000以下的素数之和. ======================= 第一次code: 1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(

HDU 2161 Primes(打表)

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9888    Accepted Submission(s): 4136 Problem Description Write a program to read in a list of integers and determine whether or not each n

HDU 4715 Difference Between Primes (素数表+二分)

Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2998    Accepted Submission(s): 850 Problem Description All you know Goldbach conjecture.That is to say, Every even inte

[leedcode] Count Primes

Description: Count the number of prime numbers less than a non-negative number, n public class Solution { public int countPrimes(int n) { if(n<3) return 0; int res=0; int temp[]=new int[n]; int i; for(i=2;i<n-1;i++){ temp[i++]=0; temp[i]=1; } temp[2

【数学】Count Primes

题目:leetcode Count Primes Description: Count the number of prime numbers less than a non-negative number, n 分析: 求出比n小的素数的个数,这个问题可以用排除法做,参考http://www.cnblogs.com/grandyang/p/4462810.html int countPrimes(int n) { if(n<=2) return 0; int res=0; int size=s

[LeetCode 204] Count Primes

题目链接:count-primes Description: Count the number of prime numbers less than a non-negative number, n public class Solution { public int countPrimes(int n) { if(n <= 2) return 0; List<Integer> primes = new ArrayList<Integer>(); primes.add(2);

Count Primes Total Accepted: 831 Total Submissions: 6167

题目来自:Leetcode https://leetcode.com/problems/count-primes/ Count Primes Total Accepted: 831 Total Submissions: 6167My Submissions Question  Solution Description: Count the number of prime numbers less than a non-negative number, n Hint: The number n c

POJ 3132 Sum of Different Primes DP背包

http://poj.org/problem?id=3132 题意: 给定n和k,问用恰好k个不同的质数来表示n的方案数. 分析: n和k都很小.反正就是个背包,选k个物品恰好填满n即可. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k,