鉴于:
double d;
int lsb;
Run Code Online (Sandbox Code Playgroud)
以下编译:
lsb = (floor(d));
lsb = lsb % 10;
Run Code Online (Sandbox Code Playgroud)
但是以下内容不是:
lsb = (floor(d)) % 10;
Run Code Online (Sandbox Code Playgroud)
IDE强调了开头floor并报告:
表达式必须具有整数或未整合的枚举类型.
所以我想写这个方法:142 ^ 23(MOD 187),并使用任何计算器我得到的结果是65,但与这段代码:
double number = Math.Pow(142, 23) % 187
我得到的53的结果为什么会这样,什么我错了在这里做?
我正在寻找一个快速的模10算法,因为我需要加速我的程序,它在循环中进行许多模运算.
我查看了这个页面,比较了一些替代方案.据我所知,T3是最快的.我的问题是,x % y看起来如何使用T3技术?
为简单起见,我在这里复制了T3技术,以防链接出现故障.
for (int x = 0; x < max; x++)
{
if (y > (threshold - 1))
{
y = 0; //reset
total += x;
}
y += 1;
}
Run Code Online (Sandbox Code Playgroud)
关于评论,如果这不比常规mod快,我寻找的模数至少比使用模数快2倍%.我看过许多使用2的功能的例子,但由于10不是,我怎么能让它工作?
编辑:
对于我的程序,假设我有2个循环,其中n=1 000 000和m=1000.
看起来像这样:
for (i = 1; i <= n; i++) {
D[(i%10)*m] = i;
for (j = 1; j <= m; j++) {
...
}
}
Run Code Online (Sandbox Code Playgroud) Let's say I have (-5) mod 8.
I tried it in both languages Java and C, and they gave me a -5 result when I was expecting 3.
Why is this happening? Can a modulus be negative? And what should I change to get the correct result?
Java code
public class Example {
public static void main(String[] args) {
int x;
x = -5%8;
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
C code
int main(){
int x;
x = -5%8;
printf("%d", x); …Run Code Online (Sandbox Code Playgroud) 我在 Python 中没有 NumPy 的情况下做到了这一点:
def fun_list(list_, x):
#set list to odd numbers in list_ raised to number x
s=[]
s= [i**x for i in list_ if (i%2)!=0]
list_=s
print(list_)
Run Code Online (Sandbox Code Playgroud)
测试功能:
list1 = [1, 2, 3, 4]
list2 = [2, 3, 4, 5]
print(fun_list(list1, 2))
print(fun_list(list2, 3))
Run Code Online (Sandbox Code Playgroud)
结果:
[1, 9]
None
[27, 125]
None
Run Code Online (Sandbox Code Playgroud)
现在需要使用 NumPy 来完成它,我不明白并且在网上找不到太多关于它的信息,我发现的东西对我来说没有意义。这是我尝试过的:
import math
#set list to odd numbers in list_ raised to number x
a=np.array([array_])
pwr=x
a=np.array([a if np.mod(a)!=0])
a=np.array([np.power(a,pwr)])
print (a)
Run Code Online (Sandbox Code Playgroud)
测试功能:
import …Run Code Online (Sandbox Code Playgroud) 我有一个函数生成我想要应用mod的整数列表,这样列表中的元素就不会大于25.例如,列表[6,8,18,28,14,25]应该返回为[6,8,18,2,14,25].到目前为止的代码如下.
let charIntList = zip ['a'..'z'] [0..25]
let getIntFromList (x:xs) a = if fst x == a then snd x else getIntFromList xs a
charToInt :: Char -> Int
let charToInt a = getIntFromList charIntList a
zipWithPosition :: [Char] -> [(Char, Int)]
let zipWithPosition m = zip m [0..length m]
position :: [(Char, Int)] -> [Int]
let position m = map snd (zipWithPosition m)
messToInt :: [Char] -> [Int]
let messToInt m = map charToInt m
almostVig :: [Int] [Int] …Run Code Online (Sandbox Code Playgroud) C# 中是否有用于 RSA 算法的 mod 运算符?我一直在使用 % ,因为我认为这可以用作 mod,但是我得到的 c 和 m 的答案不正确,所以我意识到 % 不起作用。
double e = 13;
double d; //decryption
double de = 7;
d = ((de * euiler) + 1) / e;
double message = 25;
double c = Pow(message, e) % n;
double m = Pow(c, d) % n;
Run Code Online (Sandbox Code Playgroud) #include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main(){
stringstream ss;
ss << 32;
string str = ss.str();
cout << str << endl
<< str[0] << endl
<< str[1] <<endl
<< str[0]%10;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
32
3
2
1
相反,最后一行应为3,因为3%10 = 3.