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

4.3 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
5a7dad05be01840e1778a0d1 Fractran 3 fractran

--description--

FRACTRAN是由数学家John Horton Conway发明的图灵完备的深奥编程语言。


FRACTRAN程序是正分数$ P =f_1f_2\ ldotsf_m$的有序列表,以及初始正整数输入$ n $。


该程序通过更新整数$ n $来运行,如下所示:


  • 对于第一个分数$ f_i $,在$ nf_i $为整数的列表中,用$ nf_i $替换$ n $;
  • 重复此规则,直到列表中没有分数乘以$ n $时产生整数,然后停止。

康威为FRACTRAN提供了素数计划


$ 17/91 $$ 78/85 $$ 19/51 $$ 23/38 $$ 29/33 $$ 77/29 $$ 95/23 $$ 77/19 $$ 1/17 $$ 11/13 $ $ 13/11 $ $$ 15/14 $$ 15/2 $$ 55/1 $


从$ n = 2 $开始此FRACTRAN程序将$ n $更改为$ 15 = 2 \ times15/2$,然后$ 825 = 15 \ times55/1$,生成以下整数序列:


$ 2 $$ 15 $$ 825 $$ 725 $$ 1925 $$ 2275 $$ 425 $$ 390 $$ 330 $$ 290 $$ 770 $$ \ ldots $


2之后此序列包含以下2的幂


$ 2 ^ 2 = 4 $$ 2 ^ 3 = 8 $$ 2 ^ 5 = 32 $$ 2 ^ 7 = 128 $$ 2 ^ {11} = 2048 $$ 2 ^ {13} = 8192 $$ 2 ^ {17 } = 131072 $$ 2 ^ {19} = 524288 $$ \ ldots $


这是2的主要权力。


任务:

编写一个函数将fractran程序作为字符串参数并将程序的前10个数字作为数组返回。如果结果没有10个数字则按原样返回数字。

--hints--

fractran应该是一个功能。

assert(typeof fractran == 'function');

fractran(""+tests[0]+"")应该返回一个数组。

assert(Array.isArray(fractran('3/2, 1/3')));

fractran(""+tests[0]+"")应返回"+JSON.stringify(results[0])+"

assert.deepEqual(fractran('3/2, 1/3'), [2, 3, 1]);

fractran(""+tests[1]+"")应返回"+JSON.stringify(results[1])+"

assert.deepEqual(fractran('3/2, 5/3, 1/5'), [2, 3, 5, 1]);

fractran(""+tests[2]+"")应返回"+JSON.stringify(results[2])+"

assert.deepEqual(fractran('3/2, 6/3'), [2, 3, 6, 9, 18, 27, 54, 81, 162, 243]);

fractran(""+tests[3]+"")应返回"+JSON.stringify(results[3])+"

assert.deepEqual(fractran('2/7, 7/2'), [2, 7, 2, 7, 2, 7, 2, 7, 2, 7]);

fractran(""+tests[4]+"")应返回"+JSON.stringify(results[4])+"

assert.deepEqual(
  fractran(
    '17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23, 77/19, 1/17, 11/13, 13/11, 15/14, 15/2, 55/1'
  ),
  [2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290]
);

--seed--

--seed-contents--

function fractran(progStr) {

}

--solutions--

function fractran(progStr){
  var num = new Array();
  var den = new Array();
  var val ;
  var out="";
  function compile(prog){
    var regex = /\s*(\d*)\s*\/\s*(\d*)\s*(.*)/m;
    while(regex.test(prog)){
      num.push(regex.exec(prog)[1]);
      den.push(regex.exec(prog)[2]);
      prog = regex.exec(prog)[3];
    }
  }

  function step(val){
    var i=0;
    while(i<den.length && val%den[i] != 0) i++;
    return num[i]*val/den[i];
  }

  var seq=[]

  function exec(val){
    var i = 0;
    while(val && i<limit){
      seq.push(val)
      val = step(val);
      i ++;
    }
  }

  // Main
  compile(progStr);
  var limit = 10;
  exec(2);
  return seq;
}