Factoring by coding

 

 

Write a program to factor huge integers. Read an integer N from the command line, then print it as the product of two integers (or note that it is prime) as in the following brute-force solution:

Save your time - order a paper!

Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlines

Order Paper Now

#include <stdio.h>
#include <limits.h>
main(int argc, char *argv[]) {
int d;
int N = atoi(argv[1]);
for (d = 2; d < N/d; d++){
if (N % d == 0) break;
}
if (N % d == 0)
printf(“%d = %d*%dn”, N, d, N/d);
else printf(“%d is primen”, N);
}
This code simply checks each integer greater than 1 to see whether it divides evenly into N. The header limits.h gives access to constants like INT_MAX that are not used by this program, but which you may need to avoid overflow in your own code.
If you use Python , this step is necessary, with Java you may use BigInteger.

We stop after finding any factor since we know that we could find all factors, if desired, by using the same method for the factors.

The solution just given is not quite a brute-force solution because it already contains a test that improves performance by a substantial amount:
If we do not find a factor by the time that we reach the square root of N, then we certainly will not find a factor larger than that value, and we can declare N to be prime.
A truly naive brute-force implementation might use time proportional to N (try every integer less than N; this improvement improves the running time to be proportional to the square root of N, so we can use it to factor any 32-bit integer in a fraction of a second.
In this assignment, we explore the problem of factoring larger integers.

There are two reasons that the above code is not useful for factoring huge integers. First, the standard int data type supports 32-bit integers, so we would get overflow and/or incorrect results if we were to try to use it for larger numbers.
Second, it is too slow. Even if we could deal with (say) 128-bit integers, we could never afford to try 2^64 divide operations to check for factors. That’s not the case with Python or Java BigInteger.
For this assignment, you will develop an implementation of a faster algorithm for factoring that is known as Pollard’s rho method.
The method is brought to us by the magic of number theory that is beyond the scope of this course, but it is an easy computation, based on iterating the function f(x) = x*x + c (mod N).

The post Factoring by coding first appeared on COMPLIANT PAPERS.