Problem3


Problem Setting:

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


My thinking:
Step 1  How to check factor of one number ?     mode operation
Step 2 How to check whether a number is prime?  Primality test

Primality Test:

There are several methods for testing primality of integers. The best choice depends on the circumstances. Some of the methods are faster than others, while some popular tests are actually only probabilistic algorithms that will occasionally falsely characterize a number as prime or composite. 
http://www.wikihow.com/Check-if-a-Number-Is-Prime

Try to use Trial Division here,
Trial division is the simplest test for primality. It is based on the definition of a prime number. A number is prime if it has no divisors other than itself and one.

Step 1: Let n be the number you want to test.
Step 2: Divide n by 2. If the result is an integer, then n is not prime because 2 is a factor of n. Look at the last digit and if it's an even number, it's divisible by 2. If not, continue.
Step 3:  If the result is an integer, then n is not prime because 3 is a factor of n. If not, continue.
Step 4: Continue dividing n by each number between 2 and n1/2 inclusive. If any of them divide evenly, then n is not prime because you found a factor. If n has no factors less than its square root, then n is prime. It is sufficient to check only for divisors less than or equal to n1/2 because if n = a*b, then a and b can't both exceed the square root of n.

This can be optimised by skipping the test division by numbers which are obviously not prime. For example, skip every even number greater than two and every multiple of three greater than three.

Ok then everything theoretical is prepared!

bool testPrimality(int n){
	if(n%2==0&&n!=2) return false;
	for(int i=3;i<=sqrt(n);){
        if(n%i==0) return false;
		i+=2;
	}
	return true;
}

When it comes to next step. There is another problem arose.
How to handel large number like 600851475143!

My simple solution to this number is to use a long long integer(g++compiler). 
http://www.360doc.com/content/10/0407/18/59141_21980461.shtml

 It really widens the range of integer which can be represented. But it still takes too long to compute

int main() {

clock_t startTime = clock();

	long long gf=1;// greatest factor
	//long target=600851475143;
	long long target=60085147514;
			//600851475143;

	for(long long i=1;i<=target;i++){
		if(target%i==0 && testPrimality(i)==true)
			gf=i;
	}


cout<<gf<<endl;

cout << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
	return 0;
}





It seems we need another way to get factor of a number.
Step 1 start from i=2, target
Step 2 test if target%i==0, yes then i is a factor of the number, target=target/i
Step 3 continue Step 2 until test fails
Step 4 i=i+1
Step 5 restart from Step 2 until i>target 

int main() {

clock_t startTime = clock();

	long long gf=1;// greatest factor
	//long target=600851475143;
	long long target=600851475143;
	long i=2;


	while(i<=target){
		// factor
		if(target%i==0){
			target/=i;
			if (testPrimality(i)) gf=i; //prime
		}
		else i++;
	}

cout<<gf<<endl;

cout << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;


	return 0;
}

get the correct answer:6857








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值