Problem 3 Largest prime factor (factorization factor) __problem

Source: Internet
Author: User

largest prime factor Problem 3

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

What is the largest prime factor of the number 600851475143?

Answer: 6857

Exercises

Let the given number is n and let k = 2, 3, 4, 5, .... For all k, if it is a factor of n then we divide n by K and completely divide out each k before moving to the next K. It can be seen then K is a factor it'll necessarily be prime, as all smaller factors the have been removed, andthe FINA L result of the This process would be n = 1.

Code:

#include <bits/stdc++.h>
using namespace std;
Long Long num = 600851475143ll;
int main ()
{
	long i, max = 1;
	for (i = 2; I * i <= num; i++)
	{
		if (num% i = = 0)
		{
			max = i;
			while (num% i = = 0) num/= i;
		}
 	 if (num > 1) max = num;
	}		
	cout<<max<<endl;
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.