Should approach fastPowering this way?
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 42/100
- Type d'issue
- Refactorisation
- Clarté
- Plutôt claire
- Activité
- À l'abandon
- Stack technique
- javascript
- Domaine
- tooling
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- JavaScript
- Étoiles
- 197k
- Forks
- 31k
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de trekhleb/javascript-algorithms
-
BinarySearchTreeNode removing Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
trekhleb/javascript-algorithms#2102 · 1 commentaire ·
-
Difficulté 4/5 3-5 jours Accessibilité débutants 25/100
trekhleb/javascript-algorithms#2085 · 6 commentaires · 1 réaction ·
-
LinkedList methods. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
trekhleb/javascript-algorithms#2065 · 1 commentaire ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 55/100
trekhleb/javascript-algorithms#2057 · 1 commentaire ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
trekhleb/javascript-algorithms#2056 ·
Toutes les issues de trekhleb/javascript-algorithms
Issues similaires
-
code-quality refactoring
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
githubnext/gh-aw-cao#13143 ·
-
status: needs triage
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100