Code optimization in complete number, statistical prime number and number problem

Posted by Wayne on Thu, 06 Jan 2022 16:06:08 +0100

When we finish all the problems for the first time, we may encounter TLE (time overrun), so write this article to analyze it in depth and put forward good solutions.

The problem of perfect numbers is as follows:

If the sum of all divisors of an integer other than itself is equal to the number, then we call the integer a perfect number.

For example, 6 is a perfect number because the sum of its divisors other than itself is 1 + 2 + 3 = 6.

Now, given N integers, please judge whether these numbers are complete in turn.

Input format

The first line contains the integer N, indicating that there are N test cases in total.

The next N lines contain an integer X that you need to judge.

Output format

Each test case outputs a result, and each result occupies one line.

If the test data is a perfect number, X is perfect is output, where XX is the test data.

If the test data is not a perfect number, X is not perfect is output, where XX is the test data.

Data range

1 ≤ N ≤ 100
1 ≤ X ≤ 10^8

sample input

3
6
5
28

Output example:

6 is perfect
5 is not perfect
28 is perfect

First, give the common writing method, that is, the writing method of most students:

#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
	int n, x, i, sum;

	cin >> n;
	while (n)
	{
		sum = 0;
		cin >> x;
		for (i = 1; i < x; i++)
		{
			if (x % i == 0)
				sum += i;
		}
		if (sum == x)
			printf("%d is perfect\n", x);
		else
			printf("%d is not perfect\n", x);
			n--;
	}
	return 0;
}

When submitting, you will find that the time limit is exceeded! Why? Next, let's analyze:

When writing code in C + +, the clock will calculate 1e8 times per second, that is, 100 million times. If n = 100 and x = 10 ^ 8, a total of 10 billion times will be calculated, which far exceeds the time limit. How to optimize the code?

First, suppose D is a divisor of X, then x / d is also a divisor of X. For example, 2 is the divisor of 12, and 12 / 2 = 6 is also the divisor of 12, so only enumerating the small divisor can reduce the amount of computation. That is, d < = x / d is d * d < = x is d < = root X.

code implementation

#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
	int n, x, i, sum;

	cin >> n;
	while (n--)
	{
		sum = 0;
		cin >> x;
		for (i = 1; i * i <= x; i++)
		{
			if (x % i == 0)
			{
				if (i < x)
					sum += i;
				if (i != x / i && x / i < x)
					sum += x/i;
			}
		}
		if (sum == x)
			printf("%d is perfect\n", x);
		else
			printf("%d is not perfect\n", x);
	}
	return 0;
}

Note: 1 Each if statement in the for statement must judge I < x to avoid outputting , 1 is perfect when x = 1 is read in.      2. I in the second if= X / I is to avoid adding 6 repeatedly when x is 36 (divisors 6 and 6).

This optimization method is also used for the classical problem of counting the number of primes:

subject

A natural number greater than , is called a prime number if it cannot be divided by other natural numbers except , 1 , and itself.

For example, 7 is a prime number because it can only be divided by 1 and 7.

Now, given that you have N , natural numbers greater than , 1 , please judge whether these numbers are prime numbers in turn.

Input format

The first row contains the integer N, indicating that there are N test data in total.

Next, N , lines, each containing a natural number , X.

Output format

Each test case outputs a result, and each result occupies one line.

If the test data is a prime number, output , X is prime, where , XX , is the test data.

If the test data is not a prime number, output , X is not prime, where , XX , is the test data.

Data range

1 ≤ N ≤ 100,
1 < X ≤ 10^7

#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
	int n, x, i;
	bool is_prime = true;

	cin >> n;
	while (n--)
	{
		cin >> x;
		is_prime = true;
		for (i = 2; i * i <= x; i++)
		{
			if (x % i == 0)
			{
				is_prime = false;
				break;
			}
		}
		if (is_prime == true)
			printf("%d is prime\n", x);
		else
			printf("%d is not prime\n", x);
	}
	return 0;
}

Topics: C++ Visual Studio Code