I need to store two extremely large numbers (Strings, since they won't fit in int) in two linked lists, add them and then display the result (again, a String).
I can store the numbers directly into the list.
312312 can be stored as 2->1->3->2->1->3 (actual number will be extremely long)
111119 can be stored as 9->1->1->1->1->1
Then I can add them
11->2->4->3->2->4
Normally I could do 11*10^0 + 2*10^1 +...+ 4*10^5 and get 423431 but all those operations (multiplication, addition and exponentiation) would again be integer operations and since the actual numbers are going to be extremely big, int or long won't support the operations. The final result has to be a string.
So I need a way to convert 11->2->4->3->2->4 into 423431 without using int. Also, I cannot use BigInteger. Can anyone help me?
Well, first thing you need to do is implement carry.
For each digit (that is >= 10), you need to increase the next digit by that digit /10 and set that digit to that digit %10.
So 11->2->... becomes 1->3->....
Then to actually produce the string.
For the most performant option, I suggest StringBuilder.
Just append each digit in the linked-list, then just reverse().toString() (since you started with the smallest number).
Think about how you would do it by hand on paper. If the sum of a pair digits is greater than 9 you write down a carry digit of 1, which you add into the sum of the next pair of digits.
In a computer program you can use a local variable for that: add digits from first and last numbers and the carry from earlier, if sum is greater than.. set carry to 1, else set carry to 0, move on to the next pair...
Related
Today, I have a question about multiplying, dividing numbers with a specific format. The format is like this: I will make the number into a linked list, and do the operation with this linked list. For example, if I want to multiply 1234567 and 7890123, I will make these two numbers into linked list by cutting the numbers into 3 digits at one time, like 1,234,567 and 7,890,123. Then, I will put one chunk at one node(1, 234, 567 are three chunks, and they all go into different nodes) for every numbers. Now, the problem is, I have to multiply, divide, and get the quotient and remnant of the operation between the two numbers without changing them back to the real 'numbers'. The operation must be done with the form of linked list. So, for the multiplying, I've thought 'let's multiply the numbers in each node first, and put the zeros in the back to adjust the cipher, then add them all to finish the operation. for example, if I want to multiply 1,234 and 5,678, I will do (234*678 + (1*678)000 + (5*234)000 + (1*5)000,000). But, I can't be certain this method is right, And I can't imagine about the dividing and getting the quotient and remnant part. So, please help..!
My teacher and I were discussing whether or not a recursive permutation function could be written without the use of substrings and/or arrays in Java.
Is there a way to do this?
The answer is yes, this can be done. I'm assuming that "without the use of substrings and/or arrays" refers to the info being passed to the recursion. You have to have some sort of container for the elements that are to be permuted.
In that case it can be done by pulling some hideous tricks with numerically encoding the indices of the elements as digits of a numeric argument. For instance, if there are 3 elements and I use 1 as a sentinel value in the left-most digit (so you can have 0 as the leading index sometimes), 1 means I haven't started, 10 means the first element has been selected, 102 means the first and third, and 1021 means I'm ready to print the permutation since I now have a 4 digit argument and there are 3 elements in the set. I can then deconstruct which elements to print using % 10 and / 10 arithmetic to pick them off.
I implemented this in Ruby rather than Java, and I'm not going to share the actual code because it's too horrible to contemplate. However, it works recursively with only the input array of elements and an integer as arguments, no partial solution substrings or arrays.
I have a n-digit number composed of 1's only. I want to replace 1's with 0's in all possible combinations and store the combinations in an array.
How do I find all combinations?
I was thinking of starting with one zero and then increasing the number if zeroes that will replace 1's.
If there are 2 zeroes, for example, then i will keep the position of one zero fixed and move the other, till it reaches the end then i'd do the same for the other zero. But then i'll have to root out the repeated combinations.
Basically, this is getting complicated. I want to know a better way to find combinations!
You are simply trying to generate n-digit binary numbers. It means that you can generate 2^n different numbers. So here you go:
Firstly, calculate 2^n.
Implement a loop, starting from 0 and ending at 2^n.
For every loop variable, take the variable as a decimal and convert
it to binary and append some leading zeros (if necessary) to make it
length of n.
Add the binary to your permutations array.
I’m working on a problem where its demanded to get the last 100 digits of 2^1001. The solution must be in java and without using BigInteger, only with int or Long. For the moment I think to create a class that handle 100 digits. So my question is, if there is another way the handle the overflow using int or long to get a number with 100 digits.
Thanks for all.
EDITED: I was off by a couple powers of 10 in my modulo operator (oops)
The last 100 digits of 2^1001 is the number 2^1001 (mod 10^100).
Note that 2^1001 (mod 10^100) = 2*(2^1000(mod 10^100)) (mod 10^100).
Look into properties of modulo: http://www.math.okstate.edu/~wrightd/crypt/lecnotes/node17.html
This is 99% math problem, 1% programming problem. :)
With this approach, though, you wouldn't be able to use just an integer, as 10^100 won't fit in an int or long.
However, you could use this to find, say, the last 10 digits of 2^1001, then use a separate routine to find the next 10 digits, etc, where each routine uses this feature...
The fastest way to do it (coding wise) is to create an array of 100 integers and then have a function which starts from the ones place and doubles every digit. Make sure to take the modulus of 10 and carry over 1s if needed. For the 100th digit, simply eliminate the carry over. Do it 1001 times and you're done.
So I have an two arrayLists that represent two numbers. This is so I don't have to use BigInt. so for example
ArrayList<Integer> LargeInt = [2,3,6,4] would really equal 2,364
ArrayList<Integer> LargeInt2 = [8,7,9,4,6] would be 87,946
my goal is to figure out a way to multiply the two numbers and make the answer to a string. I know that multiplying two numbers would need to be put into another array before put into a string so it won't crash with larger numbers. I also know it will be one for loop put into another. But i am finding it difficult to make a code that multiplies the two numbers. the two arrays being multiplied can be any number.
Assuming that this is homework, here is a no-code explanation of what you need to do:
Define a class that wraps ArrayList<Integer>; let's say you call it ArrayInt
Define an operation that adds two ArrayInts together, and returns a third ArrayInt that equals their sum. You can do it digit by digit, taking care of a possible carry into an extra digit, so you need to size your result accordingly.
Define an operation that multiplies your number by a power of ten by adding zeros to the array list. Again, the operation should return a new ArrayInt, rather than modifying the current one
Define an operation that multiplies a number by a single digit. You can use multiplication, or a simple loop that uses addition. The loop would not run more than nine times, so it shouldn't be too bad.
Combine the three operations that you have (addition, multiplication by a digit, and multiplication by a power of ten) into a simple multiplication algorithm that you learned in the elementary school.