Hi,
I am getting a runtime error in the following code. I dont think there is any stack over flow or referencing invalid memory etc.
Can anybody kindly point me to what might be wrong with this?
package primpat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.System.*;
import java.lang.Math.*;
import java.util.*;
class Point
{
long x;
long y;
public Point(long x,long y)
{
this.x=x;
this.y=y;
}
}
public class Main {
/**
* @param args the command line arguments
*/
public static ArrayList visited;
enum dir {
right, up, left, down
};
public static boolean IsPrime(long num) {
if(num==0 ||num==1)
{
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static long GetValueInPrimeAxisOfSquareWithNumber(long SquareNumber) {
long next=1;
if (SquareNumber == 0) {
return 0;
} else if (SquareNumber == 1) {
return 1;
} else {
for(int i=2;i<=SquareNumber;i++)
{
next=next+8*(i-1)+1;
}
return next;
}
}
public static long FindNumber(long LowSquareVal, long HighSquareVal, long LowSquareNumber, long x, long y) {
dir d = dir.up;
long currX = LowSquareNumber;
long currY = 0;
long inc = LowSquareNumber;
long currVal = LowSquareVal;
if (inc >= y && x == currX) {
currY = y;
currVal = LowSquareVal + y;
} else {
currY = inc;
d = dir.left;
currVal += inc;
inc += LowSquareNumber;
}
while (currX != x || currY != y) {
switch (d) {
case up:
if (x == currX && (currY + inc) > y && currY < y) {
currVal += y - currY;
currY = y;
} else {
currY += inc;
currVal += inc;
d = dir.left;
inc++;
}
break;
case left:
if (y == currY && (currX - inc) < x && currX > x) {
currVal += currX - x;
currX = x;
} else {
currX -= inc;
currVal += inc;
d = dir.down;
}
break;
case down:
if (x == currX && (currY - inc) < y && currY > y) {
currVal += currY - y;
currY = y;
} else {
currY -= inc;
currVal += inc;
d = dir.right;
inc++;
}
break;
case right:
if (y == currY && (currX + inc) > x && currX < x) {
currVal += x - currX;
currX = x;
} else {
currX += inc;
currVal += inc;
d = dir.up;
}
break;
}
}
return currVal;
}
public static long getNumberFromCoords(long x, long y) {
long LowSquareNumber = Math.max(Math.abs(y), Math.abs(x));
long LowSquareVal = GetValueInPrimeAxisOfSquareWithNumber(LowSquareNumber);//prime axis= axis containing 1, 10, 27, 52...
long HighSquareVal = GetValueInPrimeAxisOfSquareWithNumber(LowSquareNumber + 1);
return FindNumber(LowSquareVal, HighSquareVal, LowSquareNumber, x, y);
}
public static boolean Contains(Point a)
{
for(int i=0;i<visited.size();i++)
{
if(visited.get(i).x==a.x &&visited.get(i).y==a.y)
{
return true;
}
}
return false;
}
/**
*
* @param a
* @param b
* @return
*/
public static Point GetPrimeDiff(long a, long b)
{
long CurrNum = getNumberFromCoords(a, b);
if(IsPrime(CurrNum))
{
return new Point(a,b);
}
else
{
int index=0;
visited.add(new Point(a,b));
while(!visited.isEmpty())
{
if(!visited.contains(new Point(a-1,b)))
{
if(IsPrime(getNumberFromCoords(a-1, b))) {
return new Point(a-1,b);
}
else
{
visited.add(new Point(a-1,b));
}
}
if(!visited.contains(new Point(a+1,b)))
{
if(IsPrime(getNumberFromCoords(a+1, b))) {
return new Point(a+1,b);
}
else
{
visited.add(new Point(a+1,b));
}
}
if(!visited.contains(new Point(a,b-1)))
{
if(IsPrime(getNumberFromCoords(a, b-1))) {
return new Point(a,b-1);
}
else
{
visited.add(new Point(a,b-1));
}
}
if(!visited.contains(new Point(a,b+1)))
{
if(IsPrime(getNumberFromCoords(a, b+1))) {
return new Point(a,b+1);
}
else
{
visited.add(new Point(a,b+1));
}
}
index++;
a=visited.get(index).x;
b=visited.get(index).y;
}
}
return null;
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int numTestCases = Integer.parseInt(reader.readLine());
String s = new String();
visited = new ArrayList() {};
for (int i = 0; i < numTestCases; i++) {
s = reader.readLine();
s = s.trim();
String[] arr = s.split(" ");
long a = Integer.parseInt(arr[0]);
long b = Integer.parseInt(arr[1]);
Point p = GetPrimeDiff(a,b);
System.out.println(Math.abs(a-p.x)+Math.abs(b-p.y));
}
}
}
. But when a user is very active, there can be a lot of recent activities, and try to search between the different codes can be boring when adding a link is a lot easier. I don’t know if it was the case this time, I have forgotten to verify before, sorry 