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 ...
随机推荐
- 利用JAVA多线程来提高数据处理效率
肿瘤大数据挖掘中经常需要处理上百亿行的文本文件,这些文件往往高达数百GB,假如文件结构简单统一,那么用sed和awk 处理是非常方便和快速的.但有时候会遇到逻辑较为复杂的处理流程,这样我一般会用JAV ...
- Dockerfile的指令
指令的一般格式为 INSTRUCTION arguments,指令包括 FROM.MAINTAINER.RUN 等. FROM 格式为 FROM <image>或FROM <imag ...
- Swift基础之如何使用iOS 9的Core Spotlight框架
本文由CocoaChina译者KingOfOnePiece(博客)翻译 作者:GABRIEL THEODOROPOULOS?校对:hyhSuper 原文:How To Use Core Spotlig ...
- Maven之(六)setting.xml配置文件详解
setting.xml配置文件 maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.h ...
- Programming In Scala笔记-第五章、Scala中的变量类型和操作
这一章的一些基础性的东西,主要包括Scala中的基本变量类型,以及相关的一些操作符. 一.简单类型 下表中列出Scala语言中的基本类型,以及其字节长度,其中Byte, Short, Int, Lon ...
- Android TV开发总结(二)构建一个TV Metro界面(仿泰捷视频TV版)
前言:上篇是介绍构建TV app前要知道的一些事儿,开发Android TV和手机本质上没有太大的区别,屏大,焦点处理,按键处理,是有别于有手机和Pad的实质区别.今天来介绍TV中Metro UI风格 ...
- Win7下安装linux虚拟机
关于如何在Win7下搭建linux学习环境,特在此分享下. 一.工具 1.VMware-workstation-full-9.0.0-812388.exe 下载地址:http://pan. ...
- ORACLE数据库学习之SQL性能优化详解
Oracle sql 性能优化调整 ...
- 福利:工作经常用到的Mac软件整理(全)
每日更新关注:http://weibo.com/hanjunqiang 新浪微博!iOS开发者交流QQ群: 446310206 前言 这是我个人在工作中会用到的Mac软件,其中包括办公.开发.视频等 ...
- 2.1、Android Studio通过Lint提升你的代码
为了测试你的Android应用符合功能需求.最重要的是确保你的代码没有结构性问题.结构差的代码影响你的Android应用的可靠性,让你的代码难以维护.比如,如果你的XML资源文件包含未使用的明明空间, ...