find the least prime factor of a given number code example

Example: 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

Tags:

Misc Example