Skip to content

Instantly share code, notes, and snippets.

@maisterpl
Created March 17, 2021 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maisterpl/c049d4b535bbae18928715e4ce93c435 to your computer and use it in GitHub Desktop.
Save maisterpl/c049d4b535bbae18928715e4ce93c435 to your computer and use it in GitHub Desktop.
liczby pierwsze
// program sprawdzajacy czy liczba jest liczba pierwsza
using System;
namespace liczby_pierwsze_spoj
{
class Program
{
static bool czypierwsza(int liczba)
{
if (liczba == 2) return true;
else if (liczba % 2 == 0) return false;
for (int j = 3; j <= Math.Sqrt(liczba); j += 2)
{
if (liczba % j == 0)
{
return false;
}
}
return true;
}
static void Main()
{
/*
for (int i = 9000; i < 10000; i++)
{
Console.WriteLine("{0}, {1}", i, czypierwsza(i) ? "Tak" : "Nie");
}
Console.WriteLine(czypierwsza(121) ? "Tak" : "Nie");
Console.WriteLine(czypierwsza(169) ? "Tak" : "Nie");
Console.WriteLine(czypierwsza(2) ? "Tak" : "Nie");
*/
int n; // liczba testow
n = Int32.Parse(Console.ReadLine());
int[] tablica = new int[n];
for (int i = 0; i < n; i++)
{
tablica[i] = Int32.Parse(Console.ReadLine());
}
foreach (int i in tablica)
{
Console.WriteLine(czypierwsza(i) ? "Tak" : "Nie");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment