Advertisement
yahorrr

Untitled

Apr 14th, 2022
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.37 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace Gcd
  5. {
  6.     /// <summary>
  7.     /// Provide methods with integers.
  8.     /// </summary>
  9.     public static class IntegerExtensions
  10.     {
  11.         /// <summary>
  12.         /// Calculates GCD of two integers from [-int.MaxValue;int.MaxValue] by the Euclidean algorithm.
  13.         /// </summary>
  14.         /// <param name="a">First integer.</param>
  15.         /// <param name="b">Second integer.</param>
  16.         /// <returns>The GCD value.</returns>
  17.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  18.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or two numbers are int.MinValue.</exception>
  19.         public static int GetGcdByEuclidean(int a, int b)
  20.         {
  21.             if (a == int.MinValue)
  22.             {
  23.                 throw new ArgumentOutOfRangeException(nameof(a), "Argument a out of range");
  24.             }
  25.  
  26.             if (b == int.MinValue)
  27.             {
  28.                 throw new ArgumentOutOfRangeException(nameof(b), "Argument b out of range");
  29.             }
  30.  
  31.             a = Math.Abs(a);
  32.             b = Math.Abs(b);
  33.  
  34.             if (a == 0 && b == 0)
  35.             {
  36.                 throw new ArgumentException("Argument's can't be 0 at the same time", nameof(a));
  37.             }
  38.  
  39.             while (a != 0 && b != 0)
  40.             {
  41.                 if (a > b)
  42.                 {
  43.                     a %= b;
  44.                 }
  45.                 else
  46.                 {
  47.                     b %= a;
  48.                 }
  49.             }
  50.  
  51.             return a + b;
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Calculates GCD of three integers from [-int.MaxValue;int.MaxValue] by the Euclidean algorithm.
  56.         /// </summary>
  57.         /// <param name="a">First integer.</param>
  58.         /// <param name="b">Second integer.</param>
  59.         /// <param name="c">Third integer.</param>
  60.         /// <returns>The GCD value.</returns>
  61.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  62.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>
  63.         public static int GetGcdByEuclidean(int a, int b, int c)
  64.         {
  65.             if (a == 0 && b == 0 && c == 0)
  66.             {
  67.                 throw new ArgumentException("Argument's can't be 0 at the same time", nameof(a));
  68.             }
  69.  
  70.             if (a != 0)
  71.             {
  72.                 return GetGcdByEuclidean(GetGcdByEuclidean(a, b), c);
  73.             }
  74.             else
  75.             {
  76.                 return GetGcdByEuclidean(GetGcdByEuclidean(c, b), a);
  77.             }
  78.         }
  79.  
  80.         /// <summary>
  81.         /// Calculates the GCD of integers from [-int.MaxValue;int.MaxValue] by the Euclidean algorithm.
  82.         /// </summary>
  83.         /// <param name="a">First integer.</param>
  84.         /// <param name="b">Second integer.</param>
  85.         /// <param name="other">Other integers.</param>
  86.         /// <returns>The GCD value.</returns>
  87.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  88.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>      
  89.         public static int GetGcdByEuclidean(int a, int b, params int[] other)
  90.         {
  91.             if (a == 0 && b == 0 && AllZeros(other))
  92.             {
  93.                 throw new ArgumentException("Argument's can't be 0 at the same time", nameof(a));
  94.             }
  95.  
  96.             int gcd = 0;
  97.  
  98.             if (a != 0 || b != 0)
  99.             {
  100.                 gcd = GetGcdByEuclidean(a, b);
  101.             }
  102.  
  103.             for (int i = 0; i < other.Length; i++)
  104.             {      
  105.                 if (other[i] != 0)
  106.                 {
  107.                     gcd = GetGcdByEuclidean(gcd, other[i]);
  108.                 }
  109.             }
  110.  
  111.             return gcd;
  112.         }      
  113.  
  114.         /// <summary>
  115.         /// Calculates GCD of two integers [-int.MaxValue;int.MaxValue] by the Stein algorithm.
  116.         /// </summary>
  117.         /// <param name="a">First integer.</param>
  118.         /// <param name="b">Second integer.</param>
  119.         /// <returns>The GCD value.</returns>
  120.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  121.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or two numbers are int.MinValue.</exception>
  122.         public static int GetGcdByStein(int a, int b)
  123.         {
  124.             if (a == int.MinValue)
  125.             {
  126.                 throw new ArgumentOutOfRangeException(nameof(a), "Argument a out of range");
  127.             }
  128.  
  129.             if (b == int.MinValue)
  130.             {
  131.                 throw new ArgumentOutOfRangeException(nameof(b), "Argument b out of range");
  132.             }
  133.  
  134.             if (a == 0 && b == 0)
  135.             {
  136.                 throw new ArgumentException("Argument's can't be 0 at the same time", nameof(a));
  137.             }
  138.  
  139.             a = Math.Abs(a);
  140.             b = Math.Abs(b);
  141.  
  142.             if (a == 0)
  143.             {
  144.                 return b;
  145.             }
  146.             else if (b == 0)
  147.             {
  148.                 return a;
  149.             }
  150.             else if (a == b)
  151.             {
  152.                 return a;
  153.             }
  154.             else if (a % 2 == 0 && b % 2 == 0)
  155.             {
  156.                 return 2 * GetGcdByStein(a / 2, b / 2);
  157.             }
  158.             else if (a % 2 == 0 && b % 2 != 0)
  159.             {
  160.                 return GetGcdByStein(a / 2, b);
  161.             }
  162.             else if (a % 2 != 0 && b % 2 == 0)
  163.             {
  164.                 return GetGcdByStein(a, b / 2);
  165.             }
  166.             else if (a > b)
  167.             {
  168.                 return GetGcdByStein((a - b) / 2, b);
  169.             }
  170.             else
  171.             {
  172.                 return GetGcdByStein((b - a) / 2, a);
  173.             }
  174.         }
  175.  
  176.         /// <summary>
  177.         /// Calculates GCD of three integers [-int.MaxValue;int.MaxValue] by the Stein algorithm.
  178.         /// </summary>
  179.         /// <param name="a">First integer.</param>
  180.         /// <param name="b">Second integer.</param>
  181.         /// <param name="c">Third integer.</param>
  182.         /// <returns>The GCD value.</returns>
  183.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  184.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>
  185.         public static int GetGcdByStein(int a, int b, int c)
  186.         {
  187.             if (a == 0 && b == 0 && c == 0)
  188.             {
  189.                 throw new ArgumentException("Argument's can't be 0 at the same time", nameof(a));
  190.             }
  191.  
  192.             if (a != 0)
  193.             {
  194.                 return GetGcdByStein(GetGcdByStein(a, b), c);
  195.             }
  196.             else
  197.             {
  198.                 return GetGcdByStein(GetGcdByStein(c, b), a);
  199.             }
  200.         }
  201.  
  202.         /// <summary>
  203.         /// Calculates the GCD of integers [-int.MaxValue;int.MaxValue] by the Stein algorithm.
  204.         /// </summary>
  205.         /// <param name="a">First integer.</param>
  206.         /// <param name="b">Second integer.</param>
  207.         /// <param name="other">Other integers.</param>
  208.         /// <returns>The GCD value.</returns>
  209.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  210.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>
  211.         public static int GetGcdByStein(int a, int b, params int[] other)
  212.         {
  213.             if (a == 0 && b == 0 && AllZeros(other))
  214.             {
  215.                 throw new ArgumentException("Argument's can't be 0 at the same time", nameof(a));
  216.             }
  217.  
  218.             int gcd = 0;
  219.  
  220.             if (a != 0 || b != 0)
  221.             {
  222.                 gcd = GetGcdByStein(a, b);
  223.             }
  224.  
  225.             for (int i = 0; i < other.Length; i++)
  226.             {
  227.                 if (other[i] != 0)
  228.                 {
  229.                     gcd = GetGcdByStein(gcd, other[i]);
  230.                 }
  231.             }
  232.  
  233.             return gcd;
  234.         }
  235.  
  236.         /// <summary>
  237.         /// Calculates GCD of two integers from [-int.MaxValue;int.MaxValue] by the Euclidean algorithm with elapsed time.
  238.         /// </summary>
  239.         /// <param name="elapsedTicks">Method execution time in ticks.</param>
  240.         /// <param name="a">First integer.</param>
  241.         /// <param name="b">Second integer.</param>
  242.         /// <returns>The GCD value.</returns>
  243.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  244.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or two numbers are int.MinValue.</exception>
  245.         public static int GetGcdByEuclidean(out long elapsedTicks, int a, int b)
  246.         {
  247.             var timer = new Stopwatch();
  248.             timer.Start();
  249.             int result = GetGcdByEuclidean(a, b);
  250.             timer.Stop();
  251.             elapsedTicks = timer.ElapsedTicks;
  252.  
  253.             return result;
  254.         }
  255.  
  256.         /// <summary>
  257.         /// Calculates GCD of three integers from [-int.MaxValue;int.MaxValue] by the Euclidean algorithm with elapsed time.
  258.         /// </summary>
  259.         /// <param name="elapsedTicks">Method execution time in ticks.</param>
  260.         /// <param name="a">First integer.</param>
  261.         /// <param name="b">Second integer.</param>
  262.         /// <param name="c">Third integer.</param>
  263.         /// <returns>The GCD value.</returns>
  264.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  265.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>
  266.         public static int GetGcdByEuclidean(out long elapsedTicks, int a, int b, int c)
  267.         {
  268.             var timer = new Stopwatch();
  269.             timer.Start();
  270.             int result = GetGcdByEuclidean(a, b, c);
  271.             timer.Stop();
  272.             elapsedTicks = timer.ElapsedTicks;
  273.  
  274.             return result;
  275.         }
  276.  
  277.         /// <summary>
  278.         /// Calculates the GCD of integers from [-int.MaxValue;int.MaxValue] by the Euclidean algorithm with elapsed time.
  279.         /// </summary>
  280.         /// <param name="elapsedTicks">Method execution time in Ticks.</param>
  281.         /// <param name="a">First integer.</param>
  282.         /// <param name="b">Second integer.</param>
  283.         /// <param name="other">Other integers.</param>
  284.         /// <returns>The GCD value.</returns>
  285.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  286.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>
  287.         public static int GetGcdByEuclidean(out long elapsedTicks, int a, int b, params int[] other)
  288.         {
  289.             var timer = new Stopwatch();
  290.             timer.Start();
  291.             int result = GetGcdByEuclidean(a, b, other);
  292.             timer.Stop();
  293.             elapsedTicks = timer.ElapsedTicks;
  294.  
  295.             return result;
  296.         }
  297.  
  298.         /// <summary>
  299.         /// Calculates GCD of two integers from [-int.MaxValue;int.MaxValue] by the Stein algorithm with elapsed time.
  300.         /// </summary>
  301.         /// <param name="elapsedTicks">Method execution time in ticks.</param>
  302.         /// <param name="a">First integer.</param>
  303.         /// <param name="b">Second integer.</param>
  304.         /// <returns>The GCD value.</returns>
  305.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  306.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or two numbers are int.MinValue.</exception>
  307.         public static int GetGcdByStein(out long elapsedTicks, int a, int b)
  308.         {
  309.             var timer = new Stopwatch();
  310.             timer.Start();
  311.             int result = GetGcdByStein(a, b);
  312.             timer.Stop();
  313.             elapsedTicks = timer.ElapsedTicks;
  314.  
  315.             return result;
  316.         }
  317.  
  318.         /// <summary>
  319.         /// Calculates GCD of three integers from [-int.MaxValue;int.MaxValue] by the Stein algorithm with elapsed time.
  320.         /// </summary>
  321.         /// <param name="elapsedTicks">Method execution time in ticks.</param>
  322.         /// <param name="a">First integer.</param>
  323.         /// <param name="b">Second integer.</param>
  324.         /// <param name="c">Third integer.</param>
  325.         /// <returns>The GCD value.</returns>
  326.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  327.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>
  328.         public static int GetGcdByStein(out long elapsedTicks, int a, int b, int c)
  329.         {
  330.             var timer = new Stopwatch();
  331.             timer.Start();
  332.             int result = GetGcdByStein(a, b, c);
  333.             timer.Stop();
  334.             elapsedTicks = timer.ElapsedTicks;
  335.  
  336.             return result;
  337.         }
  338.  
  339.         /// <summary>
  340.         /// Calculates the GCD of integers from [-int.MaxValue;int.MaxValue] by the Stein algorithm with elapsed time.
  341.         /// </summary>
  342.         /// <param name="elapsedTicks">Method execution time in Ticks.</param>
  343.         /// <param name="a">First integer.</param>
  344.         /// <param name="b">Second integer.</param>
  345.         /// <param name="other">Other integers.</param>
  346.         /// <returns>The GCD value.</returns>
  347.         /// <exception cref="ArgumentException">Thrown when all numbers are 0 at the same time.</exception>
  348.         /// <exception cref="ArgumentOutOfRangeException">Thrown when one or more numbers are int.MinValue.</exception>
  349.         public static int GetGcdByStein(out long elapsedTicks, int a, int b, params int[] other)
  350.         {
  351.             var timer = new Stopwatch();
  352.             timer.Start();
  353.             int result = GetGcdByStein(a, b, other);
  354.             timer.Stop();
  355.             elapsedTicks = timer.ElapsedTicks;
  356.  
  357.             return result;
  358.         }
  359.  
  360.         private static bool AllZeros(int[] array)
  361.         {
  362.             for (int i = 0; i < array.Length; i++)
  363.             {
  364.                 if (array[i] != 0)
  365.                 {
  366.                     return false;
  367.                 }
  368.             }
  369.  
  370.             return true;
  371.         }
  372.     }
  373. }
  374.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement