0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ガウス素数

Posted at

はじめに

ガウス素数を表示するプログラムです。

(乱数で上下左右斜めに対象な画像を生成しても、似たような感じになってしまうので、絵的にはあんまり、面白くなかったです)

ガウス素数

例えば、ある複素数zを考え、絶対値(norm)の二乗が素数であるかをチェックします。

z = 2+3i のとき |z|^2=2^2+3^2=13

13は素数なので、zはガウス素数という考え方。

また、

z = 2-3i のとき |z|^2=2^2+(-3)^2=13
z = -2+3i のとき |z|^2=(-2)^2+3^2=13
z = -2-3i のとき |z|^2=(-2)^2+(-3)^2=13

なので、可視化したときに、複素平面上で上下対象、左右対称になる。
さらに、実部と虚部を入れ替え、

z = 3+2i のとき |z|^2=3^2+2^2=13

となるので、複素平面上で45度の軸でも対象になる。

下図の白点がガウス素数。
実軸、虚軸の上は4で割ってあまり3となる素数が対象。

\begin{multline}
. \\
\sqrt{2}=\sqrt{1^2+1^2}=1.4142135 \\
\sqrt{5}=\sqrt{1^2+2^2}=2.236068 \\
3 \\
\sqrt13=\sqrt{2^2+3^2}=3.6055512 \\
\sqrt17=\sqrt{1^2+4^2}=4.1231055 \\
\sqrt29=\sqrt{2^2+5^2}=5.3851647 \\
\sqrt37=\sqrt{1^2+6^2}=6.0827627 \\
\sqrt41=\sqrt{4^2+5^2}=6.4031243 \\
7 \\
.
\end{multline}

同心円を重ねた。
緑は半径3と7。
赤は半径が上記の数値。

GaussianPrime同心円.png

環境

processing 4.2
Complex Numbersライブラリ利用

プログラム

第一象限だけ計算しています。Y軸反転なので、左上が原点(0+0i)です。
サイズはwikiの画像サイズに合わせました。
GaussianPrime.png

Processing
void setup() {
  size(384, 384);
  background(0);
  fill(0);

  for (int a = 0; a <= 384; a++) {
    for (int b = 0; b <= 384; b++) {
      if (a == 0 && b == 0) continue;
      if (isGaussianPrime(a, b)) {
        set(a, b, 0xFFFFFFFF);
      }
    }
  }
  //save("GaussianPrime.png");
}

boolean isGaussianPrime(int a, int b) {
  if (a == 0) {
    return isPrime(b) && b % 4 == 3;
  }
  if (b == 0) {
    return isPrime(a) && a % 4 == 3;
  }
  int norm = a * a + b * b;
  return isPrime(norm);
}

boolean isPrime(int n) {
  if (n < 2) return false;
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) return false;
  }
  return true;
}

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?