# How to split up large numbers ?

• (3 Pages)
• 1
• 2
• 3

## 39 Replies - 6172 Views - Last Post: 27 July 2011 - 11:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=240151&amp;s=1b57da29f350a3662e79d8f466c62ccb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 daonlyillwiz

Reputation: -10
• 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.

Is This A Good Question/Topic? 0

## Replies To: How to split up large numbers ?

### #2 TFoSSDQ

• D.I.C Regular

Reputation: 123
• 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?

### #3 jon.kiparsky

• Beginner

Reputation: 10974
• Posts: 18,725
• 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.

### #4 daonlyillwiz

Reputation: -10
• 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?

### #5 jon.kiparsky

• Beginner

Reputation: 10974
• Posts: 18,725
• 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.

### #6 pbl

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

Reputation: 8378
• Posts: 31,956
• 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);

### #7 daonlyillwiz

Reputation: -10
• 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){
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.

### #8 daonlyillwiz

Reputation: -10
• 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)

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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.

### #10 daonlyillwiz

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

## Re: How to split up large numbers ?

Posted 26 July 2011 - 01:53 PM

no i have not.

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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.

### #12 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: How to split up large numbers ?

Posted 26 July 2011 - 09:06 PM

daonlyillwiz, on 26 July 2011 - 04:53 PM, said:

no i have not.

### #13 daonlyillwiz

Reputation: -10
• 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. :-\

### #14 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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.

### #15 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: How to split up large numbers ?

Posted 27 July 2011 - 09:54 AM

daonlyillwiz, 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