473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multiplying very large integers

1
Hi, I'm new here, and I'm having a problem implementing a multiplication method for a class called VeryLongInt. It's for a homework problem. As far as I know, it should work just like the Bignum class that comes with the JDK. Here is my code for the method:

Expand|Select|Wrap|Line Numbers
  1.     public void multiply (VeryLongInt otherVeryLong)
  2.     {
  3.         final int BASE = 10;
  4.  
  5.         int largerSize, partialProduct, carry = 0;
  6.         int i = 0, j = 0; // i is the bottom factor LSD, j the upper factor LSD
  7.  
  8.         largerSize = (digits.size() + otherVeryLong.digits.size()) - 1; // 5 digit quantity * another 5 digit quantity = 9 digit quantity
  9.  
  10.         ArrayList<Integer> productDigits = new ArrayList<Integer>(largerSize);
  11.  
  12.         for (otherVeryLong.least(i); i < otherVeryLong.digits.size(); i++)
  13.         {
  14.             for (least(j); j < digits.size(); j++)
  15.             {
  16.                 partialProduct = (least(j) * otherVeryLong.least(i)) + carry;
  17.                 carry = partialProduct / BASE;
  18.                 productDigits.add (partialProduct % BASE);
  19.             }
  20.         }
  21.         Collections.reverse (productDigits);
  22.         digits = productDigits;
  23.  
  24.     }
  25.  
The class that this method goes with is here: http://www.mhhe.com/engcs/compsci/co...ryLongInt.java

The two numbers I'm trying to multiply are 12,345 and 51,612. I'll try to explain the situation clearly.

I've used two for loops, one nested in the other, to take each digit from the bottom multiplicand (starting from the one's place) and multiply it by each digit in the upper multiplicand. It should then move on to the tens place in the bottom multiplicand and multiply that by all the digits in the upper multiplicand, and so on.

Curiously, after execution, my productDigits ArrayList only seems to hold the product of the one's place number in the bottom multiplicand times the upper multiplicand (12,345 x 2 = .24,960). I would have thought that it would hold 12,345 x 5, which should be the latest value.

Anyway, in addition to that, I'm not sure how add together all of the products to get the final result. I know that I will have to shift the places over somehow, but I don't know what approach to take. Do I need to make more ArrayLists to hold each product, then sum them together afterwards?

I've been working on this for a while and am starting to get frustrated. Can anyone give me some general tips to point me in the right direction? Of course, if that's against the rules (as this is part of a homework assignment), then no problem.
Oct 18 '07 #1
1 6855
r035198x
13,262 8TB
Hi, I'm new here, and I'm having a problem implementing a multiplication method for a class called VeryLongInt. It's for a homework problem. As far as I know, it should work just like the Bignum class that comes with the JDK. Here is my code for the method:

Expand|Select|Wrap|Line Numbers
  1.     public void multiply (VeryLongInt otherVeryLong)
  2.     {
  3.         final int BASE = 10;
  4.  
  5.         int largerSize, partialProduct, carry = 0;
  6.         int i = 0, j = 0; // i is the bottom factor LSD, j the upper factor LSD
  7.  
  8.         largerSize = (digits.size() + otherVeryLong.digits.size()) - 1; // 5 digit quantity * another 5 digit quantity = 9 digit quantity
  9.  
  10.         ArrayList<Integer> productDigits = new ArrayList<Integer>(largerSize);
  11.  
  12.         for (otherVeryLong.least(i); i < otherVeryLong.digits.size(); i++)
  13.         {
  14.             for (least(j); j < digits.size(); j++)
  15.             {
  16.                 partialProduct = (least(j) * otherVeryLong.least(i)) + carry;
  17.                 carry = partialProduct / BASE;
  18.                 productDigits.add (partialProduct % BASE);
  19.             }
  20.         }
  21.         Collections.reverse (productDigits);
  22.         digits = productDigits;
  23.  
  24.     }
  25.  
The class that this method goes with is here: http://www.mhhe.com/engcs/compsci/co...ryLongInt.java

The two numbers I'm trying to multiply are 12,345 and 51,612. I'll try to explain the situation clearly.

I've used two for loops, one nested in the other, to take each digit from the bottom multiplicand (starting from the one's place) and multiply it by each digit in the upper multiplicand. It should then move on to the tens place in the bottom multiplicand and multiply that by all the digits in the upper multiplicand, and so on.

Curiously, after execution, my productDigits ArrayList only seems to hold the product of the one's place number in the bottom multiplicand times the upper multiplicand (12,345 x 2 = .24,960). I would have thought that it would hold 12,345 x 5, which should be the latest value.

Anyway, in addition to that, I'm not sure how add together all of the products to get the final result. I know that I will have to shift the places over somehow, but I don't know what approach to take. Do I need to make more ArrayLists to hold each product, then sum them together afterwards?

I've been working on this for a while and am starting to get frustrated. Can anyone give me some general tips to point me in the right direction? Of course, if that's against the rules (as this is part of a homework assignment), then no problem.
You could start by posting your code here. It's just a matter of copy and paste. Some of us are afraid of clicking links.
Oct 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DJTB | last post by:
Hi, I'm trying to manually parse a dataset stored in a file. The data should be converted into Python objects. Here is an example of a single line of a (small) dataset: 3 13 17 19...
2
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
6
by: Michael Zielinski | last post by:
I was wondering if anybody knows of a package that does arbitrarily large integers that I can get preferably for free.
5
by: Edith Gross | last post by:
I'd like to add to unsigned integers and find out whether a carry occurs. Can I do that in a simple and fast way? A similar problem is subtracting with borrow and the multiplication of two...
4
by: KC | last post by:
How can I represent a 22-digit integer in C? float is not large enough. Thanks!
42
by: yong | last post by:
Hi all I have an large integer in this format x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6 now I must convert it to this format y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5 x1-x5...
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
29
by: Halid Umar A M | last post by:
Hi All, I m Halid Umar, network security research student. I will explain my situation and give me hint if you can. I expect reply from you all. * I have to handle numbers that have more than...
4
by: Tenacious | last post by:
Why is it that when I multiply two double values like this: double Multi = 100.0; double Force = 0.29; double Result = Multi * Force; I get Result = 28.999999999996 instead of 29.0? I'm not...
4
by: Shisou | last post by:
hello everyone, Well I tried to solve this one on my own but it seems i need your help again :\ I'm trying to write a program to add or subtract two large integers... easy enough.. Where i...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.