/*
* @lc app=leetcode.cn id=204 lang=c
*
* [204] 计数质数
*
* https://leetcode-cn.com/problems/count-primes/description/
*
* algorithms
* Easy (26.72%)
* Total Accepted: 13.8K
* Total Submissions: 51.6K
* Testcase Example: '10'
*
* 统计所有小于非负整数 n 的质数的数量。
*
* 示例:
*
* 输入: 10
* 输出: 4
* 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
*
*
*/ int countPrimes(int n) {
int i,j,count=;
if(n<=){
return ;
}
for(i=;i<=n;i+=){
for(j=;j<=sqrt(i);j++){
if(i%j==){
break;
}
}
if(j>sqrt(i))
count++;
}
return count;
}

统计质数还是很简单的。

-------------------------------

python:

#
# @lc app=leetcode.cn id=204 lang=python3
#
# [204] 计数质数
#
# https://leetcode-cn.com/problems/count-primes/description/
#
# algorithms
# Easy (26.72%)
# Total Accepted: 13.8K
# Total Submissions: 51.6K
# Testcase Example: '10'
#
# 统计所有小于非负整数 n 的质数的数量。
#
# 示例:
#
# 输入: 10
# 输出: 4
# 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
#
#
#
class Solution:
def countPrimes(self, n: int) -> int:
if n <= 2:
return 0
res = [True] * n
res[0] = res[1] = False
for i in range(2, n):
if res[i] == True:
for j in range(2, (n-1)//i+1):
res[i*j] = False
return sum(res)

Leecode刷题之旅-C语言/python-204计数质数的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. MQ测试

    2015年8月13日23:14:52 测试RabbitMq ====================== 千兆局域网:send ≍10000/s  receive ≍7000/s 百兆局域网:send ...

  2. linux下生成带符号的随机密码

    cat /dev/urandom |tr -dc "[:graph:]"|fold -w 15|head 结果如下:

  3. QT控件大小的方法

    http://blog.csdn.net/liang19890820/article/details/51986284

  4. 一些SAP Partners能够通过二次开发实现打通C/4HANA和S/4HANA的方法介绍

    有好几位朋友在公众号后台给我留言询问SAP C/4HANA和S/4HANA集成的方案. 尽管我给这些朋友推送了一个方案:打通C/4HANA和S/4HANA的一个原型开发:智能服务创新案例,然而我得到的 ...

  5. ApiServer_YiChat apache项目布置过程

    1.复制文件到   /var/www/  文件夹下 2.配置项目目录 3.修改/var/www/api/public 文件夹下的隐藏文件  .htaccess     增加‘?’号 4.打开/etc/ ...

  6. 2019.1.2 Spring管理事务的方式

    Spring管理事务的方式 1.编码式 1.将核心事务管理器配置到Spring容器 2.配置TransactionTemplate模版 3.将事务模版注入service 4.在Service中调用模版 ...

  7. linq 和lamba表达式

    一.什么是Linq(what)二.Linq的优点(why)三.Linq查询的步骤(how)四.查询基本操作五.結合實例代碼(具體聯繫用linqtosql來寫的增刪改查)一.什么是Linq(what). ...

  8. CentOS gitlab 安装配置

    CentOS gitlab 安装配置 2018-11-02 11:23:09   Visit  5 在/etc/yum.repos.d 目录下创建文件gitlab-ce.repo,使用国内的安装源 b ...

  9. ListView 中嵌套 GridView

    1.主布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  10. IP Addressing

    IP Addressing(处理) Each host on Internet has unique 32 bit IP address Each address has two parts: net ...