Should approach fastPowering this way?
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 42/100
- Tipo di issue
- Refactoring
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- javascript
- Ambito
- tooling
Direzione di ricerca
Read fastPowering.js and compare its odd and even branches with the proposed shared multiplier calculation. Verify that the refactor preserves results for zero, even, and odd powers; done means the approach is accepted and the issue is closed.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
While learning about Fast Powering Algorithm, I notice that a minor tweak can be made to the fastPowering.js file.
In both of the cases whether the power is odd or event, calculated multiplier will be the same. So, we can calculate multiplier before the if clause and return the multiplication based on the nature of power.
export default function fastPowering(base, power) {
if (power === 0) {
// Anything that is raised to the power of zero is 1.
return 1;
}
// multiplier will be same whether power is even or odd
const multiplier = fastPowering(base, Math.floor(power / 2));
if (power % 2 === 0) {
// If the power is even...
// we may recursively redefine the result via twice smaller powers:
// x^8 = x^4 * x^4.
return multiplier * multiplier;
}
// If the power is odd...
// we may recursively redefine the result via twice smaller powers:
// x^9 = x^4 * x^4 * x.
return multiplier * multiplier * base;
}
@trekhleb Let me know your thoughts. And if you're okay with this implementation I can raise a PR.
- Lingua principale
- JavaScript
- Stelle
- 197k
- Fork
- 31k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di trekhleb/javascript-algorithms
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
trekhleb/javascript-algorithms#2102 · 1 commento ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
trekhleb/javascript-algorithms#2085 · 6 commenti · 1 reazione ·
-
LinkedList methods. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
trekhleb/javascript-algorithms#2065 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
trekhleb/javascript-algorithms#2057 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
trekhleb/javascript-algorithms#2056 ·
Tutte le issue di trekhleb/javascript-algorithms
Issue simili
-
code-quality refactoring
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
githubnext/gh-aw-cao#13143 ·
-
status: needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100