- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个Miller-Rabin primality test基于以下伪代码:
Input: n > 2, an odd integer to be tested for primality;
k, a parameter that determines the accuracy of the test
Output: composite if n is composite, otherwise probably prime
write n − 1 as 2s·d with d odd by factoring powers of 2 from n − 1
LOOP: repeat k times:
pick a randomly in the range [2, n − 1]
x ← ad mod n
if x = 1 or x = n − 1 then do next LOOP
for r = 1 .. s − 1
x ← x2 mod n
if x = 1 then return composite
if x = n − 1 then do next LOOP
return composite
return probably prime
我的代码很少超过 31(如果我把它放在一个循环中以测试从 2 到 100 的数字)。一定有问题,但我看不出是什么。
bool isProbablePrime(ulong n, int k) {
if (n < 2 || n % 2 == 0)
return n == 2;
ulong d = n - 1;
ulong s = 0;
while (d % 2 == 0) {
d /= 2;
s++;
}
assert(2 ^^ s * d == n - 1);
outer:
foreach (_; 0 .. k) {
ulong a = uniform(2, n);
ulong x = (a ^^ d) % n;
if (x == 1 || x == n - 1)
continue;
foreach (__; 1 .. s) {
x = (x ^^ 2) % n;
if (x == 1) return false;
if (x == n - 1) continue outer;
}
return false;
}
return true;
}
我也试过这个变体
...
foreach (__; 1 .. s) {
x = (x ^^ 2) % n;
if (x == 1) return false;
if (x == n - 1) continue outer;
}
if ( x != n - 1) return false; // this is different
...
我有一个可以正常工作的不同版本的测试,但它使用了 modpow。我想要一个更接近于属于 rossetta.org task description 的伪代码的版本.
编辑:回复:溢出问题。我怀疑是这样的。我仍然很困惑为什么 Ruby 版本没有那个问题。它可能在引擎盖下以不同的方式处理它。如果我使用 BigInt,代码确实可以工作,但比我使用 modpow 时要慢很多。所以我想我无法摆脱它。很遗憾 Phobos 没有内置 modpow,或者我一定是忽略了它。
ulong x = ((BigInt(a) ^^ d) % BigInt(n)).toLong();
最佳答案
在这个声明中
ulong x = (a ^^ d) % n;
数量 (a ^^ d)
可能在 mod 操作发生之前溢出。 modpow 版本不会遇到这个问题,因为该算法避免了对任意大的中间值的需要。
关于d - 米勒-拉宾测试 : bug in my code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6184550/
作为内部研究项目的一部分,我们正在尝试从 Bugzilla 数据库中收集一些指标;我们已经找到了一个工具来帮助我们从中收集一些指标 ( BugzillaMetrics ) 但我们现在问自己应该收集哪些
我想向 Mantis Bug 跟踪器添加一个名为“On Hold”的自定义状态。根据我在网上找到的一些博客,我在 config_local.php 中添加了以下几行: $g_status_enum_s
我安装了Mantis BT,并在成功安装后重定向到登录页面。它说默认用户是“管理员”,但没有提供有关其密码的提示。 Documentation也未指定默认密码。有什么线索吗? 最佳答案 Aah ..明
我遇到了一些复杂的C++模板代码的编译错误,将其简化如下: struct MyOptions { static const size_t maxArray = 2; static co
我试图编译一些使用 Boost (1.49) 的代码,使用 trunk 中的 Clang(& libc++)。有问题的代码归结为以下内容: #include #include int main()
我在这里设置了一个 CSS 菜单: http://pans.saebermedia.com/ 问题是,我注意到 IE 无法正确显示菜单,现在我又开始使用装有 IE8 的计算机来修复它。我正在使用兼容模
我检查了 GCC buglist和 Clang buglist并且还没有看到任何相关内容。 This Wandbox link显示一些 C++11/C++14 代码为各种类型的 x 执行 declty
所以我有这个简洁的小 javascript 函数,我用它以很酷的命令提示符样式将文本打印到浏览器窗口。它接受一个字符串并以设定的时间间隔一次将一个字符打印到窗口。这是:(我已经删除了所有不必要的部分,
在我的 Galaxy Tab 2(三星)上启动我的应用程序时,我不断收到此错误消息。我正在开发的应用程序非常复杂,很难追踪此错误的来源。所以我开始逐个剥离我的应用程序,最后只剩下一个 map View
我正在使用 Workbench 在 Mathematica 中开发一个应用程序包,它开始变得复杂,包含多个版本和组件。我在一些 mathematica 文档(http://reference.wolf
我选择了四种免费工具用于基于 Web 的缺陷跟踪。它们是: 错误说明 螳螂 错误跟踪器 .NET 来自 Project Locker 软件的 Trac 考虑到以下因素,我正在尝试在这四种工具之间做出决
我正在尝试将 mysqlclient 安装到我的 Python 3.6 上。最初我想安装的是 MySQLdb,但是它说 MySQLdb 不能与 Python 3 一起使用(还是?)。所以我切换到mys
我正在尝试将 mysqlclient 安装到我的 Python 3.6。最初我要安装的是 MySQLdb,但是它说 MySQLdb 不能与 Python 3 一起使用(仍然?)。所以我切换到mysql
我正在尝试将 mysqlclient 安装到我的 Python 3.6。最初我要安装的是 MySQLdb,但是它说 MySQLdb 不能与 Python 3 一起使用(仍然?)。所以我切换到mysql
**摘要:**为了保障华为云GaussDB产品的可靠性,每一款产品发布前都要通过多轮严苛的测试用例。 说明:本文中的MySQL,如果不做特殊说明,指的是开源社区版MySQL。 华为云数据库新版本在发布
我有一个程序,它打开一个程序,然后将一个字符串复制并粘贴到该程序中,一段时间后,它使用带有 ctrl+c 的机器人将一个字符串从程序复制到剪贴板。然后我的程序检查复制的字符串是否包含单词,但不是检查最
所以我尝试为 cocos2d-x 构建项目。我目前在 cmd 中,当我键入 python android-build.py -p 19 cpp-tests 时,它开始制作项目,但随后出现构建失败的错误
我是开发物理模型的团队中唯一的软件工程师(大约 30,000 行代码)。团队的其他成员由开发代码库大约 20 年的科学家组成。我的工作流程是这样的: 科学家请求新功能 我实现 通过测试和验证,我在数字
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 2年前关闭。 Improve this
当我运行 时,以下代码抛出 ExecutionEngineException发布 构建可执行文件(启动 exe 文件 )。这是错误还是正常行为? 包大小 = 1 的值类型: [StructLayout
我是一名优秀的程序员,十分优秀!