leetcode 204题求素数个数
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题求素数个数的更多相关文章
- LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...
- 求素数个数的优化-LeetCode204
问题 计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 第一种解法容易想到但是会 超时 ...
- How many prime numbers(求素数个数)
How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 求小于n的素数个数
本文是对 LeetCode Count Primes 解法的探讨. 题目: Count the number of prime numbers less than a non-negative num ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
随机推荐
- SpringMVC 教程 - Handler Method
原文链接:https://www.codemore.top/cates/Backend/post/2018-04-21/spring-mvc-handler-methods 由注解@RequestMa ...
- Docker 数据卷
数据卷是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性: 数据卷可以在容器之间共享和重用 对数据卷的修改会立马生效 对数据卷的更新,不会影响镜像 卷会一直存在,直到没有容 ...
- Docker配置文件
Docker 的 Registry 利用配置文件提供了一些仓库的模板(flavor),用户可以直接使用它们来进行开发或生产部署. 模板 在 config_sample.yml 文件中,可以看到一些现成 ...
- matplotlib画散点图,并在散点处打上相应标签
运行环境: py3.6 matplotlib 2.1.2 x = [2,4,6,7,8,5,4,3] y = [3,6,5,8,4,3,2,4] txt = ['我','今','晚','上','吃', ...
- JavaScript中的事件模型
JS中的事件 1.鼠标事件 onclick ondbclick onmouseover onmouseout 2.HTML事件 onload onunload onsubmit ...
- iOS objc_msgSend 野指针Crash 从 Log 提取 Crash 时 selector 的地址和名字并打印
从 crash stack log 里面,提取 objc_msgSend 关键字,定位是否是野指针问题导致的crash,如果是则打印 crash 时的 objc_msgSend 调用的第二个参数,即 ...
- 项目分享:通过使用SSH框架的公司-学员关系管理系统(CRM)
----------------------------------------------------------------------------------------------[版权申明: ...
- springMVC源码分析--ModelAndViewContainer和ModelMap
ModelAndViewContainer主要是用来返回Model对象的,在ModelAndViewContainer中有defaultModel和redirectModel, defaultMode ...
- let内嵌lambda使用set!构成闭包
查了半天没有找到scheme中判断数据类型的函数,索性自己写了个type?,发现闭包和递归有着微妙的联系. 本例中,自由变量是types,外层let初始化了types的值,内层let里的(set! t ...
- Common-used commands in Docker
1. Start running a image in background mode docker run -it -d <image>:<tag> e.g. docker ...