次のプログラムは、数値の座標を直接計算することによって機能します。このメソッドNumberToPoint()
は、次のマッピングを実行します。
0 => (x0 , y0 )
1 => (x0 + 1, y0 )
2 => (x0 + 1, y0 - 1)
3 => (x0 , y0 - 1)
4 => (x0 - 1, y0 - 1)
5 => (x0 - 1, y0 )
6 => ...
残りは非常に単純な素数テストと小さなコンソールアプリケーションです。
画像を保存するために、2つの解決策を検討します。画像全体のバッファを作成できる場合は、以下のプログラムを使用してバッファを埋めることができます。
バッファが大きすぎる場合は、メソッドを作成しPointToNumber()
て計算を逆にします。メソッドは2つの座標を取り、この時点で数値を返します。この方法を使用すると、上から下、左から右に繰り返し、この時点で数値を計算し、素数であるかどうかを確認し、バッファーなしでピクセルを出力できます。ただし、どちらのソリューションでも、上下にピクセルを追加するのは非常にコストがかかるため、開始する前に画像サイズを知っておく必要があります(ただし、原因は考えられます)。
質問
- モジュロ、整数除算、および1000回の符号を使用せずに、係数ルックアップを
NumberToPoint()
堅実な数学に変換するための良いアイデアはありますか?
- 素数テストを短縮または高速化するための良いアイデアはありますか?
コード
using System;
using System.Drawing;
using System.Linq;
using System.Threading;
namespace UlamsSpiral
{
public static class Program
{
public static void Main()
{
Int32 width = 60;
Int32 height = 60;
Console.SetWindowSize(Math.Min(width, 120), Math.Min(height, 60));
Console.SetBufferSize(width, height);
Console.CursorVisible = false;
Int32 limit = (Int32)Math.Pow(Math.Min(width, height) - 2, 2);
for (Int32 n = 1; n <= limit; n++)
{
Point point = NumberToPoint(n - 1, width / 2 - 1, height / 2);
Console.ForegroundColor = n.IsPrime() ? ConsoleColor.DarkBlue : ConsoleColor.DarkGray;
Console.SetCursorPosition(point.X, point.Y);
Console.Write('\u25A0');
Console.SetCursorPosition(0, 0);
Console.Write(n);
Thread.Sleep(10);
}
Console.ReadLine();
}
private static Point NumberToPoint(Int32 n, Int32 x0, Int32 y0)
{
Int32[,] c = { { -1, 0, 0, -1, 1, 0 }, { -1, 1, 1, 1, 0, 0 }, { 1, 0, 1, 1, -1, -1 }, { 1, -1, 0, -1, 0, -1 } };
Int32 square = (Int32)Math.Floor(Math.Sqrt(n / 4));
Int32 index;
Int32 side = (Int32)Math.DivRem(n - 4 * square * square, 2 * square + 1, out index);
Int32 x = c[side, 0] * square + c[side, 1] * index + c[side, 2];
Int32 y = c[side, 3] * square + c[side, 4] * index + c[side, 5];
return new Point(x + x0, y + y0);
}
private static Boolean IsPrime(this Int32 n)
{
if (n < 3) return (n == 2);
return Enumerable.Range(2, (Int32)Math.Sqrt(n)).All(m => n % m != 0);
}
}
}