我根据以下伪代码编写了一个Miller-Rabin素性测试:
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 ? …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么我在第一种情况下得到肯定的结果而在第二种情况下得到负面结果.
auto r1 = -3.0L;
auto r2 = 2.0L;
writeln(typeid(r1)); // real
writeln(typeid(r2)); // real
writeln(typeid(r1 ^^ r2)); // real
writeln(r1 ^^ r2); // 9
writeln(typeid(-3.0L)); // real
writeln(typeid(2.0L)); // real
writeln(typeid(-3.0L ^^ 2.0L)); // real
writeln(-3.0L ^^ 2.0L); // -9
Run Code Online (Sandbox Code Playgroud) 假设我有一个LongStream范围,我想将长值放在一个映射中作为键,一个函数结果作为值.
例如:
Map<Long, Long> m = LongStream.range(1, 20) ...
long someFunction(long n) {
return n * n;
}
Run Code Online (Sandbox Code Playgroud)
然后,地图应包含映射到这些值的平方的1到20.我看过收集和收藏家,但我似乎找不到合适的解决方案.
编辑:我有以下工作.
Map<Long, Long> map = LongStream
.range(1, 20)
.boxed()
.collect(toMap(identity(), AmicablePairs::properDivsSum));
Run Code Online (Sandbox Code Playgroud)
除了没有调用盒装之外,我还得到了函数的调用符号错误.我当然不能使用双冒号,因为我需要传递一个参数.
为什么不允许在std.datetime中添加日期到日期?您可以添加月份和年份,但不能添加天数.
最近我必须计算复活节星期日的日期,然后我必须通过在最后一天添加一定天数(39,10,7,4)来计算相关假期(阿森松岛,五旬节,三位一体,语料库).
我最终使用dayOfYear:
date.dayOfYear(date.dayOfYear + offset);
Run Code Online (Sandbox Code Playgroud)
这很好,但只是因为我在同一年内.如果我要在50年代增加50天怎么办?
有一种简单的方法可以做到这一点,我忽略了吗?