From 027f6f23e187818d5770ba21bec4aee21ca096ed Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 14 Nov 2024 16:48:22 +0000 Subject: [PATCH] refactor!: replace T.MultiplicativePersistence with generic math --- CHANGELOG.md | 1 + X10D/src/Math/BigIntegerExtensions.cs | 46 ---------------------- X10D/src/Math/BinaryIntegerExtensions.cs | 50 ++++++++++++++++++++++++ X10D/src/Math/ByteExtensions.cs | 17 -------- X10D/src/Math/Int16Extensions.cs | 15 ------- X10D/src/Math/Int32Extensions.cs | 15 ------- X10D/src/Math/Int64Extensions.cs | 46 ---------------------- X10D/src/Math/SByteExtensions.cs | 15 ------- X10D/src/Math/UInt16Extensions.cs | 15 ------- X10D/src/Math/UInt32Extensions.cs | 15 ------- X10D/src/Math/UInt64Extensions.cs | 46 ---------------------- 11 files changed, 51 insertions(+), 230 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ada6f54..0cf7ea8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - X10D: Removed `IEnumerable.GreatestCommonFactor` for all integer types in favour of generic math. - X10D: Removed `IEnumerable.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.Product` for all integer types in favour of generic math. - X10D: Removed `IEnumerable.RangeTo` for all integer types in favour of generic math. - X10D: Removed `T.Saturate` for all floating-point types in favour of generic math. diff --git a/X10D/src/Math/BigIntegerExtensions.cs b/X10D/src/Math/BigIntegerExtensions.cs index 55102e6..bb408b4 100644 --- a/X10D/src/Math/BigIntegerExtensions.cs +++ b/X10D/src/Math/BigIntegerExtensions.cs @@ -90,50 +90,4 @@ public static class BigIntegerExtensions return true; } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [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; - } } diff --git a/X10D/src/Math/BinaryIntegerExtensions.cs b/X10D/src/Math/BinaryIntegerExtensions.cs index 08d76fd..47f9ff1 100644 --- a/X10D/src/Math/BinaryIntegerExtensions.cs +++ b/X10D/src/Math/BinaryIntegerExtensions.cs @@ -77,4 +77,54 @@ public static class BinaryIntegerExtensions return result; } + + /// + /// Returns the multiplicative persistence of the current integer. + /// + /// The value whose multiplicative persistence to calculate. + /// The multiplicative persistence. + /// + /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. + /// + [Pure] + [MethodImpl(CompilerResources.MaxOptimization)] + public static int MultiplicativePersistence(this TInteger value) + where TInteger : IBinaryInteger + { + 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; + } } diff --git a/X10D/src/Math/ByteExtensions.cs b/X10D/src/Math/ByteExtensions.cs index a813c0a..1c289ba 100644 --- a/X10D/src/Math/ByteExtensions.cs +++ b/X10D/src/Math/ByteExtensions.cs @@ -1,6 +1,4 @@ using System.Diagnostics.Contracts; -using System.Runtime.CompilerServices; -using X10D.CompilerServices; namespace X10D.Math; @@ -21,19 +19,4 @@ public static class ByteExtensions { return ((long)value).IsPrime(); } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [Pure] - [MethodImpl(CompilerResources.MaxOptimization)] - public static int MultiplicativePersistence(this byte value) - { - return ((long)value).MultiplicativePersistence(); - } } diff --git a/X10D/src/Math/Int16Extensions.cs b/X10D/src/Math/Int16Extensions.cs index 626403f..204de0c 100644 --- a/X10D/src/Math/Int16Extensions.cs +++ b/X10D/src/Math/Int16Extensions.cs @@ -22,19 +22,4 @@ public static class Int16Extensions { return ((long)value).IsPrime(); } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [Pure] - [MethodImpl(CompilerResources.MaxOptimization)] - public static int MultiplicativePersistence(this short value) - { - return ((long)value).MultiplicativePersistence(); - } } diff --git a/X10D/src/Math/Int32Extensions.cs b/X10D/src/Math/Int32Extensions.cs index 6ed9af9..f6d646b 100644 --- a/X10D/src/Math/Int32Extensions.cs +++ b/X10D/src/Math/Int32Extensions.cs @@ -22,19 +22,4 @@ public static class Int32Extensions { return ((long)value).IsPrime(); } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [Pure] - [MethodImpl(CompilerResources.MaxOptimization)] - public static int MultiplicativePersistence(this int value) - { - return ((long)value).MultiplicativePersistence(); - } } diff --git a/X10D/src/Math/Int64Extensions.cs b/X10D/src/Math/Int64Extensions.cs index 7dd7ec2..e4fb474 100644 --- a/X10D/src/Math/Int64Extensions.cs +++ b/X10D/src/Math/Int64Extensions.cs @@ -41,50 +41,4 @@ public static class Int64Extensions return true; } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [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; - } } diff --git a/X10D/src/Math/SByteExtensions.cs b/X10D/src/Math/SByteExtensions.cs index 53a29d6..25c0454 100644 --- a/X10D/src/Math/SByteExtensions.cs +++ b/X10D/src/Math/SByteExtensions.cs @@ -24,21 +24,6 @@ public static class SByteExtensions return ((long)value).IsPrime(); } - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [Pure] - [MethodImpl(CompilerResources.MaxOptimization)] - public static int MultiplicativePersistence(this sbyte value) - { - return ((long)value).MultiplicativePersistence(); - } - /// /// Returns an integer that indicates the sign of this 8-bit signed integer. /// diff --git a/X10D/src/Math/UInt16Extensions.cs b/X10D/src/Math/UInt16Extensions.cs index fd46029..40b7ae7 100644 --- a/X10D/src/Math/UInt16Extensions.cs +++ b/X10D/src/Math/UInt16Extensions.cs @@ -23,19 +23,4 @@ public static class UInt16Extensions { return ((ulong)value).IsPrime(); } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [Pure] - [MethodImpl(CompilerResources.MaxOptimization)] - public static int MultiplicativePersistence(this ushort value) - { - return ((ulong)value).MultiplicativePersistence(); - } } diff --git a/X10D/src/Math/UInt32Extensions.cs b/X10D/src/Math/UInt32Extensions.cs index 2c2d73d..0549f8c 100644 --- a/X10D/src/Math/UInt32Extensions.cs +++ b/X10D/src/Math/UInt32Extensions.cs @@ -23,19 +23,4 @@ public static class UInt32Extensions { return ((ulong)value).IsPrime(); } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [Pure] - [MethodImpl(CompilerResources.MaxOptimization)] - public static int MultiplicativePersistence(this uint value) - { - return ((ulong)value).MultiplicativePersistence(); - } } diff --git a/X10D/src/Math/UInt64Extensions.cs b/X10D/src/Math/UInt64Extensions.cs index b6ef392..3ad4528 100644 --- a/X10D/src/Math/UInt64Extensions.cs +++ b/X10D/src/Math/UInt64Extensions.cs @@ -42,50 +42,4 @@ public static class UInt64Extensions return true; } - - /// - /// Returns the multiplicative persistence of a specified value. - /// - /// The value whose multiplicative persistence to calculate. - /// The multiplicative persistence. - /// - /// Multiplicative persistence is defined as the recursive digital product until that product is a single digit. - /// - [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; - } }