long to int conversion in java not working - java

I am working on a problem with big number's in java.
int temp =0;
long last = 218212982912L;
temp = (int) last%10;
last = last/10;
for the above line of code I get the
temp = -4
in the first iteration. I am not sure what is the problem. I have tried a lot of solution online available.

Put parentheses around last%10
The cast to int is being applied before the modulus operation

The last positive you can get is 2,147,483,647 and when you are explicitly converting a larger number to int, your will get unpleasant results but if you put extra parentheses like (int) (someLong % 10), first the long operation get executed (which results in smaller long value that fits int memory space) and then you can cast it to int without worry

Related

Find the modo value

I have given a log value Y , i want to calculate the anti log of Y i.e
ans = (Math.pow(10,Y))%mod
where mod = 1e9+7 and the anti log of Y will always be integer i.e Y is calculate as follow Y= log(a) a is very large integer of range 10^100000
So for given Y i need to calculate ans ? How to do that considering the mod operation.
My Approach
double D = Y -(int)Y
long Pow = (long)Y
for(int i=1;i<=Pow;i++) ans = (ans*10)%mod;
ans = (ans*Math.pow(10,D))%mod
But it's not correct can someone suggest be efficient approach here ? BigDecimal can be useful there ?
For Example:
Y = 16.222122660468525
Using the straight forward method and rounding off i.e Math.log(10,Y) give me 1667718169966651 but using loops it's give me 16677181699666510. I am not using mod now just explaining that there is an error.
Here Y is small so direct method works and we can take mod easily. if Y is range of 10000 it will not work and overflow so we have to used mod.
I guess it's should work
double D = Y -(int)Y
long Pow = (long)Y
for(int i=1;i<=Pow;i++) ans = (ans*10)%mod;
ans = (ans*Math.pow(10,D))
ans = Math.round(ans)
ans%=mod
There is an error in your judgement here - the loop method is not at fault.
The value of a in your example has 17 integer digits. From this stackoverflow post, a double has ~16 significant digits of precision. Thus both the loop and direct calculations are in fact being limited by lack of precision.
(Just to confirm, using a high precision calculator, the value of a is 16677181699666650.8689546562984070600381634077.... Thus both of your values are incorrect - unless you copied them wrongly?)
Thus your loop method is not the problem; you just need a
higher-precision method to do the last step (calculating pow(10, frac(Y))).
As a side note, there is a more efficient way of doing the loop part - this post has more details.

Modulus on Combinatorials

Suppose a number n and we have to find sum of all combinatorials of n i.e. nC0+nC1+nC2+...+ nCn.
As result can be large so final answer should be sum%D (D=10^9+7).
Approach I used is-
long sum=0;
long combination=1;
for(int i=1;i<=n;i++){
combination=((combination*(n-i+1))/i)%D;
sum=(sum+combination)%D;
}
But this is not working.
Real Problem statement and code.Code is giving correct output till n=20.
You're supposed to mod the final output, not along every step of the way.
Also the numbers get very large so use BigInteger instead of long.
Here is my solution
http://ide.geeksforgeeks.org/Ew3Epn
Using the formula (2*n)!/(n!)^2
found here -> https://oeis.org/A000984

int(primitive) value not getting typecasted to long(primitive) implicitly

My question appears very simple.
int i =99999;
long square = i*i;
System.out.println(square); //prints 1409865409 - incorrect value
int i = 99999;
long square = (long) i*i;
System.out.println(square); // prints 9999800001 - correct value
It looks to be the range issue of int.
But shouldn't it typecast the product to long implicitly?
What am I missing here?
I know how to use Math.pow(double,double) function and it works perfectly. But
I want to know the reason for above output.
TIA.
In the first case, the result is first computed as an int, and only then converted to a long.
Therefore the wrong result.
In the second case, i is converted to a long before computing the result because (long) (cast to long) has a higher precedence than *.
Therefore the right result.
You have fallen prey to an operator precedence error. This line:
long square = (long) i*i;
actually does this:
long square = ((long) i)*i;
This is important, because when you multiply 99999, you get a number too large to represent as an int. This line:
long square = i*i;
squares i, causing an overflow error, then casts the result to a long and assigns it to square. The other line casts the first factor to a long, forcing it to cast the second factor to a long as well, before the calculation takes place.

How do i store numbers bigger than 10 billion

I am making a program and i need a way to make variables go over 10 billion and int only stores up to 999 million for me so i decided to use a long instead of a int and it turn out it only stores up to 999 million as well.
int TotalWorldPop = 7200000000;
gives me the "literal is out of range" error
long TotalWorldPop = 7200000000;
gives me the "literal is out of range" error as well
but
int TotalWorldPop = 999999999
is ok for me
A long can accommodate numbers as large as 263-1. But there's a trick to putting them into the primitive field.
If you're entering the primitive literal, then you have to add an L at the end, as all numeric literals are treated as int (and it can only go up to ~2.1 billion).
If you need numbers larger than that, use BigInteger.
You could use a BigInteger to store very large numbers.
Example:
Biginteger bigInt1 = new Biginteger("91826581752671985235272769716");
Biginteger bigInt2 = new Biginteger("-1796357891266373473772242");
Biginteger bigint3 = bigInt1.divide(bigInt2);
Biginteger bigint4 = bigInt1.add(bigInt2);

Checking if value of int[] can be long

I have an array of ints ie. [1,2,3,4,5] . Each row corresponds to decimal value, so 5 is 1's, 4 is 10's, 3 is 100's which gives value of 12345 that I calculate and store as long.
This is the function :
public long valueOf(int[]x) {
int multiplier = 1;
value = 0;
for (int i=x.length-1; i >=0; i--) {
value += x[i]*multiplier;
multiplier *= 10;
}
return value;
}
Now I would like to check if value of other int[] does not exceed long before I will calculate its value with valueOf(). How to check it ?
Should I use table.length or maybe convert it to String and send to
public Long(String s) ?
Or maybe just add exception to throw in the valueOf() function ?
I hope you know that this is a horrible way to store large integers: just use BigInteger.
But if you really want to check for exceeding some value, just make sure the length of the array is less than or equal to 19. Then you could compare each cell individually with the value in Long.MAX_VALUE. Or you could just use BigInteger.
Short answer: All longs fit in 18 digits. So if you know that there are no leading zeros, then just check x.length<=18. If you might have leading zeros, you'll have to loop through the array to count how many and adjust accordingly.
A flaw to this is that some 19-digit numbers are valid longs, namely those less than, I believe it comes to, 9223372036854775807. So if you wanted to be truly precise, you'd have to say length>19 is bad, length<19 is good, length==19 you'd have to check digit-by-digit. Depending on what you're up to, rejecting a subset of numbers that would really work might be acceptable.
As others have implied, the bigger question is: Why are you doing this? If this is some sort of data conversion where you're getting numbers as a string of digits from some external source and need to convert this to a long, cool. If you're trying to create a class to handle numbers bigger than will fit in a long, what you're doing is both inefficient and unnecessary. Inefficient because you could pack much more than one decimal digit into an int, and doing so would give all sorts of storage and performance improvements. Unnecessary because BigInteger already does this. Why not just use BigInteger?
Of course if it's a homework problem, that's a different story.
Are you guaranteed that every value of x will be nonnegative?
If so, you could do this:
public long valueOf(int[]x) {
int multiplier = 1;
long value = 0; // Note that you need the type here, which you did not have
for (int i=x.length-1; i >=0; i--) {
next_val = x[i]*multiplier;
if (Long.MAX_LONG - next_val < value) {
// Error-handling code here, however you
// want to handle this case.
} else {
value += next_val
}
multiplier *= 10;
}
return value;
}
Of course, BigInteger would make this much simpler. But I don't know what your problem specs are.

Categories

Resources