salty_coffee
salty_coffee

Reputation: 631

find random number using divide and conquer

This is my first post to the community and I am still very new to coding. I was hoping to get some help on finding x in the following code using divide and conquer, it doesn't seem to work for me.

import random
x= random.randint(1,1000)

##set range
high = (1000)
low = (1)

while x != high:
    mid = round(high//2, 0)
    if x == mid:
        print mid
        print ("print you have found x")
        break
    elif x > mid:
        low = mid
        print low
    elif x < mid:
        high = mid
        print high
    else:
        break

Upvotes: 0

Views: 78

Answers (1)

Kevin
Kevin

Reputation: 76234

mid = round(high//2, 0)

This doesn't look right to me. Shouldn't mid be the average of high and low?

mid = (high+low)//2

Upvotes: 1

Related Questions