Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace PrimeNumbers
- {
- class Program
- {
- static bool[] arr;
- static void Main(string[] args)
- {
- int input = 0;
- do
- {
- Console.Clear();
- Console.Write("Bitte Zahl eingeben: ");
- int.TryParse(Console.ReadLine(), out input);
- if (input < 0)
- {
- input = 0;
- }
- InitArray(input);
- Console.WriteLine(!arr[input] ? "Zahl ist Prim." : "Zahl ist nicht Prim.");
- } while (Console.ReadKey().Key != ConsoleKey.Escape);
- }
- static void InitArray(int n)
- {
- if (n < 2)
- {
- n = 2;
- }
- arr = new bool[n + 1];
- arr[0] = true;
- arr[1] = true;
- int length = arr.Length;
- for (int i = 2; i < length; i++)
- {
- if (!arr[i])
- {
- for (int j = 2 * i; j < length; j += i)
- {
- arr[j] = true;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement