I want to find a remainder of very long numbers .I am writing a program for this and as I cannot find the remainder directly due to the fact that they are large numbers (in c) .How can I do this?the limit for the number from which I have to divide the bigger number to find remainder is 500.i.e 1 to 500
I thought of dividing the number like this:
1234567=1*10^6+2*10^5+...
1234567%x=1modx*10^6modx+2modx*10^5modx...
I need a better way than this.
Hint:
Use a linked list. Store the number as a group of numbers dynamically.
For eg:
112233445566778899001122 => 11223344 55667788 99001122
Now consider the individual unit and start from left to right. Find the reminder and manipulate it to add to the next group and go on.
Now implementation is very easy :)
Edit:
112233445566778899001122/6 => 11223344 55667788 99001122/6
11223344/6 =>2
2*100000000 + 55667788 = 255667788
255667788/6 => 0
0*100000000 + 99001122 = 99001122
99001122/6=>0
So the reminder is 0.
Remember, the individual unit after manipulation should be under the maximum range int can support.
If your question regards using very long or large numbers try using something a long long. The problem could be that the data type that you are using is too small to hold the values that you require.
You could try using a bignum library like GMP or another kind of ugly way in comparison would be to use arrays or lists, somewhat similar to this.
Other than that, the modulo operation % will calculate the remainder for you.
Related
I am trying to find the result of log(10^k) , where k is big number like 10000. For example :
BigDecimal first = BigDecimal.TEN.pow(10000);
double result = Math.log(first.doubleValue());
However "result" becomes Infinity , however on wolphram approximates it to 23025.85.Any suggestion how to find the result? As a result the number with the first two digits after the decimal point are enough for me.
Use the fact that
log(10^k) = k*log(10)
So:
System.out.println(10000 * Math.log(10));
Prints:
23025.850929940458
The problem you are likely having, is that Wolphram is able to either hold the powered value or it is doing the log operation first.
When running this like your example, you will have an extremely large number that goes past the maximum value for a BigDecimal, which should result in an error or an "infinity", because it overflows the capability of the data type, I would suggest doing the operation the other way arround, perhaphs process the log first on a base 1 value for example and only then multiply it by whatever powered number you are tying to use.
See, there is a simple property of logarithms that you can use:
log(x^y) = y*log(x)
So what you can do is:
double y = y*log(x);
System.out.println(Math.round(y));
Hope this helps!
I'm very (read: extremely) new to java and was going to make a table with 5 columns print out for an assignement.
The two first columns are strings, third one an int, fourth a double and fifth an int. It is three rows in total that are affected by this.
I formatted it with printf and:
System.out.printf(Locale.ENGLISH, "%s%10s%14d%20.2f%12d\n", stringOne,
stringTwo, firstInt, stupidDouble, secondInt);
and
"%s\t%s\t\t%d\t\t%2.4f\t\t%d\n" (with the format and stuff above as well ofc).
But the teacher didn't want me to "hard code" the layout since it's part of learning special commands (like \t) and thus wanted me to change it and adress it in the variable itself instead.
I've been trying everything I can think of but can't get it to skip the last decimals, which are unneccesary 0's.
I wish I could've just used the numbers as a string instead, but I need it to calculate the last int with:
static int lastInt = (int) Math.round ( stupidDouble - firstInt )
What I suspect that my teacher is looking for me to do is tabs all the way like:
"%s\t%s\t\t%d\t\t%f\t\t%d\n"
But I need to double to be e.g. 1.2345 instead of 1.234500.
If it makes any difference, it's 3 different doubles (3 different rows in the table); one with 2 decimals, one with 4 and one with 5.
It would more or less save my weekend if someone could help me with this. The simplest possible solution would be much appreciated. <3
For example
first number = 123456.....40 digits
second number = 123456.....40digits
Then I need to store the number
third = first * second;
after that I need to print third and again I need to perform operation like
fourth = third * third;
and print fourth. So how can I handle that much long integers which data type I need to use?
Use BigInteger class in java.math, then use BigInteger.multiply to multiply them together.
Check here for more on how to use it:
https://www.tutorialspoint.com/java/math/biginteger_multiply.htm
See this question its similar Arbitrary-precision arithmetic Explanation
the answer explain it quite good.
The basic is that you work with smaller parts. Just remember how you learned to work with big numbers in school (2-3 grade) you wrote down two numbers and
2351
*12
-----
4702
2351
------
28212
You just do small operations and store them somewhere you can put them in string or better in some array of integers. Where for example
number 123456789123456789 can be
number[0] = 6789
number[1] = 2345
number[3] = 7891
number[4] = 2345
number[5] = 1
String numberToShow = "";
for(int i = 0; i
There are some links for computer arighmetics
https://en.wikipedia.org/wiki/Category:Computer_arithmetic
and for adders
https://en.wikipedia.org/wiki/Category:Adders_(electronics)
In your computer you have basically also just some adders which can work only with some size of numbers and if you need to work with bigger you need to split it in smaller parts.
Some of this parts can be done parallel, so you can speed up your algorithm. These are usually more sophisticated.
But the basic principe is similar to working with big numbers on your primary school.
I want to do an app that, in function of the parameters checked, gives me a random String that I previusly set. The problem isn't the design of the app so the problem is the methods or classes that do what I want.
-I explain my objective with an example:
We have 100 plans to do this weekend. They are in a box and I want to pick randomly one. So great, I suppose that would be more or less easy, but, this weekend is raining so I want to pick a plan that it could be done even if it rains. And it's ending the month and I don't want to spend money so a free plan would be great. Then, I check the boxes "Free plan" and "Raining" and the app gives to me a random plan that meet the conditions.
How would you do that?
I was thinking about a bigs arrays like "Raining plans", "Free plans", with strings inside and when you check the two boxes the arrays were mixed and a string was randomly picked but I don't know how to do that and I think it could be done in another easiest way.
Thanks in advance.
Do with a two dimentional array as follows:
arr[filter][tasks];
let the filter values be binary strings converted to int
say you have two filters as aforementioned "rainy" and "free", the possible vaues are
00 - 0(decimal value 0) - not rainy, not free
01 - 1(decimal value 1) - not rainy, free
10 - 2(decimal value 2) - rainy, not free
11 - 3(decimal value 3) - rainy, free
when user checks both the values, then you have 1 and 1. Now convert binary 11 to integer which would be your first index. Get the random second index for the task.
You can have any number of filters
Hint: Integer.parseInt(binary_str, 2) for binary to int conversion.
I have updated this question(found last question not clear, if you want to refer to it check out the reversion history). The current answers so far do not work because I failed to explain my question clearly(sorry, second attempt).
Goal:
Trying to take a set of numbers(pos or neg, thus needs bounds to limit growth of specific variable) and find their linear combinations that can be used to get to a specific sum. For example, to get to a sum of 10 using [2,4,5] we get:
5*2 + 0*4 + 0*5 = 10
3*2 + 1*4 + 0*5 = 10
1*2 + 2*4 + 0*5 = 10
0*2 + 0*4 + 2*5 = 10
How can I create an algo that is scalable for large number of variables and target_sums? I can write the code on my own if an algo is given, but if there's a library avail, I'm fine with any library but prefer to use java.
One idea would be to break out of the loop once you set T[z][i] to true, since you are only basically modifying T[z][i] here, and if it does become true, it won't ever be modified again.
for i = 1 to k
for z = 0 to sum:
for j = z-x_i to 0:
if(T[j][i-1]):
T[z][i]=true;
break;
EDIT2: Additionally, if I am getting it right, T[z][i] depends on the array T[z-x_i..0][i-1]. T[z+1][i] depends on T[z+1-x_i..0][i-1]. So once you know if T[z][i] is true, you only need to check one additional element (T[z+1-x_i][i-1]) to know if T[z+1][i-1] will be true.
Let's say you represent the fact whether T[z][i] was updated by a variable changed. Then, you can simply say that T[z][i] = changed && T[z-1][i]. So you should be done in two loops instead of three. This should make it much faster.
Now, to scale it - Now that T[z,i] depends only on T[z-1,i] and T[z-1-x_i,i-1], so to populate T[z,i], you do not need to wait until the whole (i-1)th column is populated. You can start working on T[z,i] as soon as the required values are populated. I can't implement it without knowing the details, but you can try this approach.
I take it this is something like unbounded knapsack? You can dispense with the loop over c entirely.
for i = 1 to k
for z = 0 to sum
T[z][i] = z >= x_i cand (T[z - x_i][i - 1] or T[z - x_i][i])
Based on the original example data you gave (linear combination of terms) and your answer to my question in the comments section (there are bounds), would a brute force approach not work?
c0x0 + c1x1 + c2x2 +...+ cnxn = SUM
I'm guessing I'm missing something important but here it is anyway:
Brute Force Divide and Conquer:
main controller generates coefficients for say, half of the terms (or however many may make sense)
it then sends each partial set of fixed coefficients to a work queue
a worker picks up a partial set of fixed coefficients and proceeds to brute force its own way through the remaining combinations
it doesn't use much memory at all as it works sequentially on each valid set of coefficients
could be optimized to ignore equivalent combinations and probably many other ways
Pseudocode for Multiprocessing
class Controller
work_queue = Queue
solution_queue = Queue
solution_sets = []
create x number of workers with access to work_queue and solution_queue
#say for 2000 terms:
for partial_set in coefficient_generator(start_term=0, end_term=999):
if worker_available(): #generate just in time
push partial set onto work_queue
while solution_queue:
add any solutions to solution_sets
#there is an efficient way to do this type of polling but I forget
class Worker
while true: #actually stops when a stop work token is received
get partial_set from the work queue
for remaining_set in coefficient_generator(start_term=1000, end_term=1999):
combine the two sets (partial_set.extend(remaining_set))
if is_solution(full_set):
push full_set onto the solution queue