2017年4月20日木曜日

開発環境

The Art of Computer Programming Volume 1 Fundamental Algorithms Third Edition 日本語版(Donald E. Knuth (著)、青木 孝 (著)、筧 一彦 (著)、鈴木 健一 (著)、長尾 高弘 (著)、有澤 誠 (その他)、和田 英一 (その他)、ドワンゴ)の第1章(基礎概念)、1.2(数学的な基礎)、演習問題10を取り組んでみる。


  1. n=10 2 10 =1024 10 3 =1000 2 10 > 10 3 2 n+1 =2· 2 n >2 n 3 ( n+1 ) 3 = n 3 +3 n 2 +3n+1 2· 2 n > ( 1+ 1 n ) 3 · 2 n > ( 1+ 1 n ) 3 · n 3 =( 1+ 3 n 2 + 3 n + 1 n 3 ) n 3 = n 3 +3n+3 n 2 +1 = ( n+1 ) 3 2 n+1 > ( n+1 ) 3

コード(Emacs)

HTML5

<div id="graph0"></div>
n = <input id="n0" type="number" min="11" step="1" value="20">
<br>
<button id="draw0">draw</button>

<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="sample10.js"></script>

JavaScript

let div0 = document.querySelector('#graph0'),
    input0 = document.querySelector('#n0'),
    width = 600,
    height = 600,
    padding = 50,
    btn0 = document.querySelector('#draw0'),
    f = (n) => Math.pow(2, n),
    g = (n) => Math.pow(n, 3);

let draw = () => {
    let points = [],
        n = parseInt(input0.value, 10);
    
    for (let i = 10; i < n + 1; i += 1) {
        points.push([i, f(i)]);
    }
    for (let i = 10; i < n + 1; i += 1) {
        points.push([i, g(i)]);
    }
    let min = Math.min(...points.map((x) => x[1])),
        max = Math.max(...points.map((x) => x[1]));
    let xscale = d3.scaleLinear()
        .domain([10, n])
        .range([padding, width - padding]);
    let yscale = d3.scaleLinear()
        .domain([min, max])
        .range([height - padding, padding]);

    let xaxis = d3.axisBottom().scale(xscale);
    let yaxis = d3.axisLeft().scale(yscale);
    div0.innerHTML = '';
    let svg = d3.select('#graph0')
        .append('svg')
        .attr('width', width)
        .attr('height', height);

    let t = points.length / 2;
    svg.selectAll('circle')
        .data(points)
        .enter()
        .append('circle')
        .attr('cx', (d) => xscale(d[0]))
        .attr('cy', (d) => yscale(d[1]))
        .attr('r', 2)
        .attr('fill', (d, i) => i < t ? 'red' : 'blue');

    svg.append('g')
        .attr('transform', `translate(0, ${height - padding})`)
        .call(xaxis);

    svg.append('g')
        .attr('transform', `translate(${padding}, 0)`)
        .call(yaxis);
}

input0.onchange = draw;
btn0.onclick = draw;

draw();
n =

0 コメント:

コメントを投稿

Comments on Google+: