-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UInt64Extensions.cs
276 lines (251 loc) · 8.61 KB
/
UInt64Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D.Math;
/// <summary>
/// Extension methods for <see cref="ulong" />.
/// </summary>
[CLSCompliant(false)]
public static class UInt64Extensions
{
/// <summary>
/// Computes the digital root of the current 64-bit unsigned integer.
/// </summary>
/// <param name="value">The value whose digital root to compute.</param>
/// <returns>The digital root of <paramref name="value" />.</returns>
/// <remarks>
/// <para>The digital root is defined as the recursive sum of digits until that result is a single digit.</para>
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong DigitalRoot(this ulong value)
{
ulong root = value % 9;
return root == 0 ? 9 : root;
}
/// <summary>
/// Returns the factorial of the current 64-bit unsigned integer.
/// </summary>
/// <param name="value">The value whose factorial to compute.</param>
/// <returns>The factorial of <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Factorial(this ulong value)
{
if (value == 0)
{
return 1;
}
var result = 1UL;
for (var i = 1UL; i <= value; i++)
{
result *= i;
}
return result;
}
/// <summary>
/// Calculates the greatest common factor between the current 64-bit unsigned integer, and another 64-bit unsigned
/// integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The greatest common factor between <paramref name="value" /> and <paramref name="other" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong GreatestCommonFactor(this ulong value, ulong other)
{
while (other != 0)
{
(value, other) = (other, value % other);
}
return value;
}
/// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this ulong value)
{
return (value & 1) == 0;
}
/// <summary>
/// Returns a value indicating whether the current value is a prime number.
/// </summary>
/// <param name="value">The value whose primality to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this ulong value)
{
switch (value)
{
case <= 1: return false;
case <= 3: return true;
}
if ((value & 1) == 0 || value % 3 == 0)
{
return false;
}
for (var iterator = 5UL; iterator * iterator <= value; iterator += 6)
{
if (value % iterator == 0 || value % (iterator + 2) == 0)
{
return false;
}
}
return true;
}
/// <summary>
/// Returns a value indicating whether the current value is not evenly divisible by 2.
/// </summary>
/// <param name="value">The value whose parity to check.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is not evenly divisible by 2, or <see langword="false" />
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this ulong value)
{
return !value.IsEven();
}
/// <summary>
/// Calculates the lowest common multiple between the current 64-bit unsigned integer, and another 64-bit unsigned
/// integer.
/// </summary>
/// <param name="value">The first value.</param>
/// <param name="other">The second value.</param>
/// <returns>The lowest common multiple between <paramref name="value" /> and <paramref name="other" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong LowestCommonMultiple(this ulong value, ulong other)
{
if (value == 0 || other == 0)
{
return 0;
}
if (value == 1)
{
return other;
}
if (other == 1)
{
return value;
}
return value * other / value.GreatestCommonFactor(other);
}
/// <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]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
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;
}
/// <summary>
/// Wraps the current 64-bit unsigned integer between a low and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="low">The inclusive lower bound.</param>
/// <param name="high">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Wrap(this ulong value, ulong low, ulong high)
{
ulong difference = high - low;
return low + (((value - low) % difference) + difference) % difference;
}
/// <summary>
/// Wraps the current 64-bit unsigned integer between 0 and a high value.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="length">The exclusive upper bound.</param>
/// <returns>The wrapped value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Wrap(this ulong value, ulong length)
{
return ((value % length) + length) % length;
}
}