Description:

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

提示晒数法:

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

https://primes.utm.edu/howmany.html

别人的代码:

int countPrimes(int n) {
if (n<=2) return 0;
vector<bool> passed(n, false);
int sum = 1;
int upper = sqrt(n);
for (int i=3; i<n; i+=2) {
if (!passed[i]) {
sum++;
//avoid overflow
if (i>upper) continue;
for (int j=i*i; j<n; j+=i) {
passed[j] = true;
}
}
}
return sum;
}

我的代码:

// countprime.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
#include <math.h>
#include<vector>
using namespace std; int countPrimes1(int n)
{
int temp = 0;
if (2 >= n) return 0; bool* primes = new bool[n];
for (int i = 2; i < n; ++i)
primes[i] = true; int sqr = (int)(sqrt((double)(n - 1)));
for (int i = 2; i <= sqr; ++i)
{
if (primes[i])
{
temp++;
for (int j = i * i; j < n; j += i)
primes[j] = false;
}
} int sum = 0;
for (int i = 2; i < n; ++i)
sum += (primes[i]) ? 1 : 0; /*cout<<temp;*/
delete[] primes; return sum;
} int countPrimes(int n)
{
if (n<=2)return 0; int sum = 0;
int sqr = (int)(sqrt((double)(n - 1))); vector<bool> prime(n,0); for(int i = 2; i < n; ++i)
prime[i] = 1; for(int i =2; i <= sqr; ++i)
{
if(prime[i]==1)
{
//sum++;
for(int j = i*i; j < n; j = j+i)
prime[j] = 0;
} } for(int i = 2;i < n; ++i)
sum += prime[i] ? 1 : 0; return sum;
} int _tmain(int argc, _TCHAR* argv[])
{ cout<<countPrimes(5)<<endl; cout<<countPrimes1(3)<<endl; getchar();
return 0;
}

超时代码:

int countPrimes(int n)
{
int sum = 0;
for(int i = 0 ;i<=n;i=i+2)
{
int j = 2;
int temp = sqrt(i);
for(;j<=temp;j=j+1)
{
if((i%temp)==0)
{break;}
sum++;
}
} return sum;
}

纪念一下首次AC

leetcode 204题求素数个数的更多相关文章

  1. LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)

    题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...

  2. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  3. C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...

  4. 求素数个数的优化-LeetCode204

    问题 计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 第一种解法容易想到但是会 超时 ...

  5. How many prime numbers(求素数个数)

    How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. 求小于n的素数个数

    本文是对 LeetCode Count Primes 解法的探讨. 题目: Count the number of prime numbers less than a non-negative num ...

  7. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

  8. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  9. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...

随机推荐

  1. 远程通信(RPC,Webservice,RMI,JMS、EJB、JNDI的区别)对比

    总结这些概念都是易混淆,最基本概念定义复习和深入理解,同时也是架构师必备课程 RPC(Remote Procedure Call Protocol) RPC使用C/S方式,采用http协议,发送请求到 ...

  2. GDALWarp设置GDALWarpOptions::dfWarpMemoryLimit过大时处理失败

    使用GDALWarp写了一个裁切图像的算法,在小内存的电脑没事,大内存的电脑就处理失败(32位也没问题),查看GDAL的日志发现下面的错误信息: Fri Apr 08 17:39:02 2016: G ...

  3. C算法实现:将字符串中的数字返回为整型数

    今天看linux内核驱动的代码,发现一个算法写得挺简单,也有意思. 分享一下我的测试代码: #include <stdio.h> typedef int U32 ; U32 String2 ...

  4. [转]django-registration quickstart

    Basic configuration and use--------------------------- Once installed, you can add django-registrati ...

  5. Redis 学习笔记1:CentOS 6.7下安装Redis

    在linux环境搭建Redis环境,首先从官网(http://redis.io/)下载Redis 版本,本人使用的3.21版本. 1. 将redis 解压到  /usr/local目录下. [root ...

  6. 基于Web在线考试系统的设计与实现

    这是一个课程设计的文档,源码及文档数据库我都修改过了,貌似这里复制过来的时候图片不能贴出,下载地址:http://download.csdn.net/detail/sdksdk0/9361973   ...

  7. 3-sum问题

    给定一个整数数组,判断能否从中找出3个数a.b.c,使得他们的和为0,如果能,请找出所有满足和为0个3个数对. #define SIZE 10 void judgeAndPut(int* arr, i ...

  8. TCP的发送系列 — tcp_sendmsg()的实现(一)

    主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 上一篇blog讲的是send().sendto().sen ...

  9. 怎样在Ubuntu 14.04中搭建gitolite git服务器

     1.   首先这里我们安装openssh-serveropenssh-client,如果你用的是VPS之类的一般都默认安装好了,不过运行一个这个命令不会有错的,如果有安装就会提示已安装. sud ...

  10. 一个简单的安卓+Servlet图片上传例子

    例子比较 简单,服务端为Java Web Servlet,doPost方法中接收图片并保存,然后将保存的图片名返回给客户端,关键代码: @SuppressWarnings("deprecat ...