Interesting C language problem-judging prime numbers

Algorithm requirements

Implement a function to determine whether a number is a prime number.
Use the function implemented above to print prime numbers between 100 and 200.

Algorithm ideas

First understand the concept: what is a prime number?
Prime number: Prime number. Except 1 and itself, if there is no other divisor, the data is prime. Generally speaking, a number can only be divisible by 1 and itself . For example: prime numbers within ten: 1, 2, 3, 5, 7, 9. The above number can only be divisible by 1 and the number itself.

Compiler Environment

Win10,VS2015

method one

The simplest and most direct way is to try one by one. From 1 to traverse one by one to the number itself. Code:

int IsPrimeNumber1(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin; i < end; i++) {
    
    
		int j = 0;
	//判断i是否为素数:用[2, i)之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j < i; j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
	// 上述循环结束之后,如果j和i相等,说明[2, i)之间的所有数据都不能被i整除,则i为素数
		if (j == i) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}

The defect of the above method: more than half of i data is definitely not a multiple of i, and many meaningless calculations are performed above .
For example, if half of 10 is 5, there is no need to detect numbers greater than 5, that is, 5~10, because numbers over 5 cannot be multiples of 10. 6x2=12, 7x2=14, these numbers have exceeded 10 by itself.

Method Two

Every time you get a piece of data, you only need to check it: whether there is an element in the interval [2, i/2] that can be divisible by 2i, which shows that i is not a prime number

int IsPrimeNumber2(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin; i < end; i++) {
    
    
		int j = 0;
		//判断i是否为素数:用[2, i/2]之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j <= i/2; j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
		// 上述循环结束之后,如果j大于i/2,说明[2, i/2]之间的所有数据都不能被i整除,则i为素数
		if (j >i/2) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}

Method two still contains some duplicate data, and then optimize: the reasons can be seen in method three.

Method Three

If i can be divisible by any data between [2, sqrt(i)], then i is not a prime number.
Reason: If m can be divisible by any integer between 2 ~ m-1, one of its two factors must be less than or Equal to sqrt(m), and the other is greater than or equal to sqrt(m).

int IsPrimeNumber3(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin; i < end; i++) {
    
    
		int j = 0;
		//判断i是否为素数:用[2, sqrt(i)]之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j <= sqrt(i); j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
		// 上述循环结束之后,如果j大于sqrt(i),说明[2, sqrt(i)]之间的所有数据都不能被i整除,则i为素数
		if (j > sqrt(i)) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}

Method Four

Continue to optimize method three, as long as i is not divisible by any data between [2, sqrt(i)], i is a prime number, but in actual operation i need not gradually increase from 101 to 200, because in addition to 2 and 3 In addition, no two consecutive adjacent data will be prime at the same time!
Note: This method i starts counting from 101

int IsPrimeNumber4(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin+1; i < end; i+=2) {
    
    
		int j = 0;
		//判断i是否为素数:用[2, sqrt(i)]之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j <= sqrt(i); j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
		// 上述循环结束之后,如果j大于sqrt(i),说明[2, sqrt(i)]之间的所有数据都不能被i整除,则i为素数
		if (j > sqrt(i)) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}

Overall code

#include <stdio.h>
#include <windows.h>
#include <math.h>
#pragma warning(disable:4996)

int IsPrimeNumber1(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin; i < end; i++) {
    
    
		int j = 0;
	//判断i是否为素数:用[2, i)之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j < i; j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
	// 上述循环结束之后,如果j和i相等,说明[2, i)之间的所有数据都不能被i整除,则i为素数
		if (j == i) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}

int IsPrimeNumber2(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin; i < end; i++) {
    
    
		int j = 0;
		//判断i是否为素数:用[2, i/2]之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j <= i/2; j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
		// 上述循环结束之后,如果j和i/2相等,说明[2, i/2]之间的所有数据都不能被i整除,则i为素数
		if (j >i/2) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}

int IsPrimeNumber3(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin; i < end; i++) {
    
    
		int j = 0;
		//判断i是否为素数:用[2, sqrt(i)]之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j <= sqrt(i); j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
		// 上述循环结束之后,如果j大于sqrt(i),说明[2, sqrt(i)]之间的所有数据都不能被i整除,则i为素数
		if (j > sqrt(i)) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}

int IsPrimeNumber4(int begin, int end) {
    
    
	int i = begin;
	int count = 0;
	//外层循环用来获取100~200之间的所有数据
	for (i = begin+1; i < end; i+=2) {
    
    
		int j = 0;
		//判断i是否为素数:用[2, sqrt(i)]之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		for (j = 2; j <= sqrt(i); j++) {
    
    
			if (i%j == 0) {
    
    
				break;
			}
		}
		// 上述循环结束之后,如果j大于sqrt(i),说明[2, sqrt(i)]之间的所有数据都不能被i整除,则i为素数
		if (j > sqrt(i)) {
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount=%d\n", count);
	return 1;
}


int main() {
    
    
	int begin = 100;
	int end = 200;
	IsPrimeNumber1(begin,end);
	IsPrimeNumber2(begin, end);
	IsPrimeNumber3(begin, end);
	IsPrimeNumber4(begin, end);
	system("pause");
	return 0;
}

operation result:

>> 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
>> count=21

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/109216817