How to split up large numbers ?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 4441 Views - Last Post: 27 July 2011 - 11:43 AM Rate Topic: -----

#1 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

How to split up large numbers ?

Posted 19 July 2011 - 12:13 PM

I have a silly question, it's been 4 years since i touched java but how would one be able to take a number such as 15,000,701,800,029 and split it up and output it in the following method using a linked list.

15*10^12+0*10^9+701*10^6+800*10^3+29*10^0=the number I have posted above.

I was thinking using a node for each digit that stores an integer between 0 and 999 in each node.

So for the example above it would require a 5 node list. I uderstand the aspect but can't seem to program it.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: How to split up large numbers ?

#2 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Re: How to split up large numbers ?

Posted 19 July 2011 - 12:19 PM

Why not split the number at the commas after making it a String and with each section put it in the format you want while decreasing in power by three each time?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7959
  • View blog
  • Posts: 13,576
  • Joined: 19-March 11

Re: How to split up large numbers ?

Posted 19 July 2011 - 12:24 PM

If you're getting the number a string formatted with commas, split on the comma and come back down the resulting array from length to 0. If it's an integer (or long or BigInt or whatever) then use % to extract a glob of digits and / to reduce the result.
Was This Post Helpful? 0
  • +
  • -

#4 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to split up large numbers ?

Posted 19 July 2011 - 02:40 PM

But what if there are no commas?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7959
  • View blog
  • Posts: 13,576
  • Joined: 19-March 11

Re: How to split up large numbers ?

Posted 19 July 2011 - 02:50 PM

Quote

If it's an integer (or long or BigInt or whatever) then use % to extract a glob of digits and / to reduce the result.


% is the modulo operator - it returns the remainder from integer division, while / returns the quotient. So 5/2 = 2, 5%2=1.

Now, what's the result of
r = 123456789 % 1000 ;
q = 123456789 / 1000 ;

System.out.println("r = " + r + " q = " + q);


Then what happens if you perform that repeatedly?

You don't need to put that into the computer, you can do it in your head.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to split up large numbers ?

Posted 19 July 2011 - 09:30 PM

just do
str.replace(",", "");
and then BigInteger x = new BigInteger(str);
Was This Post Helpful? 0
  • +
  • -

#7 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to split up large numbers ?

Posted 26 July 2011 - 10:00 AM

import java.math.BigDecimal;

public class Project1{
  public static void main(String[] args) {
  BigDecimal num1, num2;
  num1 = new BigDecimal(50.00035);
  num2 = new BigDecimal(100.0025);
  Sum(num1, num2);
  }

  public static void Sum(BigDecimal val1, BigDecimal val2){
  BigDecimal sum = val1.add(val2);
  System.out.println("Sum of two BigDecimal numbers: "+ sum);
  }
}



A little change:

So what I need to do is something similar to this HOWEVER using a linked list. If I have a number say 909600 and 702 it will add the two numbers THEN multiply the two same numbers and output the addition result and the multiplication result.
Was This Post Helpful? 0
  • +
  • -

#8 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to split up large numbers ?

Posted 26 July 2011 - 10:08 AM

- a method to read from file
- a method to put a large number into a linked list
- a method to add two large numbers
- a method to multiply two large numbers
- a method to output a large number

Thats what the program needs to do and unfortunately since i havent touched java in a WHILE and ive been doing C++ ive lost my touch and have now idea on how to even start this

EX:
LargeNumber1: 1 506 033 005 018 000 029
LargeNumber2: 1 992
Sum is: (whatever the sum is)
Product is: ( whatever the product is)
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10779
  • View blog
  • Posts: 40,135
  • Joined: 27-December 08

Re: How to split up large numbers ?

Posted 26 July 2011 - 12:15 PM

Have you taken a look at the documentation for the String class yet? Specifically the substring() method.
Was This Post Helpful? 1
  • +
  • -

#10 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to split up large numbers ?

Posted 26 July 2011 - 01:53 PM

no i have not.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10779
  • View blog
  • Posts: 40,135
  • Joined: 27-December 08

Re: How to split up large numbers ?

Posted 26 July 2011 - 01:54 PM

That would be a great starting point. :)
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to split up large numbers ?

Posted 26 July 2011 - 09:06 PM

View Postdaonlyillwiz, on 26 July 2011 - 04:53 PM, said:

no i have not.

admitted fault is half forgiven :)
Was This Post Helpful? 1
  • +
  • -

#13 daonlyillwiz  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: How to split up large numbers ?

Posted 27 July 2011 - 09:44 AM

Heh, im doing it now but this damn project is due today at 6 and i dont even know where to start. :-\
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10779
  • View blog
  • Posts: 40,135
  • Joined: 27-December 08

Re: How to split up large numbers ?

Posted 27 July 2011 - 09:48 AM

So what are you asking of us? You know you need to break apart the Strings. You know you need to put those substrings in a LinkedList. You know you need to be able to exponentiate ten and store a sum. Break down the problem.
Was This Post Helpful? 1
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to split up large numbers ?

Posted 27 July 2011 - 09:54 AM

View Postdaonlyillwiz, on 26 July 2011 - 01:08 PM, said:

Thats what the program needs to do and unfortunately since i havent touched java in a WHILE and ive been doing C++ ive lost my touch and have now idea on how to even start this

No big difference between Java and C++
Take the String
remove the space
convert to an array of char
add from right to left each element taking care of the reminder if there is one
Code in Java and C will look almost the same
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3