Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.5 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
599d1566a02b571412643b84 埃塞俄比亚的乘法 5 ethiopian-multiplication

--description--

埃塞俄比亚乘法是一种仅使用加法,加倍和减半来乘以整数的方法。

方法:

取两个数字相乘然后将它们写在两列的顶部。在左侧列中反复将最后一个数字减半丢弃任何余数并将结果写入同一列中的最后一个直到您写入值1.在右侧列中重复加倍最后一个数字并写入结果如下。在左侧列显示的同一行中添加结果时停止1.检查生成的表并丢弃左列中的值为偶数的任何行。将右侧列中的值相加,以产生将原始两个数相乘的结果

例如17×34

17 34

将第一列减半:

17 34

8

4

2

1

加倍第二列:

17 34

8 68

4 136

2 272

1 544

第一个单元格为偶数的删除行:

17 34

8 68

4 136

2 272

1 544

将右侧列中的剩余数字相加:

17 34

8 -

4 ---

2 ---

1 544

====

578

所以17乘以34埃塞俄比亚方法是578。

任务:

任务是定义三个命名函数/方法/过程/子例程:

一个将一个整数减半,一个减半整数,一个整数是偶数。

使用这些函数创建一个执行埃塞俄比亚乘法的函数。

--hints--

eth_mult是一个功能。

assert(typeof eth_mult === 'function');

eth_mult(17,34)应该返回578

assert.equal(eth_mult(17, 34), 578);

eth_mult(23,46)应该返回1058

assert.equal(eth_mult(23, 46), 1058);

eth_mult(12,27)应该返回324

assert.equal(eth_mult(12, 27), 324);

eth_mult(56,98)应该返回5488

assert.equal(eth_mult(56, 98), 5488);

eth_mult(63,74)应该返回4662

assert.equal(eth_mult(63, 74), 4662);

--seed--

--seed-contents--

function eth_mult(a, b) {

}

--solutions--

function eth_mult(a, b) {
  let sum = 0; a = [a]; b = [b];

  let half = a => a / 2,
    double = a => a * 2,
    is_even = a => a % 2 == 0;

  while (a[0] !== 1) {
    a.unshift(Math.floor(half(a[0])));
    b.unshift(double(b[0]));
  }

  for (let i = a.length - 1; i > 0; i -= 1) {
    if (!is_even(a[i])) {
      sum += b[i];
    }
  }
  return sum + b[0];
}