/*
* @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. python+appium学习之swipe

    #coding:utf-8 from appium import webdriver from time import sleep import random desired_caps={ " ...

  2. 马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与 分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴CEO的马云自曝了他第一次上电视是在1995年。“我刚开始创

    马云18年前制止偷井盖视频走红 2013-05-10 11:00:37 来源: 新快报(广州) 有0人参与   分享到 网易微博 新浪微博 腾讯空间 人人网 有道云笔记 在一次访谈中,即将卸任阿里巴巴 ...

  3. hdu 6208 The Dominator of Strings【AC自动机】

    hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...

  4. PHP------关于字符串的处理

    每一种语言对,字符串都是比较重要的,因为字符串牵扯到输出. 尤其是在网页里面,所有的内容输出,都要以字符串的形式展示在页面上.比如,输出换行.输出一段话或者输出一个标签,都是以字符串来输出的:有时用数 ...

  5. TensorFlow基础(二)实现神经网络

    (1)前向传播算法 神经网络的前向传播算法主要构成部分: 1.神经网络的输入: 2.神经网络的连接结构:神经网络是由神经元(节点)构成的 3.每个神经元中的参数. (2)TensorFlow随机数生成 ...

  6. Linux下安装Qt5.6.1

    我的环境:CentOS 6.7  64位. 1.下载Qt: Qt版本有很多,自己比较菜,希望安装的过程越简单越好,感觉比较新的版本会好安装一些,5.4版本还要更新 /usr/lib64/libstdc ...

  7. mysql5.7.22tar包安装

    mysql5.7.22tar包安装 #卸载系统自带的Mariadb [root@ ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x ...

  8. Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法

    Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...

  9. ASP.NET Core MVC的路由参数中:exists后缀有什么作用,顺便谈谈路由匹配机制

    我们在ASP.NET Core MVC中如果要启用Area功能,那么会看到在Startup类的Configure方法中是这么定义Area的路由的: app.UseMvc(routes => { ...

  10. 如何搭建私人的ghost博客

    可参考 http://blog.pandax.me/cjbk/ 此文