Need help on tiresome GCD

Every time i click submit the numbers keep changing

1 Like

Literally READ what the comments say, and you’ll beat the level. You are NOT supposed to calculate it yourself. Your code is supposed to calculate it for you.

1 Like

Thanks for the advice


You are suppose to use variables because the number is unknown to which they will give you.

1 Like

ok i’ll try that method


help! i cant solve it!

don’t get anything
about anything

Can you show us your code formated as it is described below so we will be able to help you solve it?


# Calculate the secret number and get into the Treasury.
# Those two guys know keys for the password.
friends = hero.findFriends()
number1 = friends[0].secretNumber
number2 = friends[1].secretNumber
# Just to be sure that the first number is greater.
if number2 > number1:
    swap = number1
    number1 = number2
    number2 = swap

# It's simple but slow function to find gcd.
def bruteforceGCD (a, b):
    hero.say("The naive algorithm.")
    cycles = 0
    # We enumerate all possible divisors.
    counter = b
    while True:
        cycles += 1
        if cycles > 100:
            hero.say("Calculating is hard. I'm tired.")
        # If both number have "counter" divisor.
        if a % counter == 0 and b % counter == 0:
        counter -= 1
    hero.say("I used " + cycles + " cycles")
    return counter

# It's smart way to find gcd.
def euclidianGCD (a, b):
    cycles = 0
    while b:
        cycles += 1
        swap = b
        b = a % b
        a = swap
    hero.say("I used " + cycles + " cycles")
    return a

# Maybe you need to use another function?
secretNumber = bruteforceGCD(friends[0].secretNumber, friends[1].secretNumber)
hero.moveXY(48, 34)
# The treasury is open (I hope so)! Go there!
hero.moveXY(68, 34)

Just change

secretNumber = euclidianGCD(friends[0].secretNumber, friends[1].secretNumber)