Advertisement
Danielos168

Sekwencja liczb pierwszych w tablicy

Jan 10th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. namespace ConsoleApp20
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             int[] tab =
  8.             {
  9.                 2, 3, 5, 7, 11, 13
  10.             };
  11.             Console.WriteLine(IleSekwencji(tab));
  12.             Console.ReadKey();
  13.         }
  14.  
  15.         static int IleSekwencji(int[] tab)
  16.         {
  17.             int ile = 0;
  18.             int liczba;
  19.             for (int i = 0; i < tab.Length-1; i++)
  20.             {
  21.                 liczba = tab[i];
  22.                 if (CzyPierwsza(liczba))
  23.                 {
  24.                     liczba = tab[i + 1];
  25.                     if (CzyPierwsza(liczba))
  26.                     {
  27.                         ile++;
  28.                     }
  29.                 }
  30.             }
  31.  
  32.             return ile;
  33.         }
  34.  
  35.         static bool CzyPierwsza(int liczba)
  36.         {
  37.             if (liczba == 2) return true;
  38.             for (int i = 2; i < liczba; i++)
  39.             {
  40.                 if (liczba % i == 0)
  41.                 {
  42.                     return false;
  43.                 }
  44.             }
  45.  
  46.             return true;
  47.         }
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement