Commit da36e62c authored by Romain Lefèbvre's avatar Romain Lefèbvre

fonction de prime facto la plus rapide des 3.

parent 11055c44
# include <stdio.h>
#include <math.h>
#include <sys/time.h>
// A function to print all prime factors of a given number n
# include <stdio.h>
# include <math.h>
// A function to print all prime factors of a given number n
void primeFactors(int n)
{
int count1=0;
int count2=0;
while (n%2 == 0)
{
if(count1==0) printf("%d ", 2);
n = n/2;
count1+=1;
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
if(count2==0) printf("%d ", i);
n=n/i;
count2+=1;
}
count2=0;
}
if (n > 2)
printf ("%d ", n);
}
/* Driver program to test above function */
int main() {
struct timeval stop, start;
gettimeofday(&start, NULL);
int n = 666343;
primeFactors(n);
printf("\n");
int b = 463698;
primeFactors(b);
printf("\n");
int c = 1021406;
primeFactors(c);
printf("\n");
int d = 506156;
primeFactors(d);
printf("\n");
int e = 913231;
primeFactors(e);
printf("\n");
int f = 268205;
primeFactors(f);
printf("\n");
int g = 982865;
primeFactors(g);
printf("\n");
int h = 917451;
primeFactors(h);
printf("\n");
gettimeofday(&stop, NULL);
printf("%lu", (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment