乌拉姆螺旋(Ulam Spiral)的实验图片及一段HTML代码

4 篇文章 0 订阅
2 篇文章 0 订阅

因为前一段时间看了一些近代,也了解了一些数论的皮毛,发现其中有个娱乐性的乌拉姆螺旋看起来颇有观赏性,于是自己做了一些实验,这里记录一下,并且给出了简单实现的源代码。

什么是乌拉姆螺旋(Ulam Spiral)

质数螺旋(国外叫作 Ulam spiral — “乌拉姆螺旋”)是在1963年被美籍波兰数学家斯塔尼斯拉夫·乌拉姆(Stanislaw Ulam)(1909年 - 1984年)发现。 他在会议无聊中在一张草稿纸上画了一个简单的整数螺旋。
(from Wikipedia)The Ulam spiral, or prime spiral (in other languages also called the Ulam Cloth) is a simple method of visualizing the prime numbers that reveals the apparent tendency of certain quadratic polynomials to generate unusually large numbers of primes. It was discovered by the mathematician Stanislaw Ulam in 1963, while he was doodling during the presentation of a “long and very boring paper” at a scientific meeting. Shortly afterwards, in an early application of computer graphics, Ulam with collaborators Myron Stein and Mark Wells used MANIAC II at Los Alamos Scientific Laboratory to produce pictures of the spiral for numbers up to 65,000. In March of the following year, Martin Gardner wrote about the Ulam spiral in his Mathematical Games column; the Ulam spiral featured on the front cover of the issue of Scientific American in which the column appeared.

简单的Ulam螺旋

这里写图片描述

Ulam螺旋的染色图

这里写图片描述

比较大的螺旋

这里写图片描述

这里写图片描述

扭曲的螺旋

这里写图片描述

生成基本Ulam Spiral的代码(HTML+JS)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ulam Spiral</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
    body, h1{margin:0;}
    canvas{margin: 20px; }
</style>
</head>
<body onload="draw()">
<canvas id="canvas" width=1000 height=1000 style="border: 0px;"></canvas>
<script>
function isPrime(n) {
    if (isNaN(n) || !isFinite(n) || n%1 || n<2) return false; 
    if (n%2==0) return (n==2);
    if (n%3==0) return (n==3);
    var m=Math.sqrt(n);
    for (var i=5;i<=m;i+=6) {
        if (n%i==0)     return false;
        if (n%(i+2)==0) return false;
    }
    return true;
}

var prime_colors = ["#444444", "#007FFF", "#cccccc", "green", "#ff3300"];
function dot(context, x, y, r, num) {
    context.beginPath();
    context.arc(x, y, r, 0, Math.PI * 2, true);
    if (isPrime(num)) {
        context.fillStyle = prime_colors[(num-1)%10/2];
    } else {
        context.fillStyle = "#f3f3f3";
    }
    context.fill();

    if (1) {
        if (isPrime(num)) {
            context.fillStyle = "white";
        } else {
            context.fillStyle = "#aaaaaa";
        }

        context.textAlign='center';
        context.textBaseline='middle';
        context.font = (r*0.9)+"px Courier New";
        context.fillText(num, x, y);
    }
}

function draw() {
    var a = 40;
    var r = a/2;
    var border_length = 22;
    var cx = r+1000/2;
    var cy = r+1000/2;

    var canvas=document.getElementById('canvas');
    var context=canvas.getContext('2d');
    var num = 1;

    var direction = 0;
    var stride = 1;

    for (k=0; k<border_length; k++) {
        for (var i=0; i<stride; i++) {
            dot(context, cx, cy, r, num);
            if (direction == 0) cx += a;
            if (direction == 1) cx -= a;
            num+=1;
        }
        for (var i=0; i<stride; i++) {
            dot(context, cx, cy, r, num);
            if (direction == 0) cy -= a;
            if (direction == 1) cy += a;
            num+=1;
        }
        direction = ++direction%2;
        stride++;
    }
}
</script>
</body>
</html>
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值