Skip to content

Commit

Permalink
Merge pull request #269 from tsg-ut/factorization-stage
Browse files Browse the repository at this point in the history
Factorization stage
  • Loading branch information
moratorium08 authored May 7, 2017
2 parents 90b2a26 + b382c90 commit 6fe21d1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
97 changes: 97 additions & 0 deletions stages/factorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module.exports = {
name: 'factorization',
version: 1,
parts: {
wireI: null,
wireL: null,
wireT: null,
wireX: null,
wireXdot: null,
'times-2': null,
'times-3': null,
'times-10': null,
'plus-1': null,
'minus-1': null,
'const-0': null,
'const-1': null,
'const-2': null,
add: null,
sub: null,
div: null,
mul: null,
mod: null,
pow: null,
log: null,
sqrt: null,
equal: null,
neq: null,
gt: null,
geqq: null,
lt: null,
leqq: null,
conditional: null,
transistor: null,
diode: null,
},
inputX: 5,
outputX: 5,
input: [null, null, null, 211, 221],
output: [null, null, null, 0, 13],
ioGenerator: (random) => {
const numbers = [0, 0, 0];
const primes = [2];
for (let i = 3; i < 256; i++) {
let flag = null;
for (const prime of primes) {
if (i % prime === 0) {
flag = prime;
break;
}
}
if (flag === null) {
numbers.push(0);
primes.push(i);
} else {
numbers.push(flag);
}
}

const candidates = Array.from({length: 199}, (item, index) => index + 2); // 2..200
const inputs = [];

const index1 = Math.floor(random() * 30);
inputs.push(candidates[index1]);
candidates[index1] = candidates[0];

const index2 = Math.floor(random() * 119) + 1;
inputs.push(candidates[index2]);
candidates[index2] = candidates[1];

const index3 = Math.floor(random() * 118) + 2;
inputs.push(candidates[index3]);

inputs.sort((a, b) => a - b);

return {
input: [
inputs[0],
inputs[1],
inputs[2],
211,
221,
],
output: [
numbers[inputs[0]],
numbers[inputs[1]],
numbers[inputs[2]],
0,
13,
],
};
},
width: 11,
height: 11,
clockLimit: 150,
statement: '与えられた数が素数なら0を、素数でないなら最も小さい1でない約数を探してみよう!',
title: '因数分解',
};
1 change: 1 addition & 0 deletions stages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ module.exports = [
require('./2017.js'),
require('./repeat-self.js'),
require('./fibonacci-hard.js'),
require('./factorization.js'),
];
19 changes: 19 additions & 0 deletions test/unit/stages.ls
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require! {
'../../stages/mod3-hard'
'../../stages/repeat-self'
'../../stages/fibonacci-hard'
'../../stages/factorization'
}

chai.use chai-things
Expand Down Expand Up @@ -66,6 +67,12 @@ conditional2-calc = (x) ->
18
else x

factorization-calc = (n) ->
for i from 2 to n - 1
if n % i is 0
return i
0

describe 'Stage Data' ->
before-each ->
@random = seedrandom ''
Expand Down Expand Up @@ -275,3 +282,15 @@ describe 'Stage Data' ->
output is fibonacci-calc input

expect io.input.3 .to.equal 30

describe 'factorization stage' ->
It 'generates factorization' ->
io = factorization.io-generator @random

expect io .to.satisfy io-spec

expect zip io.input, io.output .to.all.satisfy ([input, output]) ->
output is factorization-calc input

expect io.input.3 .to.equal 211
expect io.input.4 .to.equal 221

0 comments on commit 6fe21d1

Please sign in to comment.