Leetcode_204_Count Primes

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pistolove/article/details/46366207

本文是在学习中的总结,欢迎转载但请注明出处: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;
	}
}


The Primes

02-11

问题描述 :nnnnn|---|---|---|---|---|nn| 1 | 1 | 3 | 5 | 1 |nn|---|---|---|---|---|nn| 3 | 3 | 2 | 0 | 3 |nn|---|---|---|---|---|nn| 3 | 0 | 3 | 2 | 3 |nn|---|---|---|---|---|nn| 1 | 4 | 0 | 3 | 3 |nn|---|---|---|---|---|nn| 3 | 3 | 3 | 1 | 1 |nn|---|---|---|---|---|nn(Figure 1)nnnnnFigure 1 shows a square. Each row, each column and the two diagonals can be read as a five digit prime number. The rows are read from left to right. The columns are read from top to bottom. Both diagonals are read from left to right. Write a program that constructs such squares:nnnnn The prime numbers must have the same digit sum (11 in the example).nnn The digit in the top left-hand corner of the square is pre-determined (1 in the example).nnn A prime number may be used more than once in the same square.nnn If there are several solutions, all must be presented.nnn A five digit prime number cannot begin with zeros, ie 00003 is NOT a five digit prime number. nnnn nn输入:nnnYour program is to read from standard input. First the digit sum of prime numbers and then the digit in the top left-hand corner of the square. The file contains two lines. There will always be a solution to the given test data. n输出:nnnYour program is to write to standard output. Output five lines for each solution found, where each line in turn consists of a five digit prime number. The solutions are sorted by the prime in the first row, then by the prime in the second row,etc. Output a blank line after each solution.n样例输入:nn11n1n样例输出:nn11351n14033n30323n53201n13313nn11351n33203n30323n14033n33311nn13313n13043n32303n50231n13331

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试