Need faster program (time limit exceed)

I have the following program that checks if a number and the sum of its digits is prime. I would like some advice on how to make it more efficient.

The problem MUST USE the function bool es_primer_perfecte(int n) and the code in int main () CANNOT BE CHANGED

For easier programming comprehension:
bool primer (int n) this checks if a number is prime.
bool es_primer_perfecte(int n) sends input to check if it is prime. If it is prime, sum the digits of the input into a variable called "resultat" (resultat = result) and check if "resultat" is prime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

bool primer (int n) {
	if (n <= 1) return false;
	for (int i = 2; i*i <= n; ++i) if (n%i == 0) return false;
	return true;
}

bool es_primer_perfecte(int n) {
	if (not primer (n)) return false;
	else {
		int resultat = 0;
		while (n != 0) {
			int copy_n = n%10;
			resultat += copy_n;
			n /= 10;
		}
		if (not primer (resultat)) return false;
		else return true;
	}
}

int main () {
	int number;
	while (cin >> number) cout << es_primer_perfecte (number) << endl;
}
Last edited on
Anyways, how do you expect someone to help you if your code is full of foreign languages? You will be very likely ignored.

You can guess what each function is doing if you follow the logic. Even the name of the function is vaguely similar to English. You don't need to be able to understand the language to be able to know what the function is doing. The intent is as clear as day in each function.

Anyway, I've added a timer to test your program and I don't think you can make it any faster.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Timer
{
public:
    Timer( )
    {
        start = chrono::system_clock::now( );
    }
    
    ~Timer( )
    {
        auto end = chrono::system_clock::now( );
        double elapsed{ chrono::duration_cast< chrono::duration<double, milli> >( end - start ).count( ) };
        cout << scientific << setprecision( 2 ) << "\t" "elapsed = " << elapsed << "ms\n";
    }
    
private:
    chrono::time_point<chrono::system_clock> start;
};

// ...

int main () {
	int number;
	while (cin >> number) {
	    bool is_prime{};
	    {
	        Timer t{};
	        is_prime = es_primer_perfecte(number);
	    }
	    cout << is_prime << "\n";
	}
}


1234567
	elapsed = 7.16e-04ms
0
1234567
	elapsed = 8.22e-04ms
0
1234567
	elapsed = 6.99e-04ms
0
1234567
	elapsed = 1.32e-03ms
0
1234567
	elapsed = 7.61e-04ms
0
Come on, I think that my explanation was clear enough:
I have the following program that checks if a number and the sum of its digits is prime.

What you do not understand about my explanation? Anyway, changed main post with some extra information about my code.



There has to be a way to make it faster because if there was not, my program would have been accepted and that's not the case. What I could only think off is the following but it will not make a big change in execution time:

Changing this:
1
2
3
4
5
while (n != 0) {
	int copy_n = n%10;
	resultat += copy_n;
	n /= 10;
}


For this:
1
2
3
4
while (n != 0) {
	resultat += n%10;
	n /= 10;
}
Last edited on
There are a number of methods and algorithms that can speed up your program, and all of them can be found with a quick google search.
Topic archived. No new replies allowed.