我想确定 ASCII 字符串的 8 位模 256 校验和。我知道公式是:
checksum = (sum of bytes)%256
我怎样才能在Python中做到这一点(操作字节)?如果我以字符串“1c03e8”开头,我应该输出0x94. 主要问题是我不确定如何找到 ASCII 字符串的字节总和。这是我正在寻找的主要想法:
https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/
它有 CheckSum8 Modulo 256
我努力了:
component = ('1c03e8')
for i in range(len(component)):
checksum.append(int(float(component[i].encode("hex"))))
print checksum
print hex(int(sum(checksum)%256))
Run Code Online (Sandbox Code Playgroud)
这虽然给了我 0x52
我必须执行(a * b) % m, 但是a, b,和 m是 128 位无符号类型,乘法期间溢出的可能性很大。我怎样才能得到正确的答案(可能使用%更多)?
我正在尝试在 Rust 中实现模指数函数,其中最大的内置类型是u128(这是我可以使用的最大值)。这三个变量都非常大,因此(a * b) > 2^128很容易。我可以用来a.overflowing_mul(b)检测是否发生溢出,但我不知道如何从溢出的结果(可以认为是(a * b) % 2^128)返回得到(a * b) % m。
我的模块化指数代码如下所示(当前未添加溢出支持):
fn mod_exp(b: u128, e: u128, m: u128) {
(0..e).fold(1, |x, _| (x * b) % m)
// ^^^^^^^^^^^
}
Run Code Online (Sandbox Code Playgroud)
从数学角度来说:
fn mod_exp(b: u128, e: u128, m: u128) {
(0..e).fold(1, |x, _| (x * b) % …Run Code Online (Sandbox Code Playgroud) 我的代码的瓶颈是对非常大的整数重复调用 pow(base,exponent,modulus)(numpy 不支持这么大的整数,大约 100 到 256 位)。但是,我的指数和模数始终相同。我可以以某种方式利用它来通过自定义函数加速计算吗?我尝试定义一个函数,如下所示(下面的函数用于一般模数和指数)。
然而,即使我在没有 while 循环和 if 语句的情况下对固定指数和模数的每个操作进行硬编码,它也比 pow 慢。
def modular_pow(self, base, exponent, modulus):
result = 1
base = base % modulus
while exponent > 0:
if (exponent % 2 == 1):
result = (result * base) % modulus
exponent = exponent >> 1
base = (base * base) % modulus
return result
Run Code Online (Sandbox Code Playgroud)
另一种选择是如果我能以某种方式“矢量化”它。我必须计算大约 100000000 个不同基值的 pow。虽然这些值在我的脚本运行之间经常发生变化(因此查找表没有用),但我在运行时就会知道这些值(我可以立即计算它们)。
有任何想法吗?我通过使用 gmpy2 中的 mpz 数据类型获得了一些加速,但它仍然太慢。
实现
oddPairs :: [Int] -> [Int] -> [(Int, Int)]返回对列表的函数,但前提是参数列表各自元素的总和为奇数。例如:
oddPairs [1,2,3] [2,2,2] == [(1,2),(3,2)]
oddPairs [1,3,5] [2,4,6] == zip [1,3,5] [2,4,6]
oddPairs [1,2,3] [1,2,3] == []
到目前为止,我已经尝试过
oddPairs (x:xs) (y:ys) | (x+y) `mod` 2 == 0 = []
| (x+y) `mod` 2 /= 0 = [(x, y)] ++ oddPairs (xs) (ys)
Run Code Online (Sandbox Code Playgroud)
在第一个示例中,它仅返回[(1,2)],在第二个示例中,它返回正确的值,但有Non-exhaustive patterns错误。
我正在开发一些在矩阵系数类型上模板化的线性代数代码.其中一种可能的类型是进行模运算的类,天真地实现如下:
template<typename val_t> // `val_t` is an integer type
class Modular
{
val_t val_;
static val_t modulus_;
public:
Modular(const val_t& value) : val_(value) { };
static void global_set_modulus(const val_t& modulus) { modulus_ = modulus; };
Modular<val_t>& operator=(const Modular<val_t>& other) { val_ = other.val_; return *this; }
Modular<val_t>& operator+=(const Modular<val_t>& other) { val_ += other.val_; val_ %= modulus_; return *this; }
Modular<val_t>& operator-=(const Modular<val_t>& other) { val_ -= other.val_; val_ %= modulus_; return *this; }
Modular<val_t>& operator*=(const Modular<val_t>& other) { val_ …Run Code Online (Sandbox Code Playgroud) 所以我创建了一个程序,使用模块在Ruby中进行模数除法:
module Moddiv
def Moddiv.testfor(op1, op2)
return op1 % op2
end
end
Run Code Online (Sandbox Code Playgroud)
程序:
require 'mdivmod'
print("Enter the first number: ")
gets
chomp
firstnum = $_
print("Enter the second number: ")
gets
chomp
puts
secondnum = $_
puts "The remainder of 70/6 is " + Moddiv.testfor(firstnum,secondnum).to_s
Run Code Online (Sandbox Code Playgroud)
当我使用两个数字运行它时,比如70和6,我得到70作为输出!为什么会这样?
哪个操作需要更多CPU时钟,modulo或者comparison?
这段代码会花费更多时间:
for(j = i; j <= 10; j++)
{
if(j == 10) printf("0");
else printf("%d", j);
}
Run Code Online (Sandbox Code Playgroud)
或这个
for(j = i; j <= 10; j++)
printf("%d", j % 10);
Run Code Online (Sandbox Code Playgroud)
为什么?
我希望我没有提出一个愚蠢的问题但是,我找不到任何关于这个结果的好解释:
35 % 36 等于 35
https://www.google.com.ph/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=35%20%25%2036
但是,如果我将这两个数字分开,35 / 36结果就是:0.97222222222我假设余数是97.
有谁能解释一下?
问题:
我希望所有1-50的数字都不能被7整除,但也没有7中的数字,比如17,27等.下面的代码可以工作但(i-10)%7必须从头开始i=6.现在它认为数字3不计算原因(3-10)=-7是0 mod 7.如何在if语句中解决这个问题?
for(int i=1; i<=50;i++){
if(i%7!=0 && (i-10)%7!=0){
System.out.println(i);
Run Code Online (Sandbox Code Playgroud) 我不懂语法:
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
Run Code Online (Sandbox Code Playgroud)
(mQuestionBank是一个数组).
是什么原因% mQuestionBank.length?