Prime Numbers

 Descriptions:

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.

Write a program which reads a list of N integers and prints the number of prime numbers in the list.

Input

The first line contains an integer N, the number of elements in the list.

N numbers are given in the following lines.

Output

Print the number of prime numbers in the given list.

Constraints

1 ≤ N ≤ 10000

2 ≤ an element of the list ≤ 108

Sample Input 1

  1. 5
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6

Sample Output 1

  1. 3

Sample Input 2

  1. 11
  2. 7
  3. 8
  4. 9
  5. 10
  6. 11
  7. 12
  8. 13
  9. 14
  10. 15
  11. 16
  12. 17

Sample Output 2

  1. 4

题目链接:

https://vjudge.net/problem/Aizu-ALDS1_1_C

题目大意:
输入n个数,判断里面有几个是素数,逐个枚举是最简单的但是会超时,这里用了一个简单的函数,以后可以套用

  1. bool isprime(int x)
  2. {
  3. if(x==)
  4. return true;
  5. if(x<||x%==)
  6. return false;
  7. int i=;
  8. while(i<=sqrt(x))
  9. {
  10. if(x%i==)
  11. return false;
  12. i+=;
  13. }
  14. return true;
  15. }

 AC代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <deque>
  7. #include <vector>
  8. #include <queue>
  9. #include <string>
  10. #include <cstring>
  11. #include <map>
  12. #include <stack>
  13. #include <set>
  14. #include <sstream>
  15. #define mod 1000000007
  16. #define eps 1e-6
  17. #define ll long long
  18. #define INF 0x3f3f3f3f
  19. #define ME0(x) memset(x,0,sizeof(x))
  20. using namespace std;
  21. bool isprime(int x)
  22. {
  23. if(x==)
  24. return true;
  25. if(x<||x%==)
  26. return false;
  27. int i=;
  28. while(i<=sqrt(x))
  29. {
  30. if(x%i==)
  31. return false;
  32. i+=;
  33. }
  34. return true;
  35. }
  36. int n;
  37. int main()
  38. {
  39. int total=;
  40. int m;
  41. cin>>n;
  42. while(n--)
  43. {
  44. cin>>m;
  45. if(isprime(m))
  46. total++;
  47. }
  48. cout<<total<<endl;
  49. }

这题比较简单,我之所以要为他写一篇博客,是因为一下这个埃拉托色筛选法:

1.列举大于等于2的整数

2.留下最小的整数2,删除所有2的倍数

3.在剩下的整数中留下最小的3,删除所有3的倍数

4.在剩下的整数中留下最小的5,删除所有5的倍数

5.以下同理,留下仍未被删除的最小的整数,删除该整数的倍数,一直循环到结束

  1. int isprime[100005];
  2. void eratos(int x)
  3. {
  4. for(int i=; i<=x; ++i)
  5. isprime[i]=true;
  6. isprime[]=isprime[]=false;
  7. for(int i=; i<=x; ++i)
  8. {
  9. if(isprime[i])
  10. {
  11. int j=i+i;
  12. while(j<=x)
  13. {
  14. isprime[j]=false;
  15. j+=i;
  16. }
  17. }
  18. }
  19. }

这个是kuangbin的素数筛法,直接保存到数组,从1开始。

  1. const int MAXN=;
  2. int prime[MAXN+];
  3. void getPrime()
  4. {
  5. memset(prime,,sizeof(prime));
  6. for(int i=;i<=MAXN;i++)
  7. {
  8. if(!prime[i])prime[++prime[]]=i;
  9. for(int j=;j<=prime[]&&prime[j]<=MAXN/i;j++)
  10. {
  11. prime[prime[j]*i]=;
  12. if(i%prime[j]==)break;
  13. }
  14. }
  15. }

【Aizu - ALDS1_1_C】Prime Numbers(素数筛法)的更多相关文章

  1. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  2. POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

    Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...

  3. HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

    Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...

  4. CodeForces 385C Bear and Prime Numbers 素数打表

    第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...

  5. UVA 10539 - Almost Prime Numbers 素数打表

    Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In ...

  6. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  7. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  8. AOJ - 0009 Prime Number (素数筛法) && AOJ - 0005 (求最大公约数和最小公倍数)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. /* ********************* ...

  9. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

随机推荐

  1. QT开发环境的建立以及QTE4.6.3、tslib1.4的移植过程

    1.首先是建立Linux开发环境1.1.在windowsXP下安装博创公司提供的虚拟机软件VMware Workstation,版本为VMware-workstation-full-7.0.1-227 ...

  2. 给WPF文字加多条修饰线

    原文:给WPF文字加多条修饰线 这是上篇WPF中的文字修饰--上划线,中划线,基线与下划线 最后留给朋友思考的问题. 效果图: XAML代码:<Page xmlns="http://s ...

  3. 微信小程序之购物车

    这里演示从商品列表中添加到购物车 下面先做商品列表页.如下图: 布局分析: 首先一个list的主盒子,接着是item盒子,这是必须的.然后把item分成左侧的图片部分,和右侧的说明部分(item盒子使 ...

  4. libuv windows 下编译及使用出现的问题(异步IO)

    1. 下载源码:http://dist.libuv.org/dist/ 我下载的是 v1.8.0 版本,打开可看见四个文件,如下: 说明:.tar.gz 版本的是可以在Linux和Windows下编译 ...

  5. WPF Datagrid with some read-only rows - Stack Overflow

    原文:WPF Datagrid with some read-only rows - Stack Overflow up vote 21 down vote accepted I had the sa ...

  6. Angular 请求另一服务的api(请求代理)

    1.edit "start" of your package.json to look below 定义一个叫做start的新命令 "start": " ...

  7. 优雅实现INotifyPropertyChanged接口——利用Lambda表达式

    原文:优雅实现INotifyPropertyChanged接口--利用Lambda表达式 参考文章 在14年的时候,曾经读过上面的参考文章,不过当时并没有怎么理解,慢慢地也就将这篇文章忘诸脑后了. 直 ...

  8. LeapMotion Demo1

    原文:LeapMotion Demo1     LeapMotion SDK For c# 只提供了一个Sample.cs.   Leap Motion App Home 可以给初入手者提供很好的用户 ...

  9. Emgu-WPF 激光雷达研究-移动物体跟踪

    原文:Emgu-WPF 激光雷达研究-移动物体跟踪 接前两篇博客: 激光雷达数据解析并绘制雷达图 https://blog.csdn.net/u013224722/article/details/80 ...

  10. XF 标签页面

    using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompila ...