using System; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms; namespace Ulam { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool[] NotPrimes = new bool[2]; void RecalculatePrimesUpTo( int limit ) { var n = limit+1; NotPrimes = new bool[n]; for ( int i=2 ; i<=n/2 ; ++i ) { if (!NotPrimes[i]) { for (int j = 2 * i; j < n; j += i) { NotPrimes[j] = true; } } } } bool IsPrime( int n ) { if ( NotPrimes.Length <= n ) RecalculatePrimesUpTo(Math.Max(n,2*NotPrimes.Length)); return !NotPrimes[n]; } int ToSpiralN( int x, int y ) { var ring = Math.Max(Math.Abs(x),Math.Abs(y)); int n = ring * (ring+1) * 4; if ( y == +ring ) n = n - 0*ring + x - ring; else if ( x == -ring ) n = n - 2*ring + y - ring; else if ( y == -ring ) n = n - 4*ring - x + ring; else if ( x == +ring ) n = n - 6*ring - y + ring; else Debug.Fail("Impossible... the balls aren't there!"); return n; } long LongestPaint = 0; unsafe protected override void OnPaint( PaintEventArgs e ) { var w = ClientSize.Width; var h = ClientSize.Height; var stopwatch = new Stopwatch(); stopwatch.Start(); using ( var bitmap = new Bitmap(w,h,PixelFormat.Format32bppArgb) ) { var bits = bitmap.LockBits( ClientRectangle, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb ); for ( int y=0 ; y