-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongExtension.cs
101 lines (79 loc) · 2.13 KB
/
LongExtension.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
using System;
using System.Collections.Generic;
namespace Aya.Extension
{
public static class LongExtension
{
#region Abs
public static long Abs(this long value)
{
var result = Math.Abs(value);
return result;
}
public static IEnumerable<long> Abs(this IEnumerable<long> value)
{
foreach (var d in value)
{
yield return d.Abs();
}
}
#endregion
#region Range
public static bool IsInRange(this long value, long minValue, long maxValue)
{
var result = value >= minValue && value <= maxValue;
return result;
}
public static double InRange(this long value, long minValue, long maxValue, long defaultValue)
{
var result = value.IsInRange(minValue, maxValue) ? value : defaultValue;
return result;
}
#endregion
#region Times
public static void Times(this long value, Action action)
{
for (var i = 0; i < value; i++)
{
action();
}
}
public static void Times(this int value, Action<long> action)
{
for (var i = 0; i < value; i++)
{
action(i);
}
}
#endregion
#region Odd / Even
public static bool IsEven(this long value)
{
var result = value % 2 == 0;
return result;
}
public static bool IsOdd(this long value)
{
var result = value % 2 != 0;
return result;
}
#endregion
#region Prime
public static long ToPrime(this long value)
{
value = Math.Max(0, value);
long result = 0;
for (long i = 2; i < long.MaxValue; i = i << 1)
{
if (i < value)
{
continue;
}
result = i;
break;
}
return result;
}
#endregion
}
}