错误---模数不正
BigInteger正在考虑vaule 0或-ve,但我无法弄清楚在哪里
public int[] conCheck(BigInteger big)
{
int i=0,mul=1;
int a[]= new int[10];
int b[]= new int[10];
BigInteger rem[]= new BigInteger[11];
BigInteger num[]= new BigInteger[11];
String s="100000000";//,g="9";
//for(i=0;i<5;i++)
//s=s.concat(g);
BigInteger divi[]= new BigInteger[11];
divi[0]=new BigInteger(s);
num[0]=big;
for(i=0;i<10;i++)
{
int z = (int)Math.pow((double)10,(double)(i+1));
BigInteger zz = new BigInteger(String.valueOf(z));
divi[i+1]=divi[i].divide(zz);
num[i+1]=num[i].divide(zz);
}
{ for(i=0;i<10;i++)
{
rem[i] = num[i].mod(divi[i]);
b[i]=rem[i].intValue();
if(i>=4)
{
mul= b[i]*b[i-1]*b[i-2]*b[i-3]*b[i-4];
}
a[i]=mul;
}
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
在控制台上出错
C:\jdk1.6.0_07\bin>java euler/BigConCheck1
Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus no …Run Code Online (Sandbox Code Playgroud) 我怀疑之前有人问过,但似乎找不到符合的问题......
我正在使用Scala,但我很确定这只是一个Java问题......输入值是双倍的
println(28.0 / 5.6)
println(28.0 % 5.6)
Run Code Online (Sandbox Code Playgroud)
这些线的结果是
5.0
1.7763568394002505E-15
Run Code Online (Sandbox Code Playgroud)
这意味着Java正确执行除法,但由于某种原因得到模数错误,因为对于任何解析为整数的除法问题,模数应为0 ...
这有解决方法吗?
谢谢!
我需要创建一个在 C 中实现的算法,该算法在任意数量的字节和一个字节之间进行模运算。看到这个:
typedef struct{
u_int8_t * data;
u_int16_t length;
}UBigInt;
u_int8_t UBigIntModuloWithUInt8(UBigInt a,u_int8_t b){
}
Run Code Online (Sandbox Code Playgroud)
对于 2 的幂,可以使用 & (b-1) 但非 2 的幂呢?
我意识到一种方法是:a - b*(a/b)
这将需要使用 UBigIntDivisionWithUInt8 和 UBigIntMultiplicationWithUInt8 和 UBigIntSubtractionWithUBigInt。可能有更有效的方法来做到这一点?
谢谢你。
这是我现在的实现:
u_int8_t UBigIntModuloWithUInt8(UBigInt a,u_int8_t b){
if (!(b & (b - 1)))
return a.data[a.length - 1] & b - 1; // For powers of two this can be done
// Wasn't a power of two.
u_int16_t result = 0; // Prevents overflow in calculations
for(int x = …Run Code Online (Sandbox Code Playgroud) 在哪里可以找到计算整数欧几里得除法余数的实现或库0 <= r < |n|?
考虑以下函数,它在编译时根据参数类型计算积分或浮点模数:
template<typename T>
constexpr T modulo(const T x, const T y)
{
return (std::is_floating_point<T>::value) ? (x < T() ? T(-1) : T(1))*((x < T() ? -x : x)-static_cast<long long int>((x/y < T() ? -x/y : x/y))*(y < T() ? -y : y))
: (static_cast<typename std::conditional<std::is_floating_point<T>::value, int, T>::type>(x)
%static_cast<typename std::conditional<std::is_floating_point<T>::value, int, T>::type>(y));
}
Run Code Online (Sandbox Code Playgroud)
这个功能的身体能改善吗?(我需要为整数和浮点类型都有一个函数).
我试图使用%C++和fmod函数的运算符得到-1 modulo 1000000007的结果.
输出是-1,但是-1 modulo 1000000007==1000000006.
我做错了什么?
我们如何pow在模块化环境中使用负指数?
pow(x,y,[z])如果存在z,则x和y必须为整数类型,并且y必须为非负数。
>>> pow(11444, -357)
0.0
>>> pow(11444, -357) % 48731
0.0
>>> pow(11444, -357, 48731)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pow() 2nd argument cannot be negative when 3rd argument specified
Run Code Online (Sandbox Code Playgroud)
在我的用例中,我想使用Schnorr方案对消息进行加密:
y = (g ** -w) mod p
Run Code Online (Sandbox Code Playgroud)
但pow此处不接受负数作为第二个参数。例如,从
g = 11444
p = 48731
w = 357
Run Code Online (Sandbox Code Playgroud)
y应该是7355。
所以我最近一直在研究模数.我正在努力提高自己的数学技能,如果我是诚实的话,这不是最好的.但我正在努力改进.我理解这是如何工作的.我也很有能力进行长期分工.然而有些东西在困扰我,我似乎无法在网上找到答案.
我知道7%5 = 2(5次进7次,余数2次).
我不明白的是这个;
1%3 = 1
怎么会这样,3进1次,0次,余数3?肯定是1%3 = 3的答案?
有人能用最简单的方法解释一下吗?
我是否认为如果被除数(1)小于我们知道将等于0余数x的除数(3),它只是使用被除数作为结果?
谢谢你的帮助.
如何在Elixir中使用模运算符?
例如,在Ruby中,您可以执行以下操作:
5 % 2 == 0
Run Code Online (Sandbox Code Playgroud)
它与Ruby的模运算符有何不同?