4DM3M

15

Jan 3rd, 2022
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Drafty
  5. {
  6.     class Program
  7.     {
  8.         public static double Calculate(int[] values)
  9.         {
  10.             if (values.Length <= 1)
  11.             {
  12.                 throw new ArgumentException("должно быть хотя бы два числа");
  13.             }
  14.  
  15.             double q = values[1] / values[0];
  16.  
  17.             if (q == 1)
  18.             {
  19.                 throw new ArgumentException("Прогрессия не является геометрической");
  20.             }
  21.  
  22.             return (double) (values[0] * (1d - Math.Pow(q, values.Length))) / (double) (1d - q);
  23.         }
  24.  
  25.         public static double GetSum(params int[] values)
  26.         {
  27.             try
  28.             {
  29.                 return Calculate(values);
  30.             }
  31.             catch (ArgumentException ex)
  32.             {
  33.                 Console.WriteLine("Ошибка: " + ex.Message);
  34.             }
  35.  
  36.             return -1;
  37.         }
  38.  
  39.         static void Main(string[] args)
  40.         {
  41.             Console.WriteLine(
  42.                 GetSum(81)
  43.                 );
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment