当前位置:网站首页>Solve prime numbers between 100 and 200
Solve prime numbers between 100 and 200
2022-07-27 02:24:00 【Guangguangzi】
solve 100~200 Between the prime number directory
List of articles
Preface
For many people, there is some confusion about solving prime numbers , ad locum , I will explain my understanding to you in some ways , I hope I can help you . Here is the solution 100~200 Between prime numbers
One 、 What are prime numbers ?
prime number : Prime numbers are also called prime numbers , A prime is defined as a number greater than 1 Of Natural number in , except 1 And there's no more than it itself Factor The natural number of .(PS:7=1*7,7 As a prime number )
Two 、 Method of solving prime numbers
1. Conventional methods
The code is as follows ( Example ):
#include<stdio.h>
// solve 100~200 The prime between
int main()
{
int i = 0;
int j = 0;
int sum = 0; //sum Count , Statistics 100~200 The number of primes between
for (i = 100; i <= 200; i++) // Prescribed scope
{
for (j = 2; j < i; j++) // Prime numbers are defined as having only two factors :1 And itself
{
if (i % j == 0) // If i%j==0 explain j It's also i Factor of , Not satisfying the condition of prime number
{
break; // Not satisfied , Out of the loop
}
}
if (j == i) // If it's a prime number , Not in the internal circulation break operation ,j++ To the end of the cycle i
{
printf("%d ", i); // Output i
sum++; // Add one to the total
}
}
printf("\nsum=%d\n", sum);
return 0;
}2. Optimize the code
Optimize the code 1 as follows ( Example ):
int main()
{
int i = 0;
int j = 0;
int sum = 0; //sum Count , Statistics 100~200 The number of primes between
for (i = 101; i < 200; i+=2) //100,200 Even numbers are not considered ,100~200 Even numbers in do not consider each addition 2, skip 2
{
for (j = 2; j < i; j++) // Prime numbers are defined as having only two factors :1 And itself
{
if (i % j == 0) // If i%j==0 explain j It's also i Factor of , Not satisfying the condition of prime number
{
break; // Not satisfied , Out of the loop
}
}
if (j == i) // If it's a prime number , Not in the internal circulation break operation ,j++ To the end of the cycle i
{
printf("%d ", i); // Output i
sum++; // Add one to the total
}
}
printf("\nsum=%d\n", sum);
return 0;
}Through the understanding of the definition of prime numbers , Understand even numbers ( barring 0,2) It must not be a prime number , During the cycle , Even numbers are no longer considered , It can greatly improve the operation speed of the code .
for (i = 101; i < 200; i+=2) //100,200 Even numbers are not considered ,100~200 Even numbers in do not consider each addition 2, skip 2 { for (j = 2; j <= i/2; j++) // Prime numbers are defined as having only two factors :1 And itself ,i/2 For the previous session { if (i % j == 0) // If i%j==0 explain j It's also i Factor of , Not satisfying the condition of prime number { break; // Not satisfied , Out of the loop } } if (j == i) // If it's a prime number , Not in the internal circulation break operation ,j++ To the end of the cycle i { printf("%d ", i); // Output i sum++; // Add one to the total } }
for (i = 101; i < 200; i+=2) //100,200 Even numbers are not considered ,100~200 Even numbers in do not consider each addition 2, skip 2 { for (j = 2; j <= sqrt(i); j++) // Prime numbers are defined as having only two factors :1 And itself , { if (i % j == 0) // If i%j==0 explain j It's also i Factor of , Not satisfying the condition of prime number { break; // Not satisfied , Out of the loop } } if (j == i) // If it's a prime number , Not in the internal circulation break operation ,j++ To the end of the cycle i { printf("%d ", i); // Output i sum++; // Add one to the total } }
3. To open or find a new path or snap course
Using the method of function , Or adopt a special method , See the following ():
#include<stdio.h>
int main()
{
int i = 0;
int sum = 0; //sum Count , Statistics 100~200 The number of primes between
for (i = 101; i < 200; i+=2) //100,200 Even numbers are not considered ,100~200 Even numbers in do not consider each addition 2, skip 2
{
if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0&&i%11!=0&&i%13!=0) // because 2,3,5,7,11,13 Prime number
{
printf("%d ", i);
sum++;
}
}
printf("\nsum=%d\n", sum);
return 0;
}stay 100~200 In the range of , If you can divide prime numbers by numbers , Explain that prime number is its factor , The reason for not considering other numbers is that other numbers have multiple factors , Prime numbers have only two factors , If you divide prime numbers by whole numbers, they are not equal to 0, Note that the number of factors included is only 1 And itself , Then it must be a prime number .
4. Function method to solve
#include<stdio.h>
#include<math.h>
int su(int a) // You need to return a judgment number
{
int i = 0;
for (i = 2; i <= sqrt(a); i++)
{
if (a % i == 0)
{
return 1; // Not prime numbers return 1
break; // End the cycle directly , End of the function
}
}
}
int main()
{
int i = 0;
int sum = 0;
for (i = 101; i < 200; i+=2)
{
if (su(i) != 1)
{
printf("%d ", i);
sum++;
}
}
printf("\nsum=%d\n",sum);
return 0;
}summary
In this subject , We need to understand the definition of prime numbers , Understand the judgment conditions , What are the closing conditions . Praise , Please pay attention !

边栏推荐
- Timer interrupt experiment
- Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
- HCIP-第六天-OSPF静态大实验
- (super detailed version, don't know to comment at any time) codeforces round 804 (Div. 2) C the third problem
- 数字集成电路:MOS管器件章(二)
- 怎么判断一个数是奇数还是偶数?
- 全连MGRE与星型拓扑MGRE
- HCIP第一天静态路由综合实验
- NB-IOT联网通信
- C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
猜你喜欢
随机推荐
Explain exi interrupt through the counting experiment of infrared sensor
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
Self introduction and planning about programming
【降维打击,带你深度学习CPU(上)】
今天浅讲一下转义字符【萌新版】
js中的数组方法和循环
JVM面试题(面试必备)
数字集成电路:MOS管器件章(一)
HCIP第一天静态路由综合实验
NAT network address conversion experiment
C语言——数据类型、基本数据类型的取值范围
RISC-V工具链编译笔记
OSPF protocol knowledge summary
ESP8266Wi-Fi接入云平台
First knowledge of C language (2)
C语言——二维数组、指针
Tim output comparison - PWM
Educational Codeforces Round 132 (Rated for Div. 2), problem: (D) Rorororobot
Interesting C language








![[C language] relevant distinction between strlen and sizeof](/img/c0/c026818692a01c1867771434e90da8.png)


