当前位置:网站首页>[C language] screening method for prime numbers
[C language] screening method for prime numbers
2022-07-02 05:54:00 【SouLinya】
List of articles
subject
describe
Using screening method to find n Prime number within . The solution process of screening method is : take 2~n Positive integers between are stored in the array memory , Will be in the array 2 Everything after that can be 2 The number of divisions 0, then 3 Everything after that can be 3 The number of divisions 0 , And so on , until n until . Array is not 0 The number of is prime .
Input description
Multi group input , Enter a positive integer on each line ( No more than 100).
Output description
The integer entered for each line n, Output two lines , first line , Output n within ( Include n) The prime , Separate... With spaces
Example
Input :20
Output :
2 3 5 7 11 13 17 19
11
One 、 Ideas
The common method of finding and judging prime numbers is trial division , There is also the use of sqrt or 1/2. The screening method is similar to the trial division method , Every number is divided by 1 And numbers other than itself . The key to the screening method is :(1) The request will 2-n The number of is stored in the array first ;(2) Will be 2 After that , Note that 2 Later numbers can be 2 Zeroing of integer division , Then move back one bit , from 3 Start , Will be 3 Later can be 3 Zeroing of integer division , By analogy , until n. In short, the first point is the array 2 after 3, One round of loop traversal divided by 2 after , The position that the array first points to is backward 1, Pointing to the array 3 After 4, Consider how to balance the cycle i and j Value .
Two 、 Code implementation
#include<stdio.h>
int main()
{
int n;
int count=0;
int arr[100]={
0};
while(scanf("%d",&n)!=EOF)
{
int j=0;
int i=0;
for(i=2;i<=n;i++)
{
arr[j++]=i;// assignment
//arr[0]=2,arr[1]=3...arr[18]=20
}
// Zero clearing
for(i=2;i<=n;i++)// Change of divisor
{
for(j=i-1;j<n-1;j++)// The change of the dividend
{
// The first number is +1 Changing , So we use i
if(arr[j]%i==0)
{
arr[j]=0;
}
}
}
// Print
// Because one more number will be initialized , therefore i<n-1
for(i=0;i<n-1;i++)
{
if(arr[i]!=0)
{
count++;
printf("%d ",arr[i]);
}
}
printf("\n%d\n",n-1-count);
}
return 0;
}
边栏推荐
- 运动健身的一些心得经验
- 软件测试 - 概念篇
- Addchild() and addattribute() functions in PHP
- Practice C language advanced address book design
- Zzuli:1069 learn from classmate Z
- Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
- [PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
- 495.提莫攻击
- php父类(parent)
- php内类名称与类内方法名相同
猜你喜欢
如何写出好代码 — 防御式编程指南
Matplotlib double Y axis + adjust legend position
Cube magique infini "simple"
2022-2-14 learning xiangniuke project - Section 7 account setting
ThreadLocal memory leak
File contains vulnerabilities (II)
2022-2-15 learning xiangniuke project - Section 8 check login status
数理统计与机器学习
死磕大屏UI,FineReport开发日记
idea開發工具常用的插件合集匯總
随机推荐
keepalived安装使用与快速入门
Several keywords in C language
PHP inner class name is the same as the inner class method name
Huawei Hongmeng OS, is it OK?
Oled12864 LCD screen
500. 键盘行
Typora installation (no need to enter serial number)
OLED12864 液晶屏
460. LFU cache bidirectional linked list
Win10 copy files, save files... All need administrator permission, solution
死磕大屏UI,FineReport开发日记
Taskbar explicit / implicit toggle function
Common websites for Postgraduates in data mining
Generics and generic constraints of typescript
token过期自动续费方案和实现
Opencv LBP features
我所理解的DRM显示框架
1036 Boys vs Girls
【C语言】简单实现扫雷游戏
MySQL transaction and isolation level