Jump to content

[Ajuda] Optimizar Código


Recommended Posts

Posted

Boas.

O objectivo do código é este (problema 3 do ProjectEuler para quem conhecer):

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Eu fiz este programa:

#include <stdio.h>

int primo(int n)
{
int i,j=1;

for(i=2;i<n/2;i++)
{
	if(n%i==0)
	{
		j=0;
		break;
	}
	else
		continue;
}
return j;
}

int main()
{
long int n,i;

printf("Nº: ");
scanf("%ld",&n);

for(i=n/2;i>0;i--)
{
	if(primo(i))
	{
		if(n%i==0)
		{
			printf("%ld\n",i);
			break;
		}
		else
			continue;
	}
	else
		continue;
}					
return 0;
}		

O problema é que isto já esteve mais de 2horas a correr e nada.

Aliás, estou a testar agora com o número 15 134 665 (o resultado é 37) e, ao fim de 13 min, ainda não acabou de correr.

Alguém me ajuda a optimizar o código sff?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.