1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:15:40 +00:00

Add MultiplicativePersistence for integer types

This commit is contained in:
Oliver Booth 2022-04-30 11:29:09 +01:00
parent 3ae2827fd5
commit a7631b0efe
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
17 changed files with 296 additions and 56 deletions

View File

@ -49,4 +49,14 @@ public class ByteTests
Assert.IsTrue(one.IsOdd());
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, ((byte)0).MultiplicativePersistence());
Assert.AreEqual(1, ((byte)10).MultiplicativePersistence());
Assert.AreEqual(2, ((byte)25).MultiplicativePersistence());
Assert.AreEqual(3, ((byte)39).MultiplicativePersistence());
Assert.AreEqual(4, ((byte)77).MultiplicativePersistence());
}
}

View File

@ -50,6 +50,18 @@ public class Int16Tests
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, ((short)0).MultiplicativePersistence());
Assert.AreEqual(1, ((short)10).MultiplicativePersistence());
Assert.AreEqual(2, ((short)25).MultiplicativePersistence());
Assert.AreEqual(3, ((short)39).MultiplicativePersistence());
Assert.AreEqual(4, ((short)77).MultiplicativePersistence());
Assert.AreEqual(5, ((short)679).MultiplicativePersistence());
Assert.AreEqual(6, ((short)6788).MultiplicativePersistence());
}
[TestMethod]
public void NegativeFactorialShouldThrow()
{

View File

@ -50,6 +50,21 @@ public class Int32Tests
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, 0.MultiplicativePersistence());
Assert.AreEqual(1, 10.MultiplicativePersistence());
Assert.AreEqual(2, 25.MultiplicativePersistence());
Assert.AreEqual(3, 39.MultiplicativePersistence());
Assert.AreEqual(4, 77.MultiplicativePersistence());
Assert.AreEqual(5, 679.MultiplicativePersistence());
Assert.AreEqual(6, 6788.MultiplicativePersistence());
Assert.AreEqual(7, 68889.MultiplicativePersistence());
Assert.AreEqual(8, 2677889.MultiplicativePersistence());
Assert.AreEqual(9, 26888999.MultiplicativePersistence());
}
[TestMethod]
public void NegativeFactorialShouldThrow()
{

View File

@ -50,6 +50,23 @@ public class Int64Tests
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, 0L.MultiplicativePersistence());
Assert.AreEqual(1, 10L.MultiplicativePersistence());
Assert.AreEqual(2, 25L.MultiplicativePersistence());
Assert.AreEqual(3, 39L.MultiplicativePersistence());
Assert.AreEqual(4, 77L.MultiplicativePersistence());
Assert.AreEqual(5, 679L.MultiplicativePersistence());
Assert.AreEqual(6, 6788L.MultiplicativePersistence());
Assert.AreEqual(7, 68889L.MultiplicativePersistence());
Assert.AreEqual(8, 2677889L.MultiplicativePersistence());
Assert.AreEqual(9, 26888999L.MultiplicativePersistence());
Assert.AreEqual(10, 3778888999L.MultiplicativePersistence());
Assert.AreEqual(11, 277777788888899L.MultiplicativePersistence());
}
[TestMethod]
public void NegativeFactorialShouldThrow()
{

View File

@ -51,6 +51,16 @@ public class SByteTests
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, ((sbyte)0).MultiplicativePersistence());
Assert.AreEqual(1, ((sbyte)10).MultiplicativePersistence());
Assert.AreEqual(2, ((sbyte)25).MultiplicativePersistence());
Assert.AreEqual(3, ((sbyte)39).MultiplicativePersistence());
Assert.AreEqual(4, ((sbyte)77).MultiplicativePersistence());
}
[TestMethod]
public void NegativeFactorialShouldThrow()
{

View File

@ -50,4 +50,16 @@ public class UInt16Tests
Assert.IsTrue(one.IsOdd());
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, ((ushort)0).MultiplicativePersistence());
Assert.AreEqual(1, ((ushort)10).MultiplicativePersistence());
Assert.AreEqual(2, ((ushort)25).MultiplicativePersistence());
Assert.AreEqual(3, ((ushort)39).MultiplicativePersistence());
Assert.AreEqual(4, ((ushort)77).MultiplicativePersistence());
Assert.AreEqual(5, ((ushort)679).MultiplicativePersistence());
Assert.AreEqual(6, ((ushort)6788).MultiplicativePersistence());
}
}

View File

@ -50,4 +50,20 @@ public class UInt32Tests
Assert.IsTrue(one.IsOdd());
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, 0U.MultiplicativePersistence());
Assert.AreEqual(1, 10U.MultiplicativePersistence());
Assert.AreEqual(2, 25U.MultiplicativePersistence());
Assert.AreEqual(3, 39U.MultiplicativePersistence());
Assert.AreEqual(4, 77U.MultiplicativePersistence());
Assert.AreEqual(5, 679U.MultiplicativePersistence());
Assert.AreEqual(6, 6788U.MultiplicativePersistence());
Assert.AreEqual(7, 68889U.MultiplicativePersistence());
Assert.AreEqual(8, 2677889U.MultiplicativePersistence());
Assert.AreEqual(9, 26888999U.MultiplicativePersistence());
Assert.AreEqual(10, 3778888999U.MultiplicativePersistence());
}
}

View File

@ -54,4 +54,21 @@ public class UInt64Tests
Assert.IsTrue(one.IsOdd());
Assert.IsFalse(two.IsOdd());
}
[TestMethod]
public void MultiplicativePersistence_ShouldBeCorrect_ForRecordHolders()
{
Assert.AreEqual(0, 0UL.MultiplicativePersistence());
Assert.AreEqual(1, 10UL.MultiplicativePersistence());
Assert.AreEqual(2, 25UL.MultiplicativePersistence());
Assert.AreEqual(3, 39UL.MultiplicativePersistence());
Assert.AreEqual(4, 77UL.MultiplicativePersistence());
Assert.AreEqual(5, 679UL.MultiplicativePersistence());
Assert.AreEqual(6, 6788UL.MultiplicativePersistence());
Assert.AreEqual(7, 68889UL.MultiplicativePersistence());
Assert.AreEqual(8, 2677889UL.MultiplicativePersistence());
Assert.AreEqual(9, 26888999UL.MultiplicativePersistence());
Assert.AreEqual(10, 3778888999UL.MultiplicativePersistence());
Assert.AreEqual(11, 277777788888899UL.MultiplicativePersistence());
}
}

View File

@ -1,51 +0,0 @@
namespace X10D;
/// <summary>
/// Extension methods for <see cref="long" />.
/// </summary>
public static class Int64Extensions
{
/// <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>
public static int MultiplicativePersistence(this long value)
{
var persistence = 0;
long product = 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;
}
}

View File

@ -91,4 +91,19 @@ public static class ByteExtensions
{
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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int MultiplicativePersistence(this byte value)
{
return ((long)value).MultiplicativePersistence();
}
}

View File

@ -120,6 +120,21 @@ public static class Int16Extensions
return (short)(r < 0 ? r + divisor : r);
}
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int MultiplicativePersistence(this short value)
{
return ((long)value).MultiplicativePersistence();
}
/// <summary>
/// Returns an integer that indicates the sign of this 16-bit signed integer.
/// </summary>

View File

@ -120,6 +120,21 @@ public static class Int32Extensions
return r < 0 ? r + divisor : r;
}
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int MultiplicativePersistence(this int value)
{
return ((long)value).MultiplicativePersistence();
}
/// <summary>
/// Returns an integer that indicates the sign of this 32-bit signed integer.
/// </summary>

View File

@ -145,6 +145,52 @@ public static class Int64Extensions
return r < 0 ? r + divisor : r;
}
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
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;
}
/// <summary>
/// Returns an integer that indicates the sign of this 64-bit signed integer.
/// </summary>

View File

@ -121,6 +121,21 @@ public static class SByteExtensions
return (sbyte)(r < 0 ? r + divisor : r);
}
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int MultiplicativePersistence(this sbyte value)
{
return ((long)value).MultiplicativePersistence();
}
/// <summary>
/// Returns an integer that indicates the sign of this 8-bit signed integer.
/// </summary>

View File

@ -92,4 +92,19 @@ public static class UInt16Extensions
{
return !value.IsEven();
}
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int MultiplicativePersistence(this ushort value)
{
return ((ulong)value).MultiplicativePersistence();
}
}

View File

@ -92,4 +92,19 @@ public static class UInt32Extensions
{
return !value.IsEven();
}
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static int MultiplicativePersistence(this uint value)
{
return ((ulong)value).MultiplicativePersistence();
}
}

View File

@ -117,4 +117,50 @@ public static class UInt64Extensions
{
return !value.IsEven();
}
/// <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(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
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;
}
}