An warware: javascript gcd

Babban matsala tare da JavaScript GCD algorithm shine cewa yana iya ษ—aukar lokaci mai tsawo don ฦ™ididdigewa.

function gcd(a, b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

Wannan aiki ne na maimaitawa don ฦ™ididdige mafi girman mai rarraba lambobi guda biyu, ta amfani da algorithm Euclid.

Idan b daidai yake da 0, to GCD yayi daidai da a. In ba haka ba, GCD daidai yake da GCD na b da ragowar raba ta b.

Mafi Girma Common Dividor

Babban Mai Rarraba Gabaษ—aya (GCD) na lamba biyu shine mafi girman lamba wanda ke rarraba duka lambobin ba tare da barin ragowar ba. Misali, GCD na 12 da 24 shine 6.

Dakunan karatu na lissafi

Akwai ฦดan ษ—akunan karatu waษ—anda zasu iya taimakawa da lissafi a JavaScript. ฦŠaya shine Math.js, wanda ke ba da dama na asali ayyuka da abubuwa. Wani kuma numeral.js, wanda ke ba da cikakkiyar tsarin ayyuka da abubuwa.

Maimaitawa a cikin JavaScript

Recursion gini ne na shirye-shirye wanda ke ba da damar aiki don kiran kansa. A wasu kalmomi, yana ba da damar aiki don komawa kansa a cikin ma'anarsa. Ana iya amfani da maimaitawa don magance matsaloli ko cimma wasu manufofi.

ฦŠayan amfani na yau da kullun na maimaitawa shine a cikin algorithms waษ—anda ke magance matsalolin ta amfani da madaukai. Misali, ana iya magance jerin Fibonacci ta amfani da algorithm mai maimaitawa. Algorithm yana farawa ta hanyar ฦ™ididdige lambar Fibonacci a karon farko, sannan ฦ™ididdige lambar Fibonacci a karo na biyu bisa sakamakon lissafin farko. Ana maimaita wannan tsari har sai ko dai jerin sun kai ฦ™ayyadaddun iyaka ko har sai an sami kuskure.

Hakanan za'a iya amfani da ayyukan maimaitawa don magance matsalolin da suka haษ—a da jeri da jeri. Misali, a ce kuna son nemo duk madaidaitan lambobi tsakanin 2 zuwa 100. Kuna iya amfani da madauki don yin wannan, amma zai ษ—auki ษ—an lokaci kaษ—an don gudu. Madadin haka, zaku iya amfani da maimaitawa don ฦ™ididdige duk madaidaitan lambobi tsakanin 2 zuwa 100 ta amfani da kiran aiki guda ษ—aya.

Shafi posts:

1 tunani akan "An warware: javascript gcd"

Leave a Comment