nikolayneykov

Untitled

Oct 8th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2.  
  3. class GreatestCommonDivisor
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         int a = int.Parse(Console.ReadLine());
  8.         int b = int.Parse(Console.ReadLine());
  9.         int commonDivisor = Math.Min(a, b);
  10.         bool isPrime = false;
  11.         while (commonDivisor >= 3)
  12.         {
  13.             if (a % commonDivisor == 0 && b % commonDivisor == 0)
  14.             {
  15.                 for (int divider = 2; divider <= Math.Sqrt(commonDivisor); divider++)
  16.                 {
  17.                     if (commonDivisor % divider == 0)
  18.                     {
  19.                         Console.Write(commonDivisor + " ");
  20.                         isPrime = false;
  21.                         break;
  22.                     }
  23.                 }
  24.             }
  25.             commonDivisor--;
  26.         }
  27.         if (!isPrime)
  28.         {
  29.             Console.WriteLine(-1);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment