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.
Na gode da lambar lissafin java, zan yi amfani da shi a cikin gidan yanar gizona da sauri.
Barkanku da ranakuโฆ