using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Eratosthenes { public partial class Form1 : Form { ulong mez; ulong pocet; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { textBox_mez.Text = ""; textBox_mez.Focus(); textBox_vystup.Clear(); label_pocet.Text = ""; label_pocet.Visible = false; } private void textBox_mez_KeyPress(object sender, KeyPressEventArgs e) { if (!(e.KeyChar >= 48 && e.KeyChar <= 57)) { e.KeyChar = Convert.ToChar(0); } } private void button_vymaz_Click(object sender, EventArgs e) { textBox_mez.Text = ""; textBox_mez.Focus(); textBox_vystup.Clear(); label_pocet.Text = ""; label_pocet.Visible = false; } private void pictureBox1_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("https://www.itnetwork.cz/algoritmy/matematicke/algoritmus-eratosthenovo-sito"); } private void button_vypocet_Click(object sender, EventArgs e) { pocet = 0; textBox_vystup.Clear(); string hodnota; if (textBox_mez.Text == "") { MessageBox.Show("Musíte zadat číslo!", "Upozornění", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { try { mez = Convert.ToUInt64(textBox_mez.Text); } catch (OverflowException) { MessageBox.Show("Zadejte menší číslo!", "Varování", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox_mez.Text = ""; textBox_mez.Focus(); return; } if (mez > 10000000) { MessageBox.Show("Algoritmus je efektivní do 10 000 000!", "Varování", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox_mez.Text = ""; textBox_mez.Focus(); return; } else { bool[] sito = new bool[mez + 1]; for (ulong i = 0; i <= mez; i++) { sito[i] = true; //true - je prvočíslo, false - není prvočíslo } sito[0] = false; sito[1] = false; for (ulong j = 2; j <= Math.Sqrt(mez); j++) { if (sito[j] == true) { for (ulong k = j + 1; k <= mez; k++) { if (k % j == 0) { sito[k] = false; } } } } /* //Výpis přímo do textBoxu int vypis = 1; char pad = ' '; for (ulong cisla = 2; cisla <= mez; cisla++) { if (sito[cisla] == true) { hodnota = Convert.ToString(cisla); textBox_vystup.AppendText(hodnota.PadLeft(10,pad)); vypis++; if (vypis == 6) { textBox_vystup.AppendText(Environment.NewLine); vypis = 1; } pocet++; } } //Konec zápisu do textBoxu */ //Zápis do StringBuilderu StringBuilder sb = new StringBuilder(); int vypis = 1; char pad = ' '; for (ulong cisla = 2; cisla <= mez; cisla++) { if (sito[cisla] == true) { sb.Append(Convert.ToString(cisla).PadLeft(10, pad)); vypis++; if (vypis == 6) { sb.AppendLine(); vypis = 1; } pocet++; } } textBox_vystup.Text = sb.ToString(); //Konec zápisu do StringBuilderu label_pocet.Visible = true; label_pocet.Text = "Počet prvočísel : "+pocet; } } } private void textBox_mez_TextChanged(object sender, EventArgs e) { textBox_vystup.Clear(); label_pocet.Text = ""; label_pocet.Visible = false; } private void button_uloz_Click(object sender, EventArgs e) { SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.InitialDirectory = System.Environment.CurrentDirectory; saveDialog.Filter = "Textový soubor (*.txt)|*.txt"; saveDialog.RestoreDirectory = true; if (saveDialog.ShowDialog() == DialogResult.OK) { string[] radky = new string[pocet]; radky = textBox_vystup.Lines; File.WriteAllLines(saveDialog.FileName, radky); } else { MessageBox.Show("Data nebyla uložena!", "Varování", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } } }