[SOLVED] Help with Sarven Desert - Tiresome GCD


#1

Help. I don’t know what to do. It says that what I have to do is go to the treasury if it is open, but everytime I press run the hunters attacked me and said it is incorrect. I don’t know how to fix the code since it only tells me to do one thing when there is something else that needs fixing. Please help. Here is my code:
// Calculate the secret number and get into the Treasury.
// Those two guys know keys for the password.
var friends = hero.findFriends();
var number1 = friends[0].secretNumber;
var number2 = friends[1].secretNumber;
// Just to be sure that the first number is greater.
if (number2 > number1) {
var swap = number1;
number1 = number2;
number2 = swap;
}

// It’s simple but slow function to find gcd.
function bruteforceGCD (a, b) {
hero.say(“The naive algorithm.”);
var cycles = 0;
// We enumerate all possible divisors.
var counter = b;
while (true) {
cycles++;
if (cycles > 100) {
hero.say(“Calculating is hard. I’m tired.”);
break;
}
// If both number have “counter” divisor.
if (a % counter === 0 && b % counter === 0) {
break;
}
counter–;
}
hero.say(“I used " + cycles + " cycles”);
return counter;
}

// It’s smart way to find gcd.
function euclidianGCD (a, b) {
var cycles = 0;
while (b) {
cycles++;
var swap = b;
b = a % b;
a = swap;
}
hero.say(“I used " + cycles + " cycles”);
return a;
}

// Δ Maybe you need to use another function?
var secretNumber = bruteforceGCD(number1, number2);
hero.moveXY(48, 34);
hero.say(secretNumber);
// The treasury is open (I hope so)! Go there!
hero.moveXY(68, 34);

Thank You for your Attention.


#2

I found the error. Thank you. It turns out you need to switch bruteforceGCD with euclidianGCD. I think that GCD that could be use for comparison and figuring out correct algorithms


#3

Check the FAQ and learn how to format beautifully so we can help if you have actual errors in the future.