The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? code example

Example 1: he prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

def max_factor(num):
    """Find the maximum prime factor."""
    factor = 2
    while factor * factor <= num:
        while num % factor == 0:
            num /= factor
        factor += 1
    if (num > 1):
        return num
    return factor
    
print max_factor(33) #11
print max_factor(38) #19
print max_factor(600851475143) #6857

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

Tags:

C Example