Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Numerics;
- namespace Euler // Warning! Golfed code ahead.
- {
- class Tools
- {
- // Checks whether a number is a prime number
- public static bool IsPrime(BigInteger i)
- {
- BigInteger x, y = 0;
- for (x = 2; x < i; x++)
- {
- if (i % x == 0)
- {
- y++;
- }
- }
- return y==0;
- }
- // Returns the nearest integer below the number
- public static BigInteger Ceiling(BigInteger i)
- {
- return i = BigInteger.Parse(i.ToString("0.#"));
- }
- // Checks whether a number is the square root
- public static bool IsSqrt(BigInteger i, BigInteger r)
- {
- BigInteger x, y;
- x = r * r;
- y = (r + 1) * (r + 1);
- return (i >= x && i < y);
- }
- // Returns the perfect square root of a number (round down)
- public static BigInteger GetPerfectSqrt(BigInteger i)
- {
- if (i == 0) return i;
- BigInteger x, r;
- if (i > 0)
- {
- x = Ceiling((BigInteger)BigInteger.Log(i, 2));
- r = BigInteger.One << (Convert.ToInt32(x) / 2);
- while (!IsSqrt(i, r))
- {
- r += i / r;
- r /= 2;
- }
- return r;
- }
- throw new ArithmeticException("Input was not a posetive integer");
- }
- // Return the number of factors of a number
- public static BigInteger GetFactorCount(BigInteger i)
- {
- BigInteger x, y, a = 0;
- y = GetPerfectSqrt(i);
- for (x = 1; x < y; x++)
- {
- if (y % i == 0)
- {
- a += 2;
- }
- }
- if (y*y==i)
- {
- a++;
- }
- return a;
- }
- // Return all factors of a number
- public static BigInteger[] GetFactors(BigInteger i)
- {
- BigInteger x, y, z = 0;
- List<BigInteger> a = new List<BigInteger>();
- y = GetPerfectSqrt(i);
- for (x = 1; x < y; x++)
- {
- if (y % i == 0)
- {
- z += 2;
- a.Add(z);
- }
- }
- return a.ToArray();
- }
- // Return all prime factors of a number
- public static BigInteger[][] GetPrimeFactors(BigInteger i)
- {
- BigInteger x, y;
- List<BigInteger[]> a = new List<BigInteger[]>();
- for (x = 2; i > 1; x++)
- {
- if (i%x == 0)
- {
- y = 0;
- while (i%x==0)
- {
- i /= x;
- y++;
- }
- BigInteger[] z = {x,y};
- a.Add(z);
- }
- }
- return a.ToArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement