English 中文(简体)
Python Python 边环, 操作器和( &) 操作器不起作用
原标题:Python While Loop, the and (&) operator is not working

我试图找到最大的共同因素。

我写了一个错误的(操作密集)算法,它降低一个低值,用%来检查它是否均衡地将分子和分母分隔开来,如果它这样的话,它就会退出程序。然而,我当时的循环没有使用该和操作员,因此,一旦该分子可以分辨,它就停止了,即使它不是正确的答案。

我使用的数字是54和42,正确的GCD(最大公分母)是6。

#heres a simple algorithm to find the greatest common denominator: 

iterations = 0; #used to calculate number of times while loop is executed

u = 54; v= 42; d = v-1; #u is the numerator, v is the denominator, d is the number decremented by one 

while ((v % d !=0) & (u % d != 0)): #while both numerator AND denominator cannot be evenly divided by the decremented number
 d -= 1 #decrement the number by one
 print d #print the number decremented
 iterations +=1 #add 1 to the count of iterations in while loop

print "the gcd is " +str(d) #should be 6 when the number can evenly divide both
print "the number of iterations was " +str(iterations) #display times it took algorithm to complete

我得到的答案是27,它告诉我,一旦它达到27岁,并且可以平均地将54/27分开,它就停止了。

谢谢!

最佳回答

您应该使用关键词 , 而不是位元和操作员 :

while (v % d != 0) and (u % d != 0): 

这一点也是一样的:

while (v % d) and (u % d): 

注意 将给第一个情况带来相同的结果,第二个情况则不会。

您的问题在于您想要使用 而不是 。 您的算法效率也非常低。 http:// en. wikipedia. org/ wiki/ Greaest_ common_ divisor# Calculate" rel= “ noreferrer” > > 计算 GCD 的更好方法 。

问题回答

使用 关键词 。 是一个位元和运算符 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签