<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Talk%3APrime_factorization_algorithm</id>
	<title>Talk:Prime factorization algorithm - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Talk%3APrime_factorization_algorithm"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Talk:Prime_factorization_algorithm&amp;action=history"/>
	<updated>2026-10-26T03:47:01Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Talk:Prime_factorization_algorithm&amp;diff=955371&amp;oldid=prev</id>
		<title>imported&gt;Xonqnopp: Reverted 1 edit by 81.34.226.181 (talk) to last revision by PsyMar. (TW)</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Talk:Prime_factorization_algorithm&amp;diff=955371&amp;oldid=prev"/>
		<updated>2018-03-07T12:23:43Z</updated>

		<summary type="html">&lt;p&gt;Reverted 1 edit by &lt;a href=&quot;/wiki143/index.php?title=Special:Contributions/81.34.226.181&quot; title=&quot;Special:Contributions/81.34.226.181&quot;&gt;81.34.226.181&lt;/a&gt; (&lt;a href=&quot;/wiki143/index.php?title=User_talk:81.34.226.181&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User talk:81.34.226.181 (page does not exist)&quot;&gt;talk&lt;/a&gt;) to last revision by PsyMar. (&lt;a href=&quot;/wiki143/index.php?title=WP:TW&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;WP:TW (page does not exist)&quot;&gt;TW&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Can someone give me the python script which returns&lt;br /&gt;
&lt;br /&gt;
 $ python factorize.py 1800&lt;br /&gt;
 [2^3, 3^2, 5^2]&lt;br /&gt;
&lt;br /&gt;
instead of&lt;br /&gt;
&lt;br /&gt;
 $ python factorize.py 1800&lt;br /&gt;
 [2, 2, 2, 3, 3, 5, 5]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import sys,math&lt;br /&gt;
def factorize(n):&lt;br /&gt;
    def isPrime(n):&lt;br /&gt;
        return not [x for x in xrange(2,int(math.sqrt(n))+1)&lt;br /&gt;
                    if n%x == 0]&lt;br /&gt;
    primes = []&lt;br /&gt;
    candidates = xrange(2,n+1)&lt;br /&gt;
    candidate = 2&lt;br /&gt;
    while not primes and candidate in candidates:&lt;br /&gt;
        if n%candidate == 0 and isPrime(candidate):&lt;br /&gt;
            primes = primes + [candidate] + factorize(n/candidate)&lt;br /&gt;
        candidate += 1            &lt;br /&gt;
    return primes&lt;br /&gt;
&lt;br /&gt;
def condense(L):&lt;br /&gt;
  prime,count,list=0,0,[]&lt;br /&gt;
  for x in L:&lt;br /&gt;
    if x == prime:&lt;br /&gt;
      count = count + 1&lt;br /&gt;
    else:&lt;br /&gt;
      if prime != 0:&lt;br /&gt;
        list = list + [str(prime) + &amp;#039;^&amp;#039; + str(count)]&lt;br /&gt;
      prime,count=x,1&lt;br /&gt;
  list = list + [str(prime) + &amp;#039;^&amp;#039; + str(count)]&lt;br /&gt;
  return list&lt;br /&gt;
&lt;br /&gt;
print factorize(int(sys.argv[1]))&lt;br /&gt;
print condense(factorize(int(sys.argv[1])))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ python factorize.py 1800&lt;br /&gt;
[2, 2, 2, 3, 3, 5, 5]&lt;br /&gt;
[&amp;#039;2^3&amp;#039;, &amp;#039;3^2&amp;#039;, &amp;#039;5^2&amp;#039;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== small bugfix ===&lt;br /&gt;
The code above, as written, would work; however, this is partly lucky.  The isPrime function was inaccurate, as range doesn&amp;#039;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&amp;#039;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). --[[User:PsyMar|PsyMar]] ([[User talk:PsyMar|talk]]) 11:04, 2 March 2016 (UTC)&lt;br /&gt;
: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. --[[User:PsyMar|PsyMar]] ([[User talk:PsyMar|talk]]) 11:06, 2 March 2016 (UTC)&lt;br /&gt;
::This comment is just to show up in the history, as I accidentally marked my &amp;quot;bugfix&amp;quot; comment minor. --[[User:PsyMar|PsyMar]] ([[User talk:PsyMar|talk]]) 11:07, 2 March 2016 (UTC)&lt;br /&gt;
&lt;br /&gt;
== some improvements of the code ==&lt;br /&gt;
&lt;br /&gt;
The current sqrt() algorithm has limitations due to recursion. I&amp;#039;ve written a new one based on [[Square root]] that should run faster and accept larger values. Then I have rewritten the factorize() algorithm. The performance improvements here are marginal, I&amp;#039;ve just rewritten it for learning purposes. The source is available at [http://cracki.incast-security.de/downloads/primes.py]. Feel free to use it as you want.&lt;br /&gt;
&lt;br /&gt;
== C++ Example ==&lt;br /&gt;
&lt;br /&gt;
If were going to have examples shouldn&amp;#039;t they actually work? The C++ example wouldn&amp;#039;t even compile, unless their is a new version of C++ that doesn&amp;#039;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&amp;#039;t check the logs, but I&amp;#039;m thinking this was originally a C example someone haphazardly converted to C++, or someone has a goat for a compiler.)&lt;br /&gt;
&lt;br /&gt;
I&amp;#039;m putting it here until I or someone else can fix it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cmath&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
&lt;br /&gt;
int isPrime(int n)&lt;br /&gt;
{&lt;br /&gt;
  for(int ind=2;ind&amp;lt;=sqrt(n);ind++) {&lt;br /&gt;
    if (n%ind==0) {&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  return false;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  // check argc to make sure we recieved a value.&lt;br /&gt;
&lt;br /&gt;
  int n = atoi(argv[1]);&lt;br /&gt;
&lt;br /&gt;
  for(int i=2;i&amp;lt;=n;i++)&lt;br /&gt;
  {&lt;br /&gt;
    while ( !isPrime(i) &amp;amp;&amp;amp; n%i == 0 )&lt;br /&gt;
    {&lt;br /&gt;
      cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
      n=n/i;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: I agree. This code is not useful. Seems like it could also use some cleanup as to the formatting. [[User:JamesJNHu|JamesJNHu]] ([[User talk:JamesJNHu|talk]]) 16:39, 23 September 2014 (UTC)&lt;br /&gt;
&lt;br /&gt;
== A small contradiction ==&lt;br /&gt;
&lt;br /&gt;
I think there&amp;#039;s a small contradiction between the paragraph &amp;quot;description&amp;quot; and &amp;quot;example&amp;quot;. 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&amp;#039;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&amp;#039;s a good example but like i try to explained (i&amp;#039;m french) it&amp;#039;s a bit in contradiction with the description of the algorithm).&lt;br /&gt;
&lt;br /&gt;
Oh and by the way, the C++ example is a bit funny, i don&amp;#039;t see any object-oriented programmation in this, i wonder why it wasn&amp;#039;t write in C... :)&lt;br /&gt;
&lt;br /&gt;
Etienne &amp;lt;small&amp;gt;—The preceding [[Wikipedia:Sign your posts on talk pages|unsigned]] comment was added by [[Special:Contributions/69.159.129.147|69.159.129.147]] ([[User talk:69.159.129.147|talk]]) 20:31, 5 March 2007 (UTC).&amp;lt;/small&amp;gt;&amp;lt;!-- HagermanBot Auto-Unsigned --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
: It&amp;#039;s sometimes convenient to use C++ just for the standard classes, like vector or iostream. [[User:Cjpuffin|&amp;lt;font style=&amp;quot;background: black&amp;quot; face=&amp;quot;Lucinda Console&amp;quot; color=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;amp;nbsp;CjPuffin&amp;amp;nbsp;&amp;lt;/font&amp;gt;]] 00:15, 6 March 2007 (UTC)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== idea ==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Hello,&lt;br /&gt;
I wonder if it wouldn&amp;#039;t be easy to factorize primes by something like tries or binary search.&lt;br /&gt;
It seems to me the immediate thought on how to solve this, but I don&amp;#039;t realize why it shouldn&amp;#039;t work, so please tell me.&lt;br /&gt;
&lt;br /&gt;
let&amp;#039;s say I have a database of prime numbers and the steps (1-&amp;gt;2 2-&amp;gt;3 3-&amp;gt;5 4-&amp;gt;7...)&lt;br /&gt;
let&amp;#039;s say I have some prime1*prime2 result. (p*q)&lt;br /&gt;
I do result sqrt, and find a place to start searching from.&lt;br /&gt;
&lt;br /&gt;
so I take the target&amp;#039;s sqrt, and go down one step to some n (id of floor is n). I try n*n+1&lt;br /&gt;
and then n*n+2, n*n+3... until result is bigger than my target.&lt;br /&gt;
 if closest multiplication is not the target, &lt;br /&gt;
   we do n-1 * n, n-1 * n+1, n-1*n+2... until result is bigger than target.&lt;br /&gt;
&lt;br /&gt;
(n is the step in db not the prime itself)&lt;br /&gt;
and so on, until n=2.&lt;br /&gt;
if mod is more efficient than these queries, we can check for clean division, since we have the result: mod (result/n)&lt;br /&gt;
 if 0 we check if the answer is in db.&lt;br /&gt;
     if yes, we found our primes.&lt;br /&gt;
     if not: go down one step to prime(n-1).&lt;br /&gt;
 else&lt;br /&gt;
   go down one step to prime(n-1).&lt;br /&gt;
try again result/prime(n-1) -&amp;gt; if in db.. if not.. n-2.&lt;br /&gt;
this needs a direct match, but since we started in the square root there shouldn&amp;#039;t be so many. we can also try binary search (jump a certain number, and minimize search by half each time) so we find the expected range faster, and minimize on the exact point.&lt;br /&gt;
it should be pretty trivial to create a little script and check if it works.&lt;br /&gt;
it seems like a childish solution and since even encryption algorithms rely on prime factorization it&amp;#039;s hard to imagine that it&amp;#039;ll work, but maybe I&amp;#039;ll try just to find out why or if it fails.&lt;br /&gt;
do you know why?&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Why it is so hard to factor&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
The most secure RSA cyphers rely on 2048 bit numbers. That is 2&amp;lt;sup&amp;gt;2048&amp;lt;/sup&amp;gt; = &lt;br /&gt;
323170060713110073007148766886699519604441026697154840321303454275246551&lt;br /&gt;
388678908931972014115229134636887179609218980194941195591504909210950881&lt;br /&gt;
523864482831206308773673009960917501977503896521067960576383840675682767&lt;br /&gt;
922186426197561618380943384761704705816458520363050428875758915410658086&lt;br /&gt;
075523991239303855219143333896683424206849747865645694948561760353263220&lt;br /&gt;
580778056593310261927084603141502585928641771167259436037184618573575983&lt;br /&gt;
511523016459044036976132332872312271256847108202097251571017269313234696&lt;br /&gt;
785425806566979350459972683529986382155251663894373355436021354332296046&lt;br /&gt;
45318478604952148193555853611059596230656. This is about 10&amp;lt;sup&amp;gt;617&amp;lt;/sup&amp;gt;. I used python to calculate the above number. It is seriously that big. Currently all known [[Integer_factorization]] algorithms are slow as dirt when trying to factor the above number. I&amp;#039;m no expert in prime numbers, but even if there was only one prime in a million (1000000), you can clearly see how many primes there would be. It is not practically possible to calculate all the prime numbers, nor do we have the disk space to store them for fast access. BTW, if someone breaks the above number, the key just has to be made longer. A 4096 bit key would be 2&amp;lt;sup&amp;gt;2048&amp;lt;/sup&amp;gt;*2&amp;lt;sup&amp;gt;2048&amp;lt;/sup&amp;gt;. The only thing conceivable to break RSA and make it obsolete is to come up with a fast factoring algorithm or make a quantum computer.&lt;br /&gt;
&lt;br /&gt;
== A better algorithm ==&lt;br /&gt;
I&amp;#039;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&amp;#039;t think this algorithm is any slower then the big one posted on the main page. It could even be faster...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Python code to factor a number&lt;br /&gt;
#This code can factor any number of any length... eventually.&lt;br /&gt;
#This is because python has built in arbitrarily length numbers.&lt;br /&gt;
def factor(x):&lt;br /&gt;
    factors = []&lt;br /&gt;
    i = 2&lt;br /&gt;
    while x &amp;gt; 1:&lt;br /&gt;
        if x % i == 0:&lt;br /&gt;
            x = x / i&lt;br /&gt;
            factors.append(i)&lt;br /&gt;
        else:&lt;br /&gt;
            i += 1&lt;br /&gt;
    return factors&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
(I believe the following theory is correct. Let me know if I&amp;#039;m mistaken because I just came up with this to simplify that massive algorithm.)&lt;br /&gt;
&lt;br /&gt;
Proof:&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A Bug&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
one more comment on the above algorithm, when try 1234567890987654321&lt;br /&gt;
it output: [3, 4, 4, 4, 17, 29, 431, 463, 2557, 25561], where 4 is not prime.  &amp;lt;span style=&amp;quot;font-size: smaller;&amp;quot; class=&amp;quot;autosigned&amp;quot;&amp;gt;— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/128.120.52.135|128.120.52.135]] ([[User talk:128.120.52.135|talk]]) 21:10, 27 August 2013 (UTC)&amp;lt;/span&amp;gt;&amp;lt;!-- Template:Unsigned IP --&amp;gt; &amp;lt;!--Autosigned by SineBot--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Solution:&lt;br /&gt;
For Python 3 use x = x // i instead of x = x / i.  &amp;lt;span style=&amp;quot;font-size: smaller;&amp;quot; class=&amp;quot;autosigned&amp;quot;&amp;gt;— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/95.105.176.163|95.105.176.163]] ([[User talk:95.105.176.163|talk]]) 21:00, 11 December 2013 (UTC)&amp;lt;/span&amp;gt;&amp;lt;!-- Template:Unsigned IP --&amp;gt; &amp;lt;!--Autosigned by SineBot--&amp;gt;&lt;br /&gt;
:This is the [[trial division]] algorithm. The version you describe, on a b-bit number, may take as many as 2&amp;lt;sup&amp;gt;b&amp;lt;/sup&amp;gt; iterations (for instance, it will do so whenever its input is prime). It may be sped up to roughly 2&amp;lt;sup&amp;gt;b/2&amp;lt;/sup&amp;gt; by stopping and giving up when &amp;#039;&amp;#039;i&amp;#039;&amp;#039; is around the square root of the remaining value &amp;#039;&amp;#039;x&amp;#039;&amp;#039;, 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. —[[User:David Eppstein|David Eppstein]] ([[User talk:David Eppstein|talk]]) 21:06, 11 December 2013 (UTC)&lt;/div&gt;</summary>
		<author><name>imported&gt;Xonqnopp</name></author>
	</entry>
</feed>