首页 > 试题广场 >

返回小于 N 的质数个数

[编程题]返回小于 N 的质数个数
  • 热度指数:4576 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
请考虑性能

输入描述:
一个整数N


输出描述:
小于N的质数数量
示例1

输入

10

输出

4

说明

N=10,质数有 [2, 3, 5, 7]

备注:
0、1 不属于质数。
头像 罅隙·
发表于 2022-07-10 17:19:29
一、原式版本 bool is_prime(int n) { for(int i = 2; i < n; i++) { if(n % i == 0) return false; } return true; } int m 展开全文
头像 白伟仝
发表于 2020-07-24 22:05:57
暴力遍历判断质数即可: import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(Syst 展开全文
头像 bao_hu_yuan_zhang
发表于 2024-03-21 20:59:57
#include <iostream> using namespace std; bool isprime(int n) { int i=0; if(n==2) { return 1; } else { 展开全文
头像 牛客12138451号
发表于 2023-07-03 10:17:24
#include <iostream> using namespace std; bool isPrime(int n){ for(int i=2;i<n;i++){//如果n被i整除,则返回false if(n%i==0){ re 展开全文
头像 道衍丶
发表于 2022-12-25 21:02:37
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> int main() { int n = 0,cnt = 0; scanf( 展开全文