Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python While Loop, the and (&) operator is not working

I am trying to find the greatest common factor.

I wrote a bad (operation intensive) algorithm that decrements the lower value by one, checks using % to see if it evenly divides both the numerator and denominator, if it does then it exits the program. However, my while loop is not using the and operator, and thus once the numerator is divisible it stops, even though its not the correct answer.

The numbers I am using are 54 and 42, the correct GCD (greatest common denominator) is 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

The answer I am getting is 27, which tells me once it reaches 27 and can divide 54/27 evenly, it stops. Any thoughts on how to use an and operator in a while loop in python?

Thanks!

like image 347
Blakedallen Avatar asked May 26 '12 05:05

Blakedallen


People also ask

Can while loop have 2 conditions?

As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.

Can we use two conditions in while loop in Python?

Python While Loop Multiple Conditions. To combine two conditional expressions into one while loop, you'll need to use logical operators. This tells Python how you want all of your conditional expressions to be evaluated as a whole.

How do you combine two while loops in Python?

The only way you'll break out of while True: is by calling break . Use a single while loop and split it with if conditionals. that's mean i need to combine into one while loop by putting the break in between the two separate commands. is it true? i got it! the break really helps me.

How do you give multiple conditions in a statement in Python?

If we want to join two or more conditions in the same if statement, we need a logical operator. There are three possible logical operators in Python: and – Returns True if both statements are true. or – Returns True if at least one of the statements is true.


2 Answers

You should be using the keyword and instead of the bitwise and operator &:

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

This is also the same:

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

Note that & and and will give the same result in the first case, but not in the second.

Your problem though is that you want to use or instead of and. Also your algorithm is highly inefficient. There are better ways to calculate the GCD.

like image 128
Mark Byers Avatar answered Nov 02 '22 10:11

Mark Byers


Use the and keyword. & is a bitwise and operator.

like image 36
varunl Avatar answered Nov 02 '22 10:11

varunl