Random Problem
-
- Posts: 16
- Joined: Fri Jul 13, 2007 12:21 am
- Location: Waterloo
Random Problem
I don't know Java much... I did mostly Scheme, and a bit of C.
Anybody care to explain why -2^31 failed?
Thx
Anybody care to explain why -2^31 failed?
Thx
-
- Posts: 16
- Joined: Fri Jul 13, 2007 12:21 am
- Location: Waterloo
i think you've mistaken with another challenge
public int bucketFromRandom(int randomNumber) {
int a[] = new int[10];
for (int i = 0; i < a.length; i++)
a = i * randomNumber;
int index = Math.abs(randomNumber) % a.length;
return a[index];
}
this has no reference to an actual random number. i just tried every int value possible, and -2^31 gave me an error, which i don't know why it's the answer.
public int bucketFromRandom(int randomNumber) {
int a[] = new int[10];
for (int i = 0; i < a.length; i++)
a = i * randomNumber;
int index = Math.abs(randomNumber) % a.length;
return a[index];
}
this has no reference to an actual random number. i just tried every int value possible, and -2^31 gave me an error, which i don't know why it's the answer.
- PainKeeper
- Posts: 4
- Joined: Tue Jul 19, 2011 7:30 pm
- Location: Germany
i did it with this little code sniped. there some smaller / better way to do?
im not some java pro.
[code]
class Box
{
int zahl;
Box(int randomNumber) { zahl = randomNumber; }
int value() {
int a[] = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = i * zahl;
int index = Math.abs(zahl) % a.length;
try { return a[index]; } catch (ArrayIndexOutOfBoundsException e)
{ System.out.println("Exeption: " + zahl); }
return a[index];
}
public static void main(String[] args)
{
int vol;
for ( int faktor = 1; faktor <= Integer.MAX_VALUE; faktor ++ ) {
Box myTest1 = new Box (faktor);
vol = myTest1.value();
}
}
}
[/code]
javac filename.class
java Box
output:
[quote]
Exeption: -2147483648
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -8
at Box.value(rndtest.java:12)
at Box.main(rndtest.java:20)
[/quote]
im not some java pro.
[code]
class Box
{
int zahl;
Box(int randomNumber) { zahl = randomNumber; }
int value() {
int a[] = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = i * zahl;
int index = Math.abs(zahl) % a.length;
try { return a[index]; } catch (ArrayIndexOutOfBoundsException e)
{ System.out.println("Exeption: " + zahl); }
return a[index];
}
public static void main(String[] args)
{
int vol;
for ( int faktor = 1; faktor <= Integer.MAX_VALUE; faktor ++ ) {
Box myTest1 = new Box (faktor);
vol = myTest1.value();
}
}
}
[/code]
javac filename.class
java Box
output:
[quote]
Exeption: -2147483648
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -8
at Box.value(rndtest.java:12)
at Box.main(rndtest.java:20)
[/quote]
-
- Posts: 106
- Joined: Thu Oct 29, 2009 9:21 pm
-
- Posts: 8
- Joined: Mon Nov 21, 2011 9:15 pm
Hello,
I suppose Math.abs(x) returns -x if x is negative and x otherwise.
In two's complement, -x is the same as ~x+1 (inverse all the bits of x and add 1).
But -2^31 is represented by 0x80000000; it stays the same when you apply the algorithm, as 0 does.
So --2^31 is still -2^31.
And as gfoot said, +2^31 has no possible representation in 32 bits two's complement system.
You can verify it with this code:
Add to that, the modulo operation keeps the sign of the left operand, and you have your answer.
I brute-forced it too, by the way.
I suppose Math.abs(x) returns -x if x is negative and x otherwise.
In two's complement, -x is the same as ~x+1 (inverse all the bits of x and add 1).
But -2^31 is represented by 0x80000000; it stays the same when you apply the algorithm, as 0 does.
So --2^31 is still -2^31.
And as gfoot said, +2^31 has no possible representation in 32 bits two's complement system.
You can verify it with this code:
Code: Select all
public class Abs{
public static void main(String args[]){
if(args.length!=1){
System.out.println("Usage: java Abs number");
return;
}
int i=Integer.parseInt(args[0]);
System.out.println("Input: " + i);
System.out.println("Absolute: " + Math.abs(i));
System.out.println("Negative: " + (-i));
}
}
I brute-forced it too, by the way.