Spiral primes | Read about the problem in the links below
Previous post for Project Euler Problem # 58
https://projecteuler.net/problem=58
public Form1() { InitializeComponent(); } public static Brush aBrush = (Brush)Brushes.White; public int current_x = 200; public int current_y = 200; public static string path_collect = ""; public int cur_step = 1; public static int Current_Direction = 0; public bool first_turn = false; public int count = 5; public int skip_at = 7; public int skip = 3; public int diag_count = 4; public int prime_count = 2; double percentage = 100.0; protected void Move() { cur_step += 1; Graphics g = this.CreateGraphics(); if (cur_step == skip_at) { diag_count += 1; if (isPrime(cur_step)) { aBrush = (Brush)Brushes.Red; prime_count += 1; } else aBrush = (Brush)Brushes.Black; } else aBrush = (Brush)Brushes.Black; if (Current_Direction == 0) { g.FillRectangle(aBrush, current_x + 2, current_y, 2, 2); current_x += 2; path_collect = "→"; } else if (Current_Direction == 3) { g.FillRectangle(aBrush, current_x, current_y + 2, 2, 2); current_y += 2; path_collect = "↓"; } else if (Current_Direction == 2) { g.FillRectangle(aBrush, current_x - 2, current_y, 2, 2); current_x -= 2; path_collect = "←"; } else if (Current_Direction == 1) { g.FillRectangle(aBrush, current_x, current_y - 2, 2, 2); current_y -= 2; path_collect = "↑"; } percentage = Math.Round((System.Convert.ToDouble(prime_count) / System.Convert.ToDouble(diag_count)), 2); label1.Text = "Number " + cur_step + " " + path_collect + " Diag " + diag_count + " Primes " + prime_count + " Percent " + String.Format("{0:P2}", percentage) ; if (cur_step <= 3) Turn(); if (cur_step == 5)//2 Turn(); if (skip_at == cur_step) { skip_at += skip; if (first_turn == false) first_turn = true; else { first_turn = false; skip += 1; } Turn(); } count += 1; } private void bntGo_Click(object sender, EventArgs e) { bntGo.Enabled = false; backgroundWorker1.RunWorkerAsync(); } protected void Turn() { if (Current_Direction == 3) Current_Direction = 0; else Current_Direction += 1; } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while (true) Move(); } public static bool isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i < n; ++i) { if ((n % i) == 0) return false; } return true; } private void Form1_Paint(object sender, PaintEventArgs e) { int x = 10; int y = 10; for (int i = 1; i < 41; i++) { for (int j = 1; j < 41; j++) { e.Graphics.DrawEllipse(Pens.Black, x * i, y * j, 1, 1); } } Graphics g = this.CreateGraphics(); g.FillRectangle(aBrush, current_x, current_y, 2, 2); } |