Skip to content

Instantly share code, notes, and snippets.

@arialdomartini
Created August 21, 2023 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arialdomartini/5ad173f42f7ecbc8b775a277887714ab to your computer and use it in GitHub Desktop.
Save arialdomartini/5ad173f42f7ecbc8b775a277887714ab to your computer and use it in GitHub Desktop.
Factorization in Prime Numbers
public class PrimeFactorTest
{
[Property]
bool boolean_factorization_in_prime_numbers(PositiveInt positiveNumber)
{
var n = positiveNumber.Item;
var factors = factorize(n);
return factors.AreAllPrime() && factors.Multiplied() == n;
}
}
internal static class PrimeNumbersExtensions
{
internal static bool IsPrime(this int n) =>
Enumerable.Range(1, n)
.Where(i => !(i == 1 || i == n))
.All(i => n.CannotBeDividedBy(i));
private static bool CannotBeDividedBy(this int n, int i) =>
n % i != 0;
internal static bool AreAllPrime(this IEnumerable<int> xs) =>
xs.All(IsPrime);
internal static int Multiplied(this IEnumerable<int> xs) =>
xs.Aggregate(1, (product, i) => product * i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment