Advertisement
Fekke

Tools

Mar 21st, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. namespace Euler // Warning! Golfed code ahead.
  9. {
  10.     class Tools
  11.     {
  12.         // Checks whether a number is a prime number
  13.         public static bool IsPrime(BigInteger i)
  14.         {
  15.             BigInteger x, y = 0;
  16.             for (x = 2; x < i; x++)
  17.             {
  18.                 if (i % x == 0)
  19.                 {
  20.                     y++;
  21.                 }
  22.             }
  23.             return y==0;
  24.         }
  25.         // Returns the nearest integer below the number
  26.         public static BigInteger Ceiling(BigInteger i)
  27.         {
  28.             return i = BigInteger.Parse(i.ToString("0.#"));
  29.         }
  30.         // Checks whether a number is the square root
  31.         public static bool IsSqrt(BigInteger i, BigInteger r)
  32.         {
  33.             BigInteger x, y;
  34.             x = r * r;
  35.             y = (r + 1) * (r + 1);
  36.             return (i >= x && i < y);
  37.         }
  38.         // Returns the perfect square root of a number (round down)
  39.         public static BigInteger GetPerfectSqrt(BigInteger i)
  40.         {
  41.             if (i == 0) return i;
  42.             BigInteger x, r;
  43.             if (i > 0)
  44.             {
  45.                 x = Ceiling((BigInteger)BigInteger.Log(i, 2));
  46.                 r = BigInteger.One << (Convert.ToInt32(x) / 2);
  47.                 while (!IsSqrt(i, r))
  48.                 {
  49.                     r += i / r;
  50.                     r /= 2;
  51.                 }
  52.                 return r;
  53.             }
  54.             throw new ArithmeticException("Input was not a posetive integer");
  55.         }
  56.         // Return the number of factors of a number
  57.         public static BigInteger GetFactorCount(BigInteger i)
  58.         {
  59.             BigInteger x, y, a = 0;
  60.             y = GetPerfectSqrt(i);
  61.             for (x = 1; x < y; x++)
  62.             {
  63.                 if (y % i == 0)
  64.                 {
  65.                     a += 2;
  66.                 }
  67.             }
  68.             if (y*y==i)
  69.             {
  70.                 a++;
  71.             }
  72.             return a;
  73.         }
  74.         // Return all factors of a number
  75.         public static BigInteger[] GetFactors(BigInteger i)
  76.         {
  77.             BigInteger x, y, z = 0;
  78.             List<BigInteger> a = new List<BigInteger>();
  79.             y = GetPerfectSqrt(i);
  80.             for (x = 1; x < y; x++)
  81.             {
  82.                 if (y % i == 0)
  83.                 {
  84.                     z += 2;
  85.                     a.Add(z);
  86.                 }
  87.             }
  88.             return a.ToArray();
  89.         }
  90.         // Return all prime factors of a number
  91.         public static BigInteger[][] GetPrimeFactors(BigInteger i)
  92.         {
  93.             BigInteger x, y;
  94.             List<BigInteger[]> a = new List<BigInteger[]>();
  95.             for (x = 2; i > 1; x++)
  96.             {
  97.                 if (i%x == 0)
  98.                 {
  99.                     y = 0;
  100.                     while (i%x==0)
  101.                     {
  102.                         i /= x;
  103.                         y++;
  104.                     }
  105.                     BigInteger[] z = {x,y};
  106.                     a.Add(z);
  107.                 }
  108.             }
  109.             return a.ToArray();
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement