Advertisement
nikolayneykov

Untitled

Oct 8th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 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 limit = Math.Min(a, b);
  10.         bool hasValidNumber = false;
  11.         for (int commonDivisor = 3; commonDivisor <= limit; commonDivisor++)
  12.         {
  13.             if (a % commonDivisor == 0 && b % commonDivisor == 0)
  14.             {
  15.                 bool isPrime = true;
  16.                 for (int divider = 2; divider <= Math.Sqrt(commonDivisor); divider++)
  17.                 {
  18.                     if (commonDivisor % divider == 0)
  19.                     {
  20.                         isPrime = false;
  21.                         break;
  22.                     }
  23.                 }
  24.                 if (isPrime)
  25.                 {
  26.                     Console.Write(commonDivisor + " ");
  27.                     hasValidNumber = true;
  28.                 }
  29.             }
  30.         }
  31.  
  32.         if (!hasValidNumber)
  33.         {
  34.             Console.WriteLine(-1);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement