C language: Write a code to print prime numbers (prime numbers) between 100 and 200 using trial division

topic:

Use trial division to print the prime numbers between 100 and 200 .

              

Prime number (prime number) : A number can only be written as the product of one and itself .

For example: 7 can only be written as 1*7 , that is a prime number (prime number).

                    

 =========================================================================

                       

Idea 1: Use trial division

general idea:

(1). Use the outer loop : generate a number between 100 and 200.

                     

(2). Set the inner loop : generate numbers from 2 to i-1 .

                  

(3) . Set the if condition judgment statement in the inner loop ,

                 

Check if i is a prime number :

Use a number between 2 and i-1 to try to divide i , if it can be divisible , then i is not a prime number ;

If no number between 2 and i-1 can divide i evenly , then i is a prime number .

                   

(4). After the judgment is completed , judge whether it is a prime number according to the value of the variable flag , and print it out if it is .

                


                 

first step:

(1). Use the outer loop : generate prime numbers between 100 and 200.

              

(2). Set a variable flag , if the flag is 1 , then i is a prime number , if the flag is 0 , then i is not a prime number .

                     

Implementation code:

#include <stdio.h>
int main()
{
	int i = 0; //循环变量
	for (i = 100; i <= 200; i++) //生成 100~200 之间的素数
	{
		int flag = 1; //设置变量flag

	}


	return 0;
}

Realize the picture:

                 


                 

Step two:

Set the inner loop : generate numbers from 2 to i-1.

                     

Implementation code:

#include <stdio.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 100; i <= 200; i++) //生成 100~200 之间的素数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= i - 1; j++) //设置内循环:生成 2~i-1 的数
		{

		}
	}


	return 0;
}

Realize the picture:

                 


                 

third step:

(1) . Set the if condition judgment statement in the inner loop .

                   

(2).  Determine whether i is a prime number :

Use a number between 2 and i-1 to try to divide i , if it can be divisible , then i is not a prime number ;

If no number between 2 and i-1 can divide i evenly , then i is a prime number .

                     

Implementation code:

#include <stdio.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 100; i <= 200; i++) //生成 100~200 之间的素数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= i - 1; j++) //设置内循环:生成 2~i-1 的数
		{

			if (i % j == 0) //在内循环中设置 if条件判断语句,判断i是否为素数
			//用 i 模上一个 j,看 j 能不能整除 i ,有余数则表示不能整除
			{
				flag = 0; //flag == 0,则i不是素数
				break;
				//只要有一个 j 把 i 整除了,说明 i 已经不是素数了,
				//所以不用再继续循环了,使用break跳出循环。
			}

		}


	}


	return 0;
}

Realize the picture:

                 


                 

the fourth step:

After the judgment , according to the value of the variable flag , it is judged whether i is a prime number , and if it is , it is printed out .

                     

Implementation code:

#include <stdio.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 100; i <= 200; i++) //生成 100~200 之间的素数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= i - 1; j++) //设置内循环:生成 2~i-1 的数
		{

			if (i % j == 0) //在内循环中设置 if条件判断语句,判断i是否为素数
			//用 i 模上一个 j,看 j 能不能整除 i ,有余数则表示不能整除
			{
				flag = 0; //flag == 0,则i不是素数
				break;
				//只要有一个 j 把 i 整除了,说明 i 已经不是素数了,
				//所以不用再继续循环了,使用break跳出循环。
			}

		}

		if (flag == 1) //循环判断完后,根据变量的值,判断i是不是素数,是则打印
		{
			printf("%d ", i);
		}
	}


	return 0;
}

Realize the picture:

                    

Idea 1: Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 100; i <= 200; i++) //生成 100~200 之间的素数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= i - 1; j++) //设置内循环:生成 2~i-1 的数
		{

			if (i % j == 0) //在内循环中设置 if条件判断语句,判断i是否为素数
			//用 i 模上一个 j,看 j 能不能整除 i ,有余数则表示不能整除
			{
				flag = 0; //flag == 0,则i不是素数
				break;
				//只要有一个 j 把 i 整除了,说明 i 已经不是素数了,
				//所以不用再继续循环了,使用break跳出循环。
			}

		}

		if (flag == 1) //循环判断完后,根据变量的值,判断i是不是素数,是则打印
		{
			printf("%d ", i);
		}
	}


	return 0;
}

Realize the effect:

Summarize:

(1). Exercise the use of the outer loop and inner loop , and call the variables of the inner and outer loops .

            

(2). Trial division : use % to see the remainder and use it flexibly .

                    

 =========================================================================

                       

Idea 2:

general idea:

Because even numbers are not prime except for 2 , and there is no 2 in the topic range ,

Therefore, only odd numbers between 100 and 200 can be generated , and half of the numbers can be excluded .

The efficiency is doubled .

                      


                     

first step:

Only on the basis of idea one ,

Just change the initialization part and adjustment part of the outer loop,

Make the outer loop only generate odd numbers between 100 and 200 .

                     

Implementation code:

#include <stdio.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 101; i <= 200; i+=2) //生成 100~200 之间的奇数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= i - 1; j++) //设置内循环:生成 2~i-1 的数
		{

			if (i % j == 0) //在内循环中设置 if条件判断语句,判断i是否为素数
				//用 i 模上一个 j,看 j 能不能整除 i ,有余数则表示不能整除
			{
				flag = 0; //flag == 0,则i不是素数
				break;
				//只要有一个 j 把 i 整除了,说明 i 已经不是素数了,
				//所以不用再继续循环了,使用break跳出循环。
			}

		}

		if (flag == 1) //循环判断完后,根据变量的值,判断i是不是素数,是则打印
		{
			printf("%d ", i);
		}
	}


	return 0;
}

Realize the picture:

                    

Idea 2: Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 101; i <= 200; i+=2) //生成 100~200 之间的奇数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= i - 1; j++) //设置内循环:生成 2~i-1 的数
		{

			if (i % j == 0) //在内循环中设置 if条件判断语句,判断i是否为素数
				//用 i 模上一个 j,看 j 能不能整除 i ,有余数则表示不能整除
			{
				flag = 0; //flag == 0,则i不是素数
				break;
				//只要有一个 j 把 i 整除了,说明 i 已经不是素数了,
				//所以不用再继续循环了,使用break跳出循环。
			}

		}

		if (flag == 1) //循环判断完后,根据变量的值,判断i是不是素数,是则打印
		{
			printf("%d ", i);
		}
	}


	return 0;
}

Realize the effect:

                     

 =========================================================================

                       

Idea three:

general idea:

a number: k ,

If k = m * n ,

Then there must be m or n less than root k ,

Then you can put the previous 2 ~ i-1

Change to sqrt(i) , that is, the root sign i ,

Further improve efficiency .

                   


                     

first step:

Only on the basis of the second idea ,

 Just change the judgment condition part of the inner loop,

Reduce the number of executions of the inner loop .

                     

Implementation code:

#include <stdio.h>
#include <math.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 101; i <= 200; i += 2) //生成 100~200 之间的奇数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= sqrt(i); j++) //设置内循环:生成 2~i-1 的数
		{

			if (i % j == 0) //在内循环中设置 if条件判断语句,判断i是否为素数
				//用 i 模上一个 j,看 j 能不能整除 i ,有余数则表示不能整除
			{
				flag = 0; //flag == 0,则i不是素数
				break;
				//只要有一个 j 把 i 整除了,说明 i 已经不是素数了,
				//所以不用再继续循环了,使用break跳出循环。
			}

		}

		if (flag == 1) //循环判断完后,根据变量的值,判断i是不是素数,是则打印
		{
			printf("%d ", i);
		}
	}


	return 0;
}

Realize the picture:

                    

Idea 3: Final code and implementation effect

Final code:

#include <stdio.h>
#include <math.h>
int main()
{
	int i = 0; //外循环变量
	for (i = 101; i <= 200; i += 2) //生成 100~200 之间的奇数
	{
		int flag = 1; //设置变量flag

		int j = 0; //内循环变量
		for (j = 2; j <= sqrt(i); j++) //设置内循环:生成 2~i-1 的数
		{

			if (i % j == 0) //在内循环中设置 if条件判断语句,判断i是否为素数
				//用 i 模上一个 j,看 j 能不能整除 i ,有余数则表示不能整除
			{
				flag = 0; //flag == 0,则i不是素数
				break;
				//只要有一个 j 把 i 整除了,说明 i 已经不是素数了,
				//所以不用再继续循环了,使用break跳出循环。
			}

		}

		if (flag == 1) //循环判断完后,根据变量的值,判断i是不是素数,是则打印
		{
			printf("%d ", i);
		}
	}


	return 0;
}

Realize the effect:

Guess you like

Origin blog.csdn.net/weixin_63176266/article/details/131139479