Skip to content

Utilities of use when coding with numbers in C# in a readable way

License

Notifications You must be signed in to change notification settings

Aha43/NumberUtil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NumberUtil

Utilities of use when coding with numbers (mostly integers) in a readable way using C#

Background

Coding challenges (for example the Euler project's problems) in C# I started to re-use code over problems and decided to put that stuff in this library.

ToDo

Guidelines for the design of the library

Coding with IEnumerable of numbers (int, long)

Using Linq we can do

var isTrue = new int[] { 5, 20 }.Sum() == 25;

and with NumberUtil we can also do

var alsoTrue = new int[] { 5, 5 }.Product() == 25;

Coding with numbers

Number even or odd? (int, long)
16.IsEven(); // returns true
16.IsOdd(); // returns false
17.IsEven(); // returns false
-17.IsOdd(); // return true

Coding with the digits of numbers

Number of digits a number have (int, long and BigInteger)
12345.NumberOfDigits(); // Returns 5
(-12345).NumberOfDigits(); // Also returns 5: sign not counted
BigInteger.Multiply(long.MaxValue, 1000).NumberOfDigits(); // Returns 22
BigInteger.Multiply(long.MaxValue, -1000).NumberOfDigits(); // Also returns 22

The parentheses are needed for the negative number or it would mean applying the unary operator - on the result of 12345.NumberOfDigits().

Getting digits of numbers (int, long and BigInteger numbers >= 0)
var dig = 12345.Digits(); // Gets array with digits 1, 2, 3, 4 and 5 (dig[0] = 1 and dig[4] = 5)
Given digits in base 10 get the number (int, long)
var digits = 54321.Digits();
var value = digits.Number(); // value = 54321.

Prime numbers

Test if number is prime number (int and long)
101.IsPrime(); // return true
99.IsPrime(); // return false
Get prime factors of a number (int and long)
var factors = 30.PrimeFactors().ToArray(); // Array will contain 2, 3 and 5. 

Primes are not iterated over in a sorted order.

Making a (Eratosthenes) sieve with the prime up to and including N of 211 (211 happens to be a prime)
var N = 211;
var primeNumbers = new PrimeNumbers(N); // the sieve
Using the sieve to get number of prime numbers in range [0...N]
var numberOfPrimes = primeNumbers.Count; // will be 47 if N = 211
Using the sieve to test if numbers in the range [0...N] is prime number or not (assumes N = 211)
primeNumbers.IsPrime(101); // true
primeNumbers.IsPrime(210); // false
primeNumbers.IsPrime(211); // true
PrimeNumbers instance is an IEnumerable over primes up to N
var largest = primeNumbers.Last(); // using LINQ to get the largest prime less or equal to N.

Factorials (int and long)

var fact1 = 5.Factorial(); // All componenets of tuple factorial (AsInt, AsLong and Value) assigned 5!
var fact2 = 13.Factorial(); // AsInt is 0 since 13! result do not fit an int, AsLong and Value is assigned 13!.
var fact3 = 50.Factorial(); // Only Value (BigInteger) assigned since 50! do not fit long.

Polynomial (int[] and long[])

Given a array with coefficients of a polynomial p p(x) can be evaluated:

var p = new int[] { 3, -2, 5 };
var y = p.Polynomial(5); // 3*5^2 - 2*5 + 5 = 70

Number sequences

Iterators

Example of iterating over a number sequence, iterating over Fibonacci numbers

var fibSum = Sequences.Fibonacci.Take(6).Sum(); // Sums 0, 1, 1, 2, 3 and 5 : fibSum = 12

See the class Sequences for sequences implemented.

Closed forms (int and long)

Finding sum of the five first perfect square numbers

var sqSum = ClosedForms.SumOfSquares(5); // sqSum = 55 = 1 + 4 + 9 + 16 + 25

Finding the 5th Fibonacci number

var fib = ClosedForms.NthFibonacci(5); // fib = 5

See the class ClosedForms for closed forms implemented.

About

Utilities of use when coding with numbers in C# in a readable way

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages