Should approach fastPowering this way?
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 42/100
- Issue 类型
- 重构
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- javascript
- 领域
- tooling
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- JavaScript
- 星标
- 197k
- 派生
- 31k
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
trekhleb/javascript-algorithms 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
trekhleb/javascript-algorithms#2102 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
trekhleb/javascript-algorithms#2085 · 6 条评论 · 1 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 35/100
trekhleb/javascript-algorithms#2065 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 55/100
trekhleb/javascript-algorithms#2057 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
trekhleb/javascript-algorithms#2056 ·
查看 trekhleb/javascript-algorithms 的全部 Issue
相似的 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 70/100
babalae/bettergi-scripts-list#3674 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
vadimdemedes/ink#1029 ·
-
code-quality refactoring
难度 2/5 1-3 小时 新手友好度 84/100
github/gh-aw-firewall#8816 ·
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
难度 2/5 1-3 小时 新手友好度 74/100
langchain-ai/deepagents#6450 ·
-
optimization optimization:agents-md-curator
难度 2/5 1-3 小时 新手友好度 86/100
githubnext/gh-aw-cao#13143 ·