[Hackerrank] Prime Number Less Than N

static int getNumberOfPrimes(int N) {
        int n = N+1;//to include 0 as the first number for easy index operations later
        final boolean a[] = new boolean[n]; //Initialized to null by default.
        Arrays.fill(a, true);  

        a[0] = false;
        a[1] = false; 

        for (int i = 2; i < n / 2; ++i)
        {
            if (a[i])
            {
                for (int j = i + i; j < n; j += i)
                    a[j] = false;
            }
        } 

        int count = 0;for(int i = 2;i<n;++i)
        {
            if(a[i])
            {
                //System.out.print(i+",");
                ++count;
            }
        }
        return count;
    }
时间: 2024-10-23 18:49:19

[Hackerrank] Prime Number Less Than N的相关文章

Lintcode: Kth Prime Number

Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eligible numbers are like 3, 5, 7, 9, 15 ... Example If k=4, return 9. Challenge O(n log n) or O(n) time 这是丑数问题(Ugly Number), 思路参看: http://www.cppblog.co

九度OJ—题目1040:Prime Number

题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k-th prime number. 样例输入: 3 7 样例输出: 5 17 来源: 2008年上海交通大学计算机研究生机试真题 答疑: 解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7764-1-1.html #include<iostream> #include<math.h> using namespace std

Codeforces Round #209 (Div. 2)——Prime Number

MySQL使用的是插件式存储引擎. 主要包括存储引擎有:MyISAM,Innodb,NDB Cluster,Maria,Falcon,Memory,Archive,Merge,Federated. 其中最为广泛的是MyISAM 和Innodb两种存储引擎,所以接下来对它们做简单介绍. MyISAM 存储引擎简介 MyISAM 存储引擎的表存储在数据库中,每一个表都被存放为三个以表名命名的物理文件. 1.(.frm文件)任何存储引擎都不可缺少的存放表结构定义信息的文件 2.(.MYD文件)存放表数

10 001st prime number

这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? def isprime(n): boolisprime = True for i in xrange(2,n): if n % i == 0: bool

九度OJ 1040 Prime Number (筛素数,试除法)

题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k-th prime number. 样例输入: 3 7 样例输出: 5 17 这道题,好久以前使用试除法做的,原理是维护一个素数表,根据输入的num,确定是否之前算过,算过了,就直接输出,没算过,就现在开始算,并且把中间的素数全保存下来: #include<stdio.h> int k[10001]; int main(int argc, char *argv[]) { k[1]=

题目1040:Prime Number

时间限制:1 秒 内存限制:32 兆 特殊判题:否 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k-th prime number. 样例输入: 3 7 样例输出: 5 17解题思路:素数筛选法注意事项:题目要求是输出1000内任何一个素数,把数组的大小声明为1000是远远不够的,程序运行结果显示 第1000个素数为7919,因此数组要声明的大一点.第一次提交,因为数组小 所以是wrong answer,第二次错误是runtim

[LeetCode] Prime Number of Set Bits in Binary Representation

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin

762. Prime Number of Set Bits in Binary Representation 二进制表示形式中的素数位数

Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation. (Recall that the number of set bits an integer has is the number of 1s present when written in bin

JD 题目1040:Prime Number (筛法求素数)

OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下的数中选择最小的数是素数,然后去掉它的倍数. 依次类推.直到筛子为空时结束. 如有: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1不是素数.去掉.剩下的数中2最小,是素数,去掉2的