Java 素数 prime numbers-LeetCode 204

Description:

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

click to show more hints.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

求n以内的所有素数,以前看过的一道题目,通过将所有非素数标记出来,再找出素数,代码如下:

 1     public int countPrimes(int n) {
 2         if (n == 0 || n == 1 || n == 2)
 3             return 0;
 4         int[] flag = new int[n];
 5         for (int i = 2; i < Math.sqrt(flag.length); i++) {
 6             if (flag[i] == 0)
 7                 for (int j = i; i * j < flag.length; j++) {
 8                     flag[i * j] = 1;
 9                 }
10         }
11         int count = 0;
12         for (int i = 2; i < flag.length; i++)
13             count += flag[i];
14         BitSet bs = new BitSet();
15         bs.ne
16         return flag.length - count - 2;
17     }

值得注意的是外循环只需要到根号n即可,因为内循环是从i*j即i平方开始的。在LeetCode如果没加根号会产生溢出

时间: 2024-11-10 00:15:07

Java 素数 prime numbers-LeetCode 204的相关文章

leetcode 204. Count Primes 找出素数的个数 ---------- java

Description: Count the number of prime numbers less than a non-negative number, n. 找出小于n的素数个数. 1.用最淳朴的算法果然超时了. public class Solution { public int countPrimes(int n) { if (n < 2){ return 0; } int result = 0; for (int i = 2; i < n; i++){ if (isPrimes(

HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12955    Accepted Submission(s): 4490 Problem Description Give you a lot of positive integers, just to find out how many pr

hdu5108Alexandra and Prime Numbers(素数的性质)

题目链接: huangjing 思路:每一个数都可以表示成若干个素数的乘积,那么可以对N从2一直枚举到sqrt(N),然后对每个数都能除到不能取余为止,那么后面的合数就不会除了,所以最后得到的数就是最大的质因子,然后直接N/最大的质因子,还有就是N=1的时候没有存在的数  . 题目: Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth

[email&#160;protected] Sieve of Eratosthenes (素数筛选算法) &amp; Related Problem (Return two prime numbers )

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,

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

HDu 2138 How many prime numbers 高效Miller素数测试

题目就是给出一组数,让我们测试其中有多少个是素数. 求素数有测试sqrt(n)个数的方法,有筛子方法,不过对于本题这样的题目来说就都不是高效的. 本题使用Miller Rabin素数测试法,效率奇高,对于不是极其大的整数测试都几乎是常数时间.令人神往的算法啊. 网上有个程序,好像是什么吉林的模板程序,不过我一直没看懂他是什么思路写的,是个AC的程序,不过却是错误的,呵呵,因为程序一直把9当做素数. 于是上网查找了其中原理,自己写了个程序,效率和他的差不多一样,通过时间基本无差别,不过我的思路是按

Add Two Numbers leetcode java

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (4 -> 6 -&