【发布时间】:2016-09-11 16:00:17
【问题描述】:
我这辈子都不知道如何找到模乘逆模 5。我已经阅读了所有的 wiki 文章、观看了视频,甚至向同学寻求帮助,但找不到解决方案。我发现的所有东西要么是另一种编程语言,我不能在其中翻译成 Java(编程新手)和/或使用双精度数而不是整数,我只能根据教授的规范使用整数。这是我到目前为止写的类,在我弄清楚inverse()方法之前不能使用divide()方法:
public class ModInt {
/**
* the integer modulo base
*/
private int base;
/**
* the number
*/
private int number;
/**
* creates the modulo 2 number 0
*/
public ModInt()
{
base = 2;
number = 0;
}
/**
* creates a modulo b number n
* @param n the number
* @param b the base
*/
public ModInt(int n, int b)
{
number = n;
base = b;
}
/**
* creates an equivalent number in the same integer modulo base as the specified integer modulo number.
* @param m an integer modulo number
*/
public ModInt(ModInt m)
{
number = m.number;
base = m.base;
}
/**
* gives the number of the integer modulo number.
* @return the number
*/
public int getNumber()
{
return number;
}
/**
* gives the base of the specified integer modulo number.
* @return the base
*/
public int getBase()
{
return base;
}
/**
* modifies the integer modulo number using the specified parameters
* @param n the new number
* @param b the new base
*/
public void setModInt(int n, int b)
{
number = n;
base = b;
}
/**
* adds this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the sum of this number and the specified number
*/
public ModInt add(ModInt m)
{
return new ModInt((number + m.number) % base, base);
}
/**
* subtracts this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the difference this number and the specified number
*/
public ModInt subtract(ModInt m)
{
return new ModInt(((base - number + m.number) % base, base);
}
/**
* multiplies this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the product of this number and the specified number
*/
public ModInt multiply(ModInt m)
{
return new ModInt((number * m.number) % base, base);
}
/**
* computes the inverse of this integer modulo number
* @return the inverse of this number
*/
public ModInt inverse()
{
return new ModInt();
}
/**
* divides this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the quotient of this number and the specified number
*/
public ModInt divide(ModInt m)
{
return new ModInt();
}
/**
* give the string representation of an integer modulo number in the format
* n(mod b), where n is the number and b is the base
* @return a string representation of the integer modulo number in the format
* n(mod b); for example 3(mod 5) is the representation of the number
* 3 in integer modulo base 5
*/
public String toString()
{
return String.format("%d(mod %d)", number, base);
}
}
我正在尝试编写 inverse() 方法,以便它返回整数模数的倒数 (mod 5)。现在,我让它只返回默认构造函数,这样在运行代码时错误就会消失。任何人都可以尝试解释如何仅使用整数类型、没有双精度类型或任何其他类型来找到模乘逆吗?这是我教授的解释,但我不明白:
乘法逆或简单的数字 n 的逆, 表示 n^(-1),以整数模基 b 表示,是一个数,当 乘以 n 等于 1;也就是说,n × n^(−1) ≡ 1(mod b)。为了 例如,5^(−1) 整数模 7 为 3,因为 (5 × 3) mod 7 = 15 mod 7 ≡ 1。 数字 0 没有倒数。不是每个数字都是可逆的。为了 例如,2^(-1) 整数模 4 是不确定的,因为 {0 中没有整数, 1, 2, 3}可以乘以2得到1。
感谢任何帮助,谢谢。
【问题讨论】:
-
你有一些代码,但究竟是什么不起作用?!如果你能给出一些具体的例子来说明你的代码没有按照你期望的那样做,那将会很有帮助。不要指望我们为您做到这一点!
-
@GhostCat 抱歉,我已将我的问题更新为更具体。
-
PS 以下行
System.out.println(new ModInt(2, 5).subtract(new ModInt(4, 5)));打印-2(mod 5)— 是否符合预期?我可能期望 3(mod 5) 因为 3 + 4 等于 2 (mod 5)。 -
@OleV.V.是的,你是对的。我在上面编辑了我的方法。谢谢。
标签: java