当前位置:网站首页>[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;
}
边栏推荐
- "Simple" infinite magic cube
- Generics and generic constraints of typescript
- Mathematical statistics and machine learning
- RGB 无限立方体(高级版)
- 《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
- I want to understand the swift code before I learn it. I understand it
- PHP inner class name is the same as the inner class method name
- 2022-2-15 learning xiangniuke project - Section 8 check login status
- A collection of commonly used plug-ins for idea development tools
- [paper translation] gcnet: non local networks meet squeeze exception networks and beyond
猜你喜欢
随机推荐
File contains vulnerabilities (II)
token过期自动续费方案和实现
492.构造矩形
vite如何兼容低版本浏览器
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
Practice C language advanced address book design
mock-用mockjs模拟后台返回数据
Nacos 启动报错 Error creating bean with name ‘instanceOperatorClientImpl‘ defined in URL
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond
keepalived安装使用与快速入门
软件测试答疑篇
GRBL 软件:简单解释的基础知识
PHP gets CPU usage, hard disk usage, and memory usage
我所理解的DRM显示框架
php继承(extends)
Zzuli:1061 sequential output of digits
Generics and generic constraints of typescript
脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
15 C language advanced dynamic memory management
Go language web development is very simple: use templates to separate views from logic









