find prime factorization code example
Example 1: prime factorization in c
#include <stdio.h>
#define MIN 100
#define MAX 100000
int main(){
int pdiv=2,j;
for (int num=MIN;num<=MAX;num++){
printf("The prime factors of %d are:\n",num);
j=num;
do {
if (j%pdiv==0)
{
printf("%d\n",pdiv);
j=j/pdiv;
}else
{
pdiv++;
}
}while(j>1);
pdiv=2;
}
}
Example 2: program to find the largest prime factor of a number
## to find the greatest factor of “A” in the the range up to “B”
1. #include<iostream>
2. #include<algorithm>
3. #include<math.h>
4. using namespace std;
5. int main()
6. {
7. int T;
8. cin>>T;
9. while(T--)
10. {
11. int A,B;
12. cin>>A>>B;
13. int mn=A;
14. for(int i=1;i<=sqrt(A);i++)
15. {
16. if(A%i==0)
17. {
18. if(i<=B)
19. {
20. mn=min(mn,A/i);
21. }
22. if(A/i<=B)
23. {
24. mn=min(mn,i);
25. }
26. }
27. }
28. cout<<mn<<endl;
29. }
30. return 0;
31. }
use the Sieve of Eratosthenes