Advertisement
braveheart1989

Square Numbers

May 30th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         List<double> numbers = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToList();
  10.         List<double> squareNumbers = new List<double>();
  11.         for (int i = 0; i < numbers.Count; i++)
  12.         {
  13.             if ((numbers[i] == MathPow(MathSqrt(numbers[i])) && numbers[i]!=0))
  14.             {
  15.                 squareNumbers.Add(numbers[i]);
  16.             }
  17.         }
  18.         squareNumbers.Sort();
  19.         squareNumbers.Reverse();
  20.         Console.WriteLine(string.Join(" ", squareNumbers));
  21.     }
  22.  
  23.     static double MathPow(double num)
  24.     {
  25.         double result;
  26.  
  27.         result = num*num;
  28.         return result;
  29.     }
  30.  
  31.     public static double MathSqrt(double num)
  32.     {
  33.         if (num == 0)
  34.         {
  35.             return 0;
  36.         }
  37.         double n = (num / 2) + 1;
  38.         double n1 = (n + num / n) / 2;
  39.         while (n1 < n)
  40.         {
  41.             n = n1;
  42.             n1 = (n + (num / n)) / 2;
  43.         } // end while  
  44.         return n;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement