Why is the brute force factoring algorithm exponential time?

In summary: This problem is known as the "factoring problem" and is a major area of research in mathematics and computer science.In summary, the conversation discusses a factoring algorithm and its analysis, including its time complexity and comparison to other algorithms. The goal is to find an algorithm that can factor numbers in polynomial time, but the best known algorithms are subexponential. The topic is also known as the "factoring problem" and is an active area of research.
  • #1
michael879
698
7
ok so I thought of this factoring algorithm, and I did some quick analysis on it (attached picture, time is in seconds * 5). It appears to be polynomial. However, when I see the basic brute force factoring algorithm I think that's also polynomial. So before I get excited over nothing. Can someone explain to me why the brute force factoring algorithm shown below is exponential time? What I get is approximately O(n^m) where n is the number and m is the number of factors.

Code:
brute-force(n)
{
   for(i = 2; i < n/2; i++)
   {
      if(n%i == 0)
         return CONCAT(brute_force(i), brute_force(n/i));
   }
   return n;
}

basically it searches till it finds a factor, and then it returns a list of the concatination of the factors of the two factors it found (i and n/i). For prime numbers it just returns the number. The main loop is obviously O(n). And as far as I can tell the function is recursively called on the order of m where m is the number of factors. This gives O(n^m).
 
Mathematics news on Phys.org
  • #2
attachment got messed up here it is.
 

Attachments

  • factor.JPG
    factor.JPG
    67 KB · Views: 547
  • #3
That program has running time roughly proportional to the largest factor of the number, which in the worst case is the whole number if it's prime. (If you check for primality first that reduces it massively to the square root of the number in the worst case.)

However much faster algorithms are known. The ideal algorithm, which is probably impossible*, would run in polynomial time. But "polynomial time" in this case means polynomial in the size of the input.

If you have a b-bit number n, you have [itex]2^{b-1}\le n<2^b[/itex]. Your algorithm then has run time [itex]\mathcal{O}(2^b)[/itex] (or [itex]\mathcal{O}(\sqrt2\,^b)[/itex] if you check for primes), while there are algorithms that are [itex]\mathcal{O}((1+\epsilon)^b)[/itex] for all [itex]\epsilon>0[/itex] -- that is, they are subexponential.

* This has not been proven.
 
  • #4
CRGreathouse said:
That program has running time roughly proportional to the largest factor of the number

doesnt that make that program O(n)??
 
  • #5
While you're on the topic of brute-force factoring, here's a way to make the program much faster for numbers with small factors. (Without checking to see if factors are prime, you'll never factor numbers with large prime factors quickly.) This checks only two numbers every six (speeding it up by a factor of 3) and doesn't start over every time it finds a new factor.

Code:
brute-force2(n)
{
   if (n%2 == 0)
      return CONCAT(2, brute_force2(n/2));
   if (n%3 == 0)
      return CONCAT(2, brute_force2(n/2));

   return brute_force3(n, 5);
}

brute_force3(n, k)
   for(i = k; i * i < n; i += 6)
   {
      if(n%i == 0)
         return CONCAT(i, brute_force3(n/i, i));
      if(n%(i+2) == 0)
         return CONCAT(i+2, brute_force3(n/(i+2), i));
   }
   return n;
}
 
  • #6
michael879 said:
doesnt that make that program O(n)??

O(n) but O(k^b). Being polynomial in n is easy; being polynomial in b is hard (probably impossible).
 
  • #7
o are you saying that factoring run times are based on log2(n) rather than n? well no wonder theyre all exponential... your taking a function that's polynomial and logging x...
 
  • #8
ok so yea my function is O(2E-09e^1.3102b) then. thanks for the clarification.
 
  • #9
so what the problem really is is to find an algorithm that factors in O(log(n))? That clears up a lot...
 
  • #10
michael879 said:
so what the problem really is is to find an algorithm that factors in O(log(n))? That clears up a lot...

The best general-purpose factoring algorithms now known are slower than O(b^k) for all k but faster than O(k^b) for all k > 0. They're subexponential but superpolynomial.
 

Related to Why is the brute force factoring algorithm exponential time?

1. What is a factoring algorithm and why is it important?

A factoring algorithm is a mathematical method used to break down a given number into smaller prime factors. This is important because it allows us to find the prime factors of a number, which is useful in many fields such as cryptography, computer science, and physics.

2. How does the time complexity of a factoring algorithm affect its efficiency?

The time complexity of a factoring algorithm refers to how long it takes for the algorithm to find the prime factors of a number. The lower the time complexity, the more efficient the algorithm is. This is because a lower time complexity means the algorithm can find the factors in a shorter amount of time.

3. What is the fastest factoring algorithm currently known?

The fastest factoring algorithm currently known is the General Number Field Sieve (GNFS). It is able to factor large numbers in sub-exponential time, making it significantly faster than other factoring algorithms such as the Quadratic Sieve.

4. Can factoring algorithms be used for encryption and decryption?

Yes, factoring algorithms can be used for encryption and decryption in certain cases. For example, the RSA encryption method relies on the difficulty of factoring large numbers to keep information secure. However, not all factoring algorithms are suitable for encryption and decryption.

5. What are some real-world applications of factoring algorithms?

Factoring algorithms have many real-world applications, including cryptography, code-breaking, and data compression. They are also used in fields such as number theory, computer science, and physics to solve complex problems and equations.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
Replies
6
Views
1K
Replies
9
Views
1K
Replies
4
Views
7K
  • General Math
Replies
1
Views
1K
Replies
8
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Replies
19
Views
3K
  • General Math
Replies
2
Views
9K
Replies
6
Views
1K
Back
Top