当前位置:网站首页>Multiple optimization methods print prime numbers between 100 and 200
Multiple optimization methods print prime numbers between 100 and 200
2022-07-24 07:41:00 【Grumpy applet ape】
Print 100~200 The prime between
Multiple optimization methods print 100~200 The prime between .
This paper uses four methods to print 100~200 The prime between , If it is helpful to you, you can pay attention to plus three companies , Continuous updating .
One 、 Title Overview
Print 100~200 The prime between .
prime number : Prime number , except 1 And myself , There is no other divisor , Then the data is prime .
Two 、 Method
Method 1 : Trial division
Try the idea of division :
Because we're going to 100 to 200 The prime between , And 100 It's definitely not prime , So set variables i The range of phi is zero 101~200, Set another variable j, Because any number can be 1 Aliquot so j from 2 Start , If j<i, Will judge i%j Is it equal to 0, If it is equal to 0 shows i Not primes ,break Jump out then i++; If j and i equal , explain [2, i) All data between cannot be i to be divisible by , be i As a prime number
The code is as follows ( Example ):
Ideas :
prime number : Prime number , except 1 And myself , There is no other divisor , Then the data is prime , The specific way is as follows
*/
// Method 1 : Trial division
int main()
{
int i = 0;
int count = 0;
// The outer loop is used to get 100~200 All data between ,100 It's definitely not prime , therefore i from 101 Start
for(i=101; i<=200; i++)
{
// Judge i Prime or not : use [2, i) Every data between is i except , As long as one can be divisible , It's not a prime number
int j = 0;
for(j=2; j<i; j++)
{
if(i%j == 0)
{
break;
}
}
// After the above cycle , If j and i equal , explain [2, i) All data between cannot be i to be divisible by , be i As a prime number
if(j==i)
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
Defects of trial and error method : Through the analysis of these data , Find more than i/2 The data is definitely not i Multiple , This method does many meaningless operations , We should try to improve .
Method 2
For method one ( Trial division ) The optimization of the , Get the data , Just test 【2,i/2】 Whether there are elements in the interval of can be divisible .
The code is as follows ( Example ):
int main()
{
int i = 0;//
int count = 0;
for(i=101; i<=200; i++)
{
// Judge i Prime or not
//2->i-1
int j = 0;
for(j=2; j<=i/2; j++)
{
if(i%j == 0)
{
break;
}
}
//...
if(j>i/2)
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
Method 2 still contains some duplicate data , To optimize the :
Method 3
Method 3 : If i It can be [2, sqrt(i)] Any data division between , be i Not primes .
reason : If m Can be 2 ~ m-1 Divide by any integer , The second factor must have one less than or equal to sqrt(m), The other is greater than or equal to sqrt(m).
int main()
{
int i = 0;
int count = 0;
for(i=101; i<=200; i++)
{
// Judge i Prime or not
//2->i-1
int j = 0;
for(j=2; j<=sqrt(i); j++)
{
if(i%j == 0)
{
break;
}
}
//...
if(j>sqrt(i))
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
Continue to optimize method 3
Method four
as long as i Don't be [2, sqrt(i)] Divide any data between , be i Prime number , But in practice i Don't go from 101 Gradually increase to 200, Because something happened 2 and 3 outside , No two consecutive adjacent data are prime numbers at the same time .
int main()
{
int i = 0;
int count = 0;
for(i=101; i<=200; i+=2)
{
// Judge i Prime or not
//2->i-1
int j = 0;
for(j=2; j<=sqrt(i); j++)
{
if(i%j == 0)
{
break;
}
}
//...
if(j>sqrt(i))
{
count++;
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
summary
Here are four ways to judge and print 100~200 The prime , Each method is the optimization of the former method . If it is useful to everyone , You can pay attention to plus three , Continuous updating , You can also send me a private letter ~
边栏推荐
- requests-爬虫实现一个简易网页采集器
- Arduino's super power-saving sleep mode has worked with one 18650 battery for 17 years
- Network security B module windows operating system penetration test of national vocational college skills competition
- App performance test case
- Introduction to C language II. Functions
- JS的DOM操作——style的操作
- hcip第八天笔记
- Appium doctor command error pit - resolved
- Harbor2.2 quick check of user role permissions
- 【云原生】MySql索引分析及查询优化
猜你喜欢

【HiFlow】腾讯云HiFlow场景连接器实现校园信息管理智能化

Error when using PIP: pip is configured with locations that requires tls/ssl

Service Vulnerability & FTP & RDP & SSH & Rsync

Laplace distribution

requests-爬虫实现一个简易网页采集器

Unable to auto assemble, bean of type "redistemplate" not found

php 转义字符串

Advanced part of C language IV. detailed explanation of user-defined types

One click Copy and import of web interface data into postman

Influxdb未授权访问&CouchDB权限绕过
随机推荐
Introduction to C language v First understanding pointer VI. first understanding structure
2021-06-03pip error valueerror: unable to find resource t64.exe in package pip_ vendor.distlib
There are two tables in Oracle, a and B. these two tables need to be associated with the third table C. how to update the field MJ1 in table a to the value MJ2 in table B
Amber tutorial A17 learning - concept
Arduino在不同主频下稳定支持的TTL串口速率
Mitre att & CK ultra detailed learning notes-02 (a large number of cases)
【sklearn】PCA
Reptile learning - Overview
Requests crawl page source code data
CentOS 7 install mysql5.6.37
Tools for data visualization
Installation and use of Zen path & defect report & defect operation
多种优化方法打印100~200之间的素数
【FreeRTOS】11 软件定时器
2021-06-03 database query - sorting
Opencascade notes: GP package
Using depth and normal textures in unity
Requests crawler implements a simple web page collector
游戏三子棋
Feature Selective Anchor-Free Module for Single-Shot Object Detection