Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have a new algorithm to find factors or primes in linear time - need verification for this

I have developed an algorithm to find factors of a given number. Thus it also helps in finding if the given number is a prime number. I feel this is the fastest algorithm for finding factors or prime numbers.

This algorithm finds if a give number is prime in time frame of 5*N (where N is the input number). So I hope I can call this a linear time algorithm.

How can I verify if this is the fastest algorithm available? Can anybody help in this matter? (faster than GNFS and others known)

Algorithm is given below

Input: A Number (whose factors is to be found)
Output: The two factor of the Number. If the one of the factor found is 1 then it can be concluded that the
Number is prime.

Integer N, mL, mR, r;
Integer temp1; // used for temporary data storage
mR = mL = square root of (N);
/*Check if perfect square*/
temp1 = mL * mR;
if temp1 equals N then
{
  r = 0; //answer is found
  End;
}
mR = N/mL; (have the value of mL less than mR)
r = N%mL;
while r not equals 0 do
{
  mL = mL-1;
  r = r+ mR;

  temp1 = r/mL;
  mR = mR + temp1;
  r = r%mL;
}
End; //mR and mL has answer

Please provide your comments.. dont hesitate to contact me for any more information.

Thanks, Harish http://randomoneness.blogspot.com/2011/09/algorithm-to-find-factors-or-primes-in.html

like image 883
harish Avatar asked Apr 07 '11 12:04

harish


3 Answers

"Linear time" means time proportional to the length of the input data: the number of bits in the number you're trying to factorize, in this case. Your algorithm does not run in linear time, or anything close to it, and I'm afraid it's much slower than many existing factoring algorithms. (Including, e.g., GNFS.)

like image 82
Gareth McCaughan Avatar answered Dec 06 '22 10:12

Gareth McCaughan


The size of the input in this case is not n, but the number of bits in n, so the running time of your algorithm is exponential in the size of the input. This is known as pseudo-polynomial time.

like image 41
hammar Avatar answered Dec 06 '22 10:12

hammar


I haven't looked closely at your algorithm, but prime number tests are usually faster than O(n) (where n is the input number). Take for example this simple one:

def isprime(n):
   for f in range(2,int(sqrt(n))):
      if n % f == 0:
         return "not prime"
   return "prime"

Here it is determined in O(sqrt(n)) if n is prime or not, simply by checking all possible factors up to sqrt(n).

like image 26
sth Avatar answered Dec 06 '22 10:12

sth