다음 프로그램은 직접 숫자의 좌표를 계산하여 작동합니다. 방법 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 => ...
나머지는 매우 간단한 소수 테스트 및 소형 콘솔 응용 프로그램입니다.
이미지를 저장하기 위해 두 가지 해결책을 고려할 것입니다. 전체 이미지에 대한 버퍼를 만들 수 있다면 아래 프로그램을 사용하여 버퍼를 채울 수 있습니다.
버퍼가 커지면 PointToNumber()
메서드를 만들고 계산을 반전합니다.이 메서드는 두 좌표를 사용하고이 지점에서 숫자를 반환합니다. 이 방법을 사용하면 위에서 아래로, 왼쪽에서 오른쪽으로 반복하고이 시점에서 수를 계산하고 소수인지 확인하고 버퍼없이 진행하면서 픽셀을 출력 할 수 있습니다. 하지만 두 솔루션 모두 이미지 크기는 시작하기 전에 알아야합니다. 상단 및 왼쪽에 픽셀을 추가하는 것은 비용이 많이 듭니다 (하지만 가능할 수도 있기 때문).
질문
- 모듈, 정수 나누기를 사용하지 않고 견고한 수학에
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);
}
}
}
+1 로고를 참조하는 경우 - 좋은 접근 방식입니다! – Nelson