将如下代码转成C#语言代码import numpy as np def get_random(i, j=None): if j == None: # 返回0-i的随机整数 return np.random.randint(i + 1) if i > j: i, j = j, i # 获取i-j的随机整数 return np.random.randint(i, j + 1) def fast_power(base, power, n): result = 1 tmp = base while power > 0: if power & 1 == 1: result = (result * tmp) % n tmp = (tmp * tmp) % n power = power >> 1 return result def Miller_Rabin(n, s): # 2是素数 if n == 2: return True # n是偶数或小于2 if n & 1 == 0 or n < 2: return False # n-1 = (2^s) m m, p = n - 1, 0 while m & 1 == 0: m = m >> 1 p += 1 for _ in range(s): b = fast_power(get_random(2, n - 1), m, n) if b == 1 or b == n - 1: continue for __ in range(p - 1): b = fast_power(b, 2, n) if b == n - 1: break else: return False return True if name == 'main': num = 50000 s = 3 prime = [x for x in range(2, num) if not [y for y in range(2, int(np.sqrt(x) + 1)) if x % y == 0]] result = [] for i in range(num): flag = Miller_Rabin(i, s) if flag and i not in prime: print('error1: %d' % i) elif not flag and i in prime: print('error2: %d' % i)
时间: 2024-04-25 12:26:35 浏览: 180
以下是将代码转成C#语言代码的结果:
using System;
namespace MillerRabin
{
class Program
{
static void Main(string[] args)
{
int num = 50000;
int s = 3;
int[] prime = new int[num];
int k = 0;
for (int i = 2; i < num; i++)
{
bool isPrime = true;
for (int j = 2; j <= Math.Sqrt(i); j++)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
prime[k++] = i;
}
}
int[] result = new int[num];
for (int i = 0; i < num; i++)
{
bool flag = Miller_Rabin(i, s);
if (flag && !prime.Contains(i))
{
Console.WriteLine("error1: " + i);
}
else if (!flag && prime.Contains(i))
{
Console.WriteLine("error2: " + i);
}
}
}
static int get_random(int i, int? j = null)
{
if (j == null)
{
// 返回0-i的随机整数
Random random = new Random();
return random.Next(i + 1);
}
if (i > j)
{
int temp = i;
i = (int)j;
j = temp;
}
// 获取i-j的随机整数
Random rand = new Random();
return rand.Next(i, (int)j + 1);
}
static int fast_power(int baseNum, int power, int n)
{
int result = 1;
int tmp = baseNum;
while (power > 0)
{
if ((power & 1) == 1)
{
result = (result * tmp) % n;
}
tmp = (tmp * tmp) % n;
power = power >> 1;
}
return result;
}
static bool Miller_Rabin(int n, int s)
{
// 2是素数
if (n == 2) return true;
// n是偶数或小于2
if ((n & 1) == 0 || n < 2) return false;
// n-1 = (2^s) m
int m = n - 1, p = 0;
while ((m & 1) == 0)
{
m = m >> 1;
p++;
}
for (int i = 0; i < s; i++)
{
int b = fast_power(get_random(2, n - 1), m, n);
if (b == 1 || b == n - 1)
{
continue;
}
for (int j = 0; j < p - 1; j++)
{
b = fast_power(b, 2, n);
if (b == n - 1)
{
break;
}
}
if (b != n - 1)
{
return false;
}
}
return true;
}
}
}
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)