2016年10月10日月曜日

開発環境

素数夜曲 (吉田 武 (著)、東海大学出版会)の付録D(女王陛下のLISP)、D.5(コラッツの問題)、初期値の集団を JavaScript で取り組んでみる。

コード(Emacs)

HTML5

<div id="graph0"></div>
<label for="n0">n = </label>
<input id="n0" type="number" min="1" step="1" value="27">
<span id="output0"></span>
<br>
<label for="n1">y = </label>
<input id="n1" type="number" min="1" step="1" value="9232"> 
<span id="output1"></span>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.6/d3.min.js" integrity="sha256-5idA201uSwHAROtCops7codXJ0vja+6wbBrZdQ6ETQc=" crossorigin="anonymous"></script>
<script src="coconatz2.js"></script>

JavaScript

{
    'use strict';
    let dataset,
        w = 600,
        h = 600,
        padding = 50,
        div_graph = document.querySelector('#graph0'),
        div_graph1 = document.querySelector('#graph1'),
        span_output = document.querySelector('#output0'),
        span_output1 = document.querySelector('#output1'),
        input_n = document.querySelector('#n0'),
        input_n1 = document.querySelector('#n1');

    let cocolatz = (n) => {
        return n === 1 ? [1] :
            [n].concat(
                n % 2 === 0 ? cocolatz(n / 2) : cocolatz(3 * n + 1)
            );
    };
    let iota = (start, end) => {
        let a = [];

        for (let i = start; i <= end; i += 1) {
            a.push(i);
        }
        return a;
    };
    let peak = (n) => {
        return iota(1, n).map((i) => Math.max(...cocolatz(i)));
    };
    let draw = () => {
        let n = parseInt(input_n.value, 10);
        
        dataset = peak(n);

        let xmax = dataset.length,
            ymax = Math.max(...dataset);

        div_graph.innerHTML = '';
        let svg = d3.select('#graph0')
            .append('svg')
            .attr('width', w)
            .attr('height', h);

        let xscale = d3.scaleLinear()
            .domain([0, xmax])
            .range([padding, w]);
        let yscale = d3.scaleLinear()
            .domain([0, ymax])
            .range([h - padding, padding]);
        let xaxis = d3.axisBottom().scale(xscale);
        let yaxis = d3.axisLeft().scale(yscale);

        let rect_w = xscale(2) - xscale(1);
        svg.selectAll('rect')
            .data(dataset)
            .enter()
            .append('rect')
            .attr('x', (d, i) => xscale(i))
            .attr('y', (d) => yscale(d))
            .attr('width', rect_w)
            .attr('height', (d) => yscale(0) - yscale(d))
            .attr('fill', 'rgba(0, 0, 0, 0)')
            .attr('stroke', 'green');
        
        svg.append('g')
            .attr('transform', `translate(0, ${h - padding})`)
            .call(xaxis);
        svg.append('g')
            .attr('transform', `translate(${padding}, 0)`)
            .call(yaxis);

        let m = dataset.filter((x) => x === ymax).length;
        span_output.innerText = `初期値の最大値: ${ymax}, 最大値の個数: ${m}`;
    };
    let just = () => {
        let n = parseInt(input_n1.value, 10),
            len = dataset.filter((d) => d === n).length;

        span_output1.innerText = `個数: ${len}`;
    };

    input_n.onchange = () => {
        draw();
        just();
    };
    input_n1.onchange = just;
    
    draw();
    just();
}

0 コメント:

コメントを投稿

Comments on Google+: