Talk:Prime factorization algorithm

message: The code above, as written, would work; however, this is partly lucky.  The isPrime function was inaccurate, as range doesn't include the higher end, so e.g. if checking for primality of 9, it would try numbers from 2 to 2, and conclude it was prime.  I've added 1 to the upper end of the range so that the isPrime function works, in case anyone else comes along and tries to use it.  The overall function worked only because it only picked off the smallest prime factor each time (and it could be sped up by not trying smaller prime factors than were found last time). --PsyMar (talk) 11:04, 2 March 2016 (UTC)
replies: text: Wait, is xrange a thing in python2 maybe?  I had to change it to range to make it work under python3, maybe this was the problem. --PsyMar (talk) 11:06, 2 March 2016 (UTC)
replies: text: This comment is just to show up in the history, as I accidentally marked my "bugfix" comment minor. --PsyMar (talk) 11:07, 2 March 2016 (UTC)
thread_title:  small bugfix 

message: If were going to have examples shouldn't they actually work? The C++ example wouldn't even compile, unless their is a new version of C++ that doesn't use namespaces or require an int return from main()? If you fix those minor errors however the example seesm to be putting out nonsense; id est, giving the same primes for output no matter what the input. I wish life were that easy. :D (I didn't check the logs, but I'm thinking this was originally a C example someone haphazardly converted to C++, or someone has a goat for a compiler.)
I'm putting it here until I or someone else can fix it.
#include <iostream>
#include <cmath>
#include <stdlib.h>
using std::cout;
using std::endl;
int isPrime(int n)
{
  for(int ind=2;ind<=sqrt(n);ind++) {
    if (n%ind==0) {
      return true;
    }
  }
  return false;
}
int main(int argc, char *argv[])
{
  // check argc to make sure we recieved a value.
  int n = atoi(argv[1]);
  for(int i=2;i<=n;i++)
  {
    while ( !isPrime(i) && n%i == 0 )
    {
      cout << i << " ";
      n=n/i;
    }
  }
  cout << endl;
  return 0;
}
: I agree. This code is not useful. Seems like it could also use some cleanup as to the formatting. JamesJNHu (talk) 16:39, 23 September 2014 (UTC)
thread_title:  C++ Example 

message: I think there's a small contradiction between the paragraph "description" and "example". The descriptions says that if n is prime, then you have the factorization and you stop there; but in the example, when n become prime (i.e when n = 13), it's still testing if n can be divided cleanly by the next primer number (11). So, maybe someone should think about changing a little bit the example (must say it's a good example but like i try to explained (i'm french) it's a bit in contradiction with the description of the algorithm).
Oh and by the way, the C++ example is a bit funny, i don't see any object-oriented programmation in this, i wonder why it wasn't write in C... :)
Etienne —The preceding unsigned comment was added by 69.159.129.147 (talk) 20:31, 5 March 2007 (UTC)
replies: text: It's sometimes convenient to use C++ just for the standard classes, like vector or iostream.  CjPuffin  00:15, 6 March 2007 (UTC)
thread_title:  A small contradiction 

message: I'm a senior university student in computer science and I know a bit about this topic. The big algorithms are very complex and difficult to read. So I decided to post a much simpler algorithm. After some very careful study, I don't think this algorithm is any slower then the big one posted on the main page. It could even be faster...
#Python code to factor a number
#This code can factor any number of any length... eventually.
#This is because python has built in arbitrarily length numbers.
def factor(x):
    factors = []
    i = 2
    while x > 1:
        if x % i == 0:
            x = x / i
            factors.append(i)
        else:
            i += 1
    return factors
This algorithm attempts to divide the number in question by all positive integers starting at 2. Although the algorithm will admittedly try to pull out factors of non prime numbers, it is guaranteed not to.
(I believe the following theory is correct. Let me know if I'm mistaken because I just came up with this to simplify that massive algorithm.)
Proof:
Lets assume that the algorithm tries to factor out a non prime number, say, 15. Because 15 is not prime, it will have factors less then 15, namely 3 and 5. Trying to factor out 15 is the same as trying to factor out 3 and 5 at once. However, because 3 and 5 are less then 15, the algorithm would have already factored them out. This means that the number 15 will never be factored by the algorithm.
In general any composite number will have factors less then the composite. Because all factors of the composite will have been removed from the number being factored before hand, the composite will not factor out. Therefor, the algorithm can only succeed in factoring out prime numbers. :D
Assuming the above prof is correct, the original number will have all of its factors removed and be reduced to 1. When that happens the algorithm terminates. This more or less proves that the algorithm will halt when the number is factored and not keep going on forever.
A Bug
</pre>
one more comment on the above algorithm, when try 1234567890987654321
it output: [3, 4, 4, 4, 17, 29, 431, 463, 2557, 25561], where 4 is not prime.  — Preceding unsigned comment added by 128.120.52.135 (talk) 21:10, 27 August 2013 (UTC)
replies: text: This is the trial division algorithm. The version you describe, on a b-bit number, may take as many as 2b iterations (for instance, it will do so whenever its input is prime). It may be sped up to roughly 2b/2 by stopping and giving up when i is around the square root of the remaining value x, but this is still much much slower than state-of-the-art algorithms. In any case, if you actually had a new algorithm, rather than a reinvention of the wheel, this would not be the place to promote it. This talk page should be for discussion of improvements to the Wikipedia article only, not for discussion of improvements to the topic that the article is about. —David Eppstein (talk) 21:06, 11 December 2013 (UTC)
thread_title:  A better algorithm