Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Assignment help: multiply large numbers without BigInteger  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm very new to both Java and this forum. I hope I am posting in a correct thread.

I was given an assignment that is supposed to let me calculate large numbers on tables, without using BigInteger. I have managed to write one for addition and substraction, but I am just not sure how to make one for multiplying. I checked with my professor, and he said the addition and substraction ones are fine, and that I should try to make one with multiplying based on them. But no matter what I try, I just can't get the correct result. I would be really grateful for any help.
Oh, also, we are not supposed to multiply using recurring addition.
 
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,

welcome to the ranch and enjoy the stay!

How about doing multiplication in the way you were taught at primary school, i.e like you do the addition and subtracting? It is a bit trickier, since you need an int [][] to store the in-between values.

Since you mention BigIntegers, is there any chance of using flexible lists instead of fixed-size arrays? And lastly, how do you handle negative input numbers?  
 
Andrew Whittle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

thank you so much for the reply!

No, we have to use fixed-size arrays. We can ignore negative numbers for now, it will be covered in later classes. We only have to focus on positive numbers.

Yes, I thought about doing it that way, but I have no idea where to start. We covered a little bit of how to do addition in class, so I managed to finish it. And substracting was a little similiar, though it took more time. But I don't know at all how to even start on multiplying.
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

It is about the same as addition, but slightly more complex.

You are familiar with the 'by hand' method:
   xxx
    yyy
   ===  *
   aaa
  bbb0
 ccc00
 =====
dddddd

So, lets call the first multiplicant A, the second one B. You have the method to turn a String into an array of ints.
So you would get something like:

int[][] inbetweenResults = new int[B.length][2 * max(A.length, B.length). Define a method that is able to multiply an array by just one digit. If you have that, you can fill the inbetweenResults array with arrays formed by the successive multiplication of the first multiplicant by all the digits of the second multiplicant, taking the overflows into account. Then, adding all the in between arrays, you already have an add-method for that.
And don't forget the zeroes at the right of the inbetween arrays!

As, said, a bit trickier, but it should be a lot of fun! And of course, you might get an 'ArrayIndexOutOfBound' exception,just add one to the InbetweenArrays dimension.

Keep us informed!
 
Andrew Whittle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took a bit of time, but it worked! Thank you so much for your help!
 
Marshal
Posts: 63842
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

Do show us your result (if yuo are allowed to).
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!