/*
* @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. 使用UIScreenEdgePanGestureRecognizer写iOS7侧边栏

    使用UIScreenEdgePanGestureRecognizer写iOS7侧边栏 A UIScreenEdgePanGestureRecognizer looks for panning (dra ...

  2. 年金(annuity)

    一.定义 一系列的付款(或收款),付款时间和付款金额具有一定规律性. 二.分类 1-支付时间和支付金额是否确定?确定年金(annuity-certain)风险年金(contingent annuity ...

  3. vue记录

    vue项目中使用默认图片代替异常图片 第一种方法 <img onerror="javascript:this.src='../../static/custom.png';" ...

  4. System IPC 与Posix IPC(共享内存)

    系统v(共享内存) 1.对于系统V共享内存,主要有以下几个API:shmget().shmat().shmdt()及shmctl(). 2.shmget()用来获得共享内存区域的ID,如果不存在指定的 ...

  5. 关于函数指针与c++多态

    原文  https://www.cnblogs.com/zhchngzng/p/4013031.html 虚函数是实现多态的重要元素,请看: class A { public: void a0(){c ...

  6. xalan\xalan\2.7.2\xercesImpl.jar (系统找不到指定的文件)问题

    本文转自:http://blog.csdn.net/lveliu/article/details/77772828 环境搭建为:maven+tomcat tomcat 8.5.2 以上会出现改问题(包 ...

  7. StringUtils工具类介绍

    1 abbreviate方法缩写一段文字 StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg...& ...

  8. HDU 1281 棋盘游戏 【二分图最大匹配】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1281 题意概括: 有N*M大的棋盘,要在里面放尽量多的“车”,求最多能放的车的个数,和为了放最多的车有多 ...

  9. AAAI 2016 paper阅读

    本篇文章调研一些感兴趣的AAAI 2016 papers.科研要多读paper!!! Learning to Generate Posters of Scientific Papers,Yuting ...

  10. Visual C++中MFC消息的分类

    Visual C++中MFC消息的分为三类:标准(窗口)消息.命令消息.控件消息. 1.标准(窗口)消息:窗口消息一般与窗口内部运作有关,如创建窗口,绘制窗口,销毁窗口,通常,消息是从系统发到窗口,或 ...