Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem Regarding Queue

0 views
Skip to first unread message

mukesh tiwari

unread,
Feb 9, 2010, 5:06:56 PM2/9/10
to
Could some one please tell what is wrong with this code. I am trying
to use Queue in this program but i am getting error
Traceback (most recent call last):
File "/home/user/NetBeansProjects/NewPythonProject2/src/
Pollard_rho.py", line 80, in <module>
factor(n)
File "/home/user/NetBeansProjects/NewPythonProject2/src/
Pollard_rho.py", line 59, in factor
Q_1=Queue()
NameError: global name 'Queue' is not defined.
As i am new to python so kindly pardon me if i sound stupid.
Here is the code
# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="Mukesh Tiwari"
__date__ ="$Feb 10, 2010 1:35:26 AM$"

import random
import sys
def gcd(a,b):
while b:
a,b=b,a%b
return a

def rabin_miller(p):
if(p<2):
return False
if(p!=2 and p%2==0):
return False
s=p-1
while(s%2==0):
s>>=1
for i in xrange(10):
a=random.randrange(p-1)+1
temp=s
mod=pow(a,temp,p)
while(temp!=p-1 and mod!=1 and mod!=p-1):
mod=(mod*mod)%p
temp=temp*2
if(mod!=p-1 and temp%2==0):
return False
return True

def pollard(n):
if(n%2==0):
return 2;
x=random.randrange(2,1000000)
c=random.randrange(2,1000000)
y=x
d=1
while(d==1):
x=(x*x+c)%n
y=(y*y+c)%n
y=(y*y+c)%n
d=gcd(x-y,n)
if(d==n):
break;
return d;
def factor(n):
#if(rabin_miller(n)):
# print n
# return
#d=pollard(n)
#if(d!=n):
# factor(d)
# factor(n/d)
#else:
# factor(n)

Q_1=Queue()
Q_2=Queue()
Q_1.put(n)
while(not Q_1.empty()):
l=Q_1.get()
if(rabin_miller(l)):
Q_2.put(l)
continue
d=pollard(l)
if(d==l):Q_1.put(l)
else:
Q_1.put(d)
Q_1.put(l/d)
while(not Q_2.empty()):
print Q_2.get()

if __name__ == "__main__":
while(True):
n=input();
factor(n)

Rob Williscroft

unread,
Feb 9, 2010, 7:16:24 PM2/9/10
to
mukesh tiwari wrote in news:80fed7d5-76eb-40c8-ace1-0c35736de399
@t17g2000prg.googlegroups.com in comp.lang.python:

> Could some one please tell what is wrong with this code. I am trying
> to use Queue in this program but i am getting error

The type you appear to be trying to use is Queue.Queue which you import
with:

from Queue import Queue

http://docs.python.org/library/queue.html?highlight=queue#Queue.Queue

> Q_1=Queue()
> Q_2=Queue()
> Q_1.put(n)
> while(not Q_1.empty()):
> l=Q_1.get()
> if(rabin_miller(l)):
> Q_2.put(l)
> continue
> d=pollard(l)
> if(d==l):Q_1.put(l)
> else:

As the help page above points out also check out the deque Class:

http://docs.python.org/library/collections.html#collections.deque

Rob.

0 new messages