Skip to content

Commit 742be47

Browse files
committed
Use bitwise opeartions
1 parent 65dd629 commit 742be47

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

primalityTest/millerRabinMethod/MillerRabinMethod.cs

+17-15
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private static BigInteger RandomInRangeFromZeroToPositive(BigInteger max)
9393
/// Miller-Rabin primality test
9494
/// </summary>
9595
/// <remarks>
96-
/// Test if <paramref name="number"/> could be prime using the Miller-Rabin Primality Test with <paramref name="rounds"/> rounds.
96+
/// Test if <paramref name="number"/> could be prime using the Miller-Rabin primality test with <paramref name="rounds"/> rounds.
9797
/// A return value of false means <paramref name="number"/> is definitely composite, while true means it is probably prime.
9898
/// The higher <paramref name="rounds"/> is, the more accurate the test is.
9999
/// </remarks>
@@ -111,9 +111,9 @@ public static bool MillerRabin(BigInteger number, int rounds = 40)
111111
// Factor out the powers of 2 from {number - 1} and save the result
112112
BigInteger d = number - 1;
113113
int r = 0;
114-
while (d % 2 == 0)
114+
while (d.IsEven)
115115
{
116-
d /= 2;
116+
d >>= 1;
117117
++r;
118118
}
119119

@@ -138,24 +138,26 @@ public static bool MillerRabin(BigInteger number, int rounds = 40)
138138
}
139139
return true;
140140
}
141-
}
142141

143-
public static class MillerRabinMethod
144-
{
145-
static void Main(string[] args)
142+
143+
public static class MillerRabinMethod
146144
{
147-
int count = 0;
148-
int upper_bound = 1000;
149-
Console.WriteLine($"Prime numbers lower than {upper_bound}:");
150-
for (int i = 1; i < upper_bound; ++i)
145+
static void Main(string[] args)
151146
{
152-
if (PrimalityTest.MillerRabin(i))
147+
int count = 0;
148+
int upper_bound = 1000;
149+
Console.WriteLine($"Prime numbers lower than {upper_bound}:");
150+
for (int i = 1; i < upper_bound; ++i)
153151
{
154-
Console.WriteLine($"\t{i}");
155-
++count;
152+
if (PrimalityTest.MillerRabin(i))
153+
{
154+
Console.WriteLine($"\t{i}");
155+
++count;
156+
}
156157
}
158+
Console.WriteLine($"Total: {count}");
157159
}
158-
Console.WriteLine($"Total: {count}");
159160
}
160161
}
162+
161163
}

primalityTest/millerRabinMethod/MillerRabinMethod.java

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ class MillerRabinMethod {
66
/**
77
*
88
* Generate a random BigInteger between {@code min} and {@code max}, inclusive.
9-
* Random BigInteger implementation from
10-
* <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow
11-
* answer</a> Uses an offset on the
12-
* {@link #RandomInRangeFromZeroToPositive(BigInteger)} function to return a
13-
* random BigInteger in a range.
9+
* Random BigInteger implementation from <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow answer</a>
10+
* Uses an offset on the {@link #RandomInRangeFromZeroToPositive(BigInteger)}
11+
* function to return a random BigInteger in a range.
1412
*
1513
* @param min lower bound
1614
* @param max upper bound
@@ -36,9 +34,8 @@ public static BigInteger RandomInRange(BigInteger min, BigInteger max) {
3634
/**
3735
*
3836
* Generate a random BigInteger smaller or equal to {@code max}. Random
39-
* BigInteger implementation from
40-
* <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow
41-
* answer</a> Returns a random BigInteger lower than {@code max} derived from a
37+
* BigInteger implementation from <a href="https://stackoverflow.com/a/48855115/12771343">this StackOverflow* answer</a>
38+
* Returns a random BigInteger lower than {@code max} derived from a
4239
* random byte array" />
4340
*
4441
* @param max upper bound
@@ -86,10 +83,11 @@ private static BigInteger RandomInRangeFromZeroToPositive(BigInteger max) {
8683

8784
/**
8885
*
89-
* Miller-Rabin primality test Test if {@code number} could be prime using the
90-
* Miller-Rabin Primality Test with {@code round} rounds. A return value of
91-
* false means {@code number} is definitely composite, while true means it is
92-
* probably prime. The higher {@code round} is, the more accurate the test is.
86+
* Miller-Rabin primality test
87+
* Test if {@code number} could be prime using the Miller-Rabin primality test
88+
* with {@code round} rounds. A return value of false means {@code number} is
89+
* definitely composite, while true means it is probably prime.
90+
* The higher {@code round} is, the more accurate the test is.
9391
*
9492
* @param number the number to be tested for primality
9593
* @param rounds how many rounds to use in the test
@@ -106,8 +104,8 @@ public static Boolean MillerRabin(BigInteger number, int rounds) {
106104
// Factor out the powers of 2 from {number - 1} and save the result
107105
BigInteger d = number.subtract(BigInteger.valueOf(1));
108106
int r = 0;
109-
while (d.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(0))) {
110-
d = d.divide(BigInteger.valueOf(2));
107+
while ((d.getLowestSetBit() & 1) == 0) {
108+
d = d.shiftLeft(1);
111109
++r;
112110
}
113111

primalityTest/millerRabinMethod/miller_rabin_method.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ def miller_rabin_method(number: int, rounds: int = 40) -> bool:
2424
# Factor out the powers of 2 from {number - 1} and save the result
2525
d = number - 1
2626
r = 0
27-
while d % 2 == 0:
28-
d /= 2
27+
while not d & 1:
28+
d = d >> 1
2929
r += 1
30-
d = int(d)
3130

3231
# Cycle at most {round} times
3332
for i in range(rounds + 1):

0 commit comments

Comments
 (0)