mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-25 02:58:47 +00:00
refactor!: replace T.MultiplicativePersistence with generic math
This commit is contained in:
parent
7af3c96405
commit
027f6f23e1
@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
- X10D: Removed `IEnumerable<T>.GreatestCommonFactor` for all integer types in favour of generic math.
|
- X10D: Removed `IEnumerable<T>.GreatestCommonFactor` for all integer types in favour of generic math.
|
||||||
- X10D: Removed `IEnumerable<T>.LowestCommonMultiple` for all integer types in favour of generic math.
|
- X10D: Removed `IEnumerable<T>.LowestCommonMultiple` for all integer types in favour of generic math.
|
||||||
|
- X10D: Removed `T.MultiplicativePersistence` for all integer types in favour of generic math.
|
||||||
- X10D: Removed `IEnumerable<T>.Product` for all integer types in favour of generic math.
|
- X10D: Removed `IEnumerable<T>.Product` for all integer types in favour of generic math.
|
||||||
- X10D: Removed `IEnumerable<T>.RangeTo` for all integer types in favour of generic math.
|
- X10D: Removed `IEnumerable<T>.RangeTo` for all integer types in favour of generic math.
|
||||||
- X10D: Removed `T.Saturate` for all floating-point types in favour of generic math.
|
- X10D: Removed `T.Saturate` for all floating-point types in favour of generic math.
|
||||||
|
@ -90,50 +90,4 @@ public static class BigIntegerExtensions
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this BigInteger value)
|
|
||||||
{
|
|
||||||
var persistence = 0;
|
|
||||||
BigInteger product = BigInteger.Abs(value);
|
|
||||||
|
|
||||||
while (product > 9)
|
|
||||||
{
|
|
||||||
if (value % 10 == 0)
|
|
||||||
{
|
|
||||||
return persistence + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (value > 9)
|
|
||||||
{
|
|
||||||
value /= 10;
|
|
||||||
if (value % 10 == 0)
|
|
||||||
{
|
|
||||||
return persistence + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BigInteger newProduct = 1;
|
|
||||||
BigInteger currentProduct = product;
|
|
||||||
while (currentProduct > 0)
|
|
||||||
{
|
|
||||||
newProduct *= currentProduct % 10;
|
|
||||||
currentProduct /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
product = newProduct;
|
|
||||||
persistence++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return persistence;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -77,4 +77,54 @@ public static class BinaryIntegerExtensions
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the multiplicative persistence of the current integer.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
||||||
|
/// <returns>The multiplicative persistence.</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
||||||
|
/// </remarks>
|
||||||
|
[Pure]
|
||||||
|
[MethodImpl(CompilerResources.MaxOptimization)]
|
||||||
|
public static int MultiplicativePersistence<TInteger>(this TInteger value)
|
||||||
|
where TInteger : IBinaryInteger<TInteger>
|
||||||
|
{
|
||||||
|
var nine = TInteger.CreateChecked(9);
|
||||||
|
var ten = TInteger.CreateChecked(10);
|
||||||
|
|
||||||
|
var persistence = 0;
|
||||||
|
TInteger product = TInteger.Abs(value);
|
||||||
|
|
||||||
|
while (product > nine)
|
||||||
|
{
|
||||||
|
if (value % ten == TInteger.Zero)
|
||||||
|
{
|
||||||
|
return persistence + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (value > nine)
|
||||||
|
{
|
||||||
|
value /= ten;
|
||||||
|
if (value % ten == TInteger.Zero)
|
||||||
|
{
|
||||||
|
return persistence + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TInteger newProduct = TInteger.One;
|
||||||
|
TInteger currentProduct = product;
|
||||||
|
while (currentProduct > TInteger.Zero)
|
||||||
|
{
|
||||||
|
newProduct *= currentProduct % ten;
|
||||||
|
currentProduct /= ten;
|
||||||
|
}
|
||||||
|
|
||||||
|
product = newProduct;
|
||||||
|
persistence++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return persistence;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
using System.Diagnostics.Contracts;
|
using System.Diagnostics.Contracts;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using X10D.CompilerServices;
|
|
||||||
|
|
||||||
namespace X10D.Math;
|
namespace X10D.Math;
|
||||||
|
|
||||||
@ -21,19 +19,4 @@ public static class ByteExtensions
|
|||||||
{
|
{
|
||||||
return ((long)value).IsPrime();
|
return ((long)value).IsPrime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this byte value)
|
|
||||||
{
|
|
||||||
return ((long)value).MultiplicativePersistence();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -22,19 +22,4 @@ public static class Int16Extensions
|
|||||||
{
|
{
|
||||||
return ((long)value).IsPrime();
|
return ((long)value).IsPrime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this short value)
|
|
||||||
{
|
|
||||||
return ((long)value).MultiplicativePersistence();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -22,19 +22,4 @@ public static class Int32Extensions
|
|||||||
{
|
{
|
||||||
return ((long)value).IsPrime();
|
return ((long)value).IsPrime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this int value)
|
|
||||||
{
|
|
||||||
return ((long)value).MultiplicativePersistence();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -41,50 +41,4 @@ public static class Int64Extensions
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this long value)
|
|
||||||
{
|
|
||||||
var persistence = 0;
|
|
||||||
long product = System.Math.Abs(value);
|
|
||||||
|
|
||||||
while (product > 9)
|
|
||||||
{
|
|
||||||
if (value % 10 == 0)
|
|
||||||
{
|
|
||||||
return persistence + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (value > 9)
|
|
||||||
{
|
|
||||||
value /= 10;
|
|
||||||
if (value % 10 == 0)
|
|
||||||
{
|
|
||||||
return persistence + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long newProduct = 1;
|
|
||||||
long currentProduct = product;
|
|
||||||
while (currentProduct > 0)
|
|
||||||
{
|
|
||||||
newProduct *= currentProduct % 10;
|
|
||||||
currentProduct /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
product = newProduct;
|
|
||||||
persistence++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return persistence;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -24,21 +24,6 @@ public static class SByteExtensions
|
|||||||
return ((long)value).IsPrime();
|
return ((long)value).IsPrime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this sbyte value)
|
|
||||||
{
|
|
||||||
return ((long)value).MultiplicativePersistence();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns an integer that indicates the sign of this 8-bit signed integer.
|
/// Returns an integer that indicates the sign of this 8-bit signed integer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -23,19 +23,4 @@ public static class UInt16Extensions
|
|||||||
{
|
{
|
||||||
return ((ulong)value).IsPrime();
|
return ((ulong)value).IsPrime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this ushort value)
|
|
||||||
{
|
|
||||||
return ((ulong)value).MultiplicativePersistence();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -23,19 +23,4 @@ public static class UInt32Extensions
|
|||||||
{
|
{
|
||||||
return ((ulong)value).IsPrime();
|
return ((ulong)value).IsPrime();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this uint value)
|
|
||||||
{
|
|
||||||
return ((ulong)value).MultiplicativePersistence();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -42,50 +42,4 @@ public static class UInt64Extensions
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the multiplicative persistence of a specified value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value whose multiplicative persistence to calculate.</param>
|
|
||||||
/// <returns>The multiplicative persistence.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
|
|
||||||
/// </remarks>
|
|
||||||
[Pure]
|
|
||||||
[MethodImpl(CompilerResources.MaxOptimization)]
|
|
||||||
public static int MultiplicativePersistence(this ulong value)
|
|
||||||
{
|
|
||||||
var persistence = 0;
|
|
||||||
ulong product = value;
|
|
||||||
|
|
||||||
while (product > 9)
|
|
||||||
{
|
|
||||||
if (value % 10 == 0)
|
|
||||||
{
|
|
||||||
return persistence + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (value > 9)
|
|
||||||
{
|
|
||||||
value /= 10;
|
|
||||||
if (value % 10 == 0)
|
|
||||||
{
|
|
||||||
return persistence + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ulong newProduct = 1;
|
|
||||||
ulong currentProduct = product;
|
|
||||||
while (currentProduct > 0)
|
|
||||||
{
|
|
||||||
newProduct *= currentProduct % 10;
|
|
||||||
currentProduct /= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
product = newProduct;
|
|
||||||
persistence++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return persistence;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user