java中的大数乘法和加法
我目前正在开发一个java类,该类应该能够乘法和加法,其整数的大小是任何原始数据类型都无法存储的。我需要帮助想出一个算法来把这些大数字相乘。需要工作的方法是java中的大数乘法和加法,java,algorithm,multiplication,Java,Algorithm,Multiplication,我目前正在开发一个java类,该类应该能够乘法和加法,其整数的大小是任何原始数据类型都无法存储的。我需要帮助想出一个算法来把这些大数字相乘。需要工作的方法是public-BigNum-mult(BigNum-other)。任何帮助都将不胜感激 public class BigNum { // F i e l d s private String num; // Representation of our number // C o n s t r u c t o
public-BigNum-mult(BigNum-other)
。任何帮助都将不胜感激
public class BigNum {
// F i e l d s
private String num; // Representation of our number
// C o n s t r u c t o r s
public BigNum(String num) {
for (int i=0; i<num.length(); i++) {
if ( ! Character.isDigit(num.charAt(i)) ) {
throw new IllegalArgumentException();
}
}
if ( num.length() == 0 ) {
throw new IllegalArgumentException();
}
this.num = num;
}
/** Constructs a <tt>BigNum</tt> from a non-negative integer.
* @param num The non-negative integer to be interpreted.
* @throws IllegalArgumentException if num is negative
*/
public BigNum(int num) {
// If num<0, redirected constructor will throw exception due to "-"
this(""+num);
for(int i=0; i<num; i++)
{
if(num<0)
{
throw new IllegalArgumentException();
}
}
}
/** Constructs a <tt>BigNum</tt> with value zero.
*/
public BigNum() {
num="0";
}
// P u b l i c M e t h o d s
/** Adds two <tt>BigNum</tt> objects' values together and returns a new
* <tt>BigNum</tt> object with the resulting value.
*
* @param other this and other objects get added together
* @return a new BigNum with the resulting value
*/
public BigNum add(BigNum other) {
// Make shorter refer to the shorter num, longer to the longer num
String shorter = other.num;
String longer = this.num;
if (this.num.length() < other.num.length()) {
shorter = this.num;
longer = other.num;
}
// Prepend zeros to make shorter as long as longer
while (shorter.length() < longer.length()) {
shorter = "0" + shorter;
}
// Add columns like we did in grade school
int carry = 0;
String result = "";
for (int i=shorter.length()-1; i>=0; i--) {
int temp = Character.getNumericValue(shorter.charAt(i)) +
Character.getNumericValue(longer.charAt(i)) + carry;
result = (temp%10)+result;
carry = temp/10;
}
// Handle carry-out, if there is one. Return result
if (carry == 1) {
result = "1"+result;
}
return new BigNum(result);
}
***public BigNum mult(BigNum other)***
{
String shorter = other.num;
String longer = this.num;
if(this.num.length()<other.num.length())
{
shorter = this.num;
longer = other.num;
}
if(shorter=="0"||longer=="0")
{
return new BigNum(0);
}
int carry = 0;
int temp =0;
String result = "";
for(int i=shorter.length()-1; i>=0; i--)
{
for(int j=longer.lenght()-1; j>=0; j--)
{
temp = Character.getNumericValue(shorter.charAt(i)) *
Character.getNumericValue(longer.charAt(j));
}
}
result = temp+result;
return new BigNum(result);
}
/** Returns a string representation of the number. No leading zeros
* will exist unless the overall value is zero.
*
* @return String representation of the BigNum
*/
public String toString() {
return num;
}
/** Used only for unit testing. When run, should output only trues. */
public static void main(String[] args) {
// Test constructors
BigNum test = new BigNum("123");
System.out.println(test.toString().equals("123"));
test = new BigNum(123);
test = new BigNum();
System.out.println(test.toString().equals("0"));
// Test addition
BigNum a = new BigNum();
BigNum b = new BigNum();
BigNum c = a.add(b);
BigNum d = new BigNum();
BigNum e = new BigNum();
System.out.println(c.toString().equals("0"));
a = new BigNum("999");
b = new BigNum("101");
c = a.add(b);
System.out.println(c.toString().equals("1100"));
a = new BigNum("237468273643278");
b = new BigNum("87326487236437826");
c = a.add(b);
d= new BigNum("1");
e = a.mult(b);
System.out.println(e);
System.out.println(c.toString().equals("87563955510081104"));
//String s = "1234";
//int x =Integer.valueOf(s);
//System.out.println(x);
}
}
公共类BigNum{
//F i e l d s
私有字符串num;//表示我们的数字
//C o n s t r u C t o r s
公共BigNum(字符串num){
对于(int i=0;i我建议将您的大数字作为int[]
或列表存储在内部,其中每个项目都是<10
且最不重要。这将使您的计算更加容易
我还建议将你的BigNum
设置为不可变的(如果作业允许的话)。这样可以更清楚地了解你在做什么
您可以将大数乘法分解为单独的方法,这些方法可以使用高中数学进行组合:multiplyBy(int)
(数字<10-使用简单进位)、multiplybyten
(左移加零)、getDigits
(获取以单位开头的数字列表)
然后,这些可以组合到长乘法的标准算术算法中:
BigNum multiplyBy(BigNum bigNum) {
BigNum answer = BigNum(0);
for (int digit: getDigits()) {
answer = answer.add(bigNum.multiplyBy(digit));
bigNum = bigNum.multiplyByTen();
}
return answer;
}
这是一个类赋值还是您想学习如何实现它?否则有biginger
(和BigDecimal
)在JDK中,您需要回忆一下小学长的乘法步进。这是一个作业,我不能使用容易获得的BigTigand和BigDigimals。如果您坚持不使用<代码> BigDecimal < /> >或<代码> BigInteger <代码>,java java SDK,至少考虑一下他们的来源,看看Java效率如何。y实现了它们。其中一些东西超出了我们头脑中可能使用的逻辑方法,而是依靠数论来优化机器的速度。等到你开始做诸如n次方根之类的运算时,你必须咨询好的牛顿。关于这一点,非常好的帖子在