Z as shown in the image below.
Consider the rectangular spiral in the image below. It starts from the origin and twirls and twirls forever in an anticlockwise direction along the integer coordinates of the Cartesian coordinate plane.Each point along the spiral is numbered with an integer
What is the value of the integer Z at the coordinate ( 1 2 , − 2 2 ) ?
Details and assumptions
As an explicit example Z is 1 0 for ( 2 , 0 ) , 3 for ( 0 , 1 ) and 1 0 for ( 2 , 0 ) .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Nice observation. I couldn't think of any simple pattern of changing directions and ended up implementing a complicated algorithm.
Nice solution and nice problem. Keep posting more.
Seriously, nicely observed.
you're a programer!!!
java solution
class Temp {
public static void main(String... s)
{
int x = 0,y = 0;
int cx = 0,cy = 0;
int axis_x=12,axis_y=-22;
int count = 0;
for(int i = 1;;i++)
{
x=i;
y=i;
if(x%2!=0&&(cx!=axis_x||cy!=axis_y))
{
for(int j = 0 ; j < x; j++)
{
cx++;
count++;
System.out.println("count="+count+",i="+i);
if(cx==axis_x)
{
break;
}
}
}
else if(x%2==0&&(cx!=axis_x||cy!=axis_y))
{
for(int j = 0 ; j < x; j++)
{
cx--;
count++;
System.out.println("count="+count+",i="+i);
if(cx==axis_x)
{
break;
}
}
}
if(y%2!=0&&(cx!=axis_x||cy!=axis_y))
{
for(int k = 0 ; k < y; k++)
{
cy++;
count++;
System.out.println("count="+count+",i="+i);
if(cy==axis_y)
{
break;
}
}
}
else if(y%2==0&&(cx!=axis_x||cy!=axis_y))
{
for(int k = 0 ; k < y; k++)
{
cy--;
count++;
System.out.println("count="+count+",i="+i);
if(cy==axis_y)
{
break;
}
}
}
System.out.println("x="+cx);
System.out.println("y="+cy);
System.out.println("Xcount="+count+",i="+i);
if(cx==axis_x&&cy==axis_y)
break;
}
}
}
Great job ! Much better than my code :D
@Thaddeus Abiy I wanted to thank you for letting us know that Google Code Jam has started.
I read your status and was able to submit the problems just a couple of hours before the deadline. It was very interesting. How much did you scored in the Qualification Round?
You are welcome.Unfortunately the power went out as soon I did the first two problems and it didn't come the whole night.I was able to scrape a mere 25 points and qualify. The problems were very interesting though..It would be great if someone shared them here.
It is so dumb to write a computer program and have the computer solve it for you just by brute force enumeration. At least the mathematicians were intelligent enough to find a formula to calculate it by themselves
It seems you are at a wrong place.Computer Science is not your cup of tea
Let X = Z + 1 .
X = ( 2 ⋅ 0 + 1 ) 2 for ( 0 , 0 )
X = ( 2 ⋅ 1 + 1 ) 2 for ( 1 , − 1 )
X = ( 2 ⋅ 2 + 1 ) 2 for ( 2 , − 2 )
…
X = ( 2 ⋅ 2 2 + 1 ) 2 = 2 0 2 5 for ( 2 2 , − 2 2 )
X = 2 0 2 5 − 1 0 = 2 0 1 5 for ( 1 2 , − 2 2 )
Answer is 2 0 1 5 − 1 = 2 0 1 4
Can you please explain why multiplying every first value with 2? Like (2.0+1)^2 & (2.1+1)^2 etc. Inside every bracket, how "2" came, any rules? Thanks.
Nice!
Diag (1,0), (2,-1),(3,-2)....(23,-22) say (n+1, -n)
Int Z at points [n+1- ( - n)]^2 i.e. (2n+1)^2
z(23,-22)=[ 2x22 + 1]^2= 45x45= 2025
so, z(12,-22)= z(23,-22)-11= 2025-11= 2014 [ current year ;) ]
Exactly !!!
My-Solution in Python :
moves=[(1,0),(0,1),(-1,0),(0,-1)]
def SquareSpiral(x,y):
z=0; i=0
while True:
for j in range(int(i/2)+1):
if (x,y)==(0,0):
return z
z+=1
x-=moves[i%4][0]
y-=moves[i%4][1]
i+=1
print(SquareSpiral(12,-22))
Python + Modular Arithmetic = The Shortest Working Code for Solving this Problem !
Nice Problem.
Here's my solution in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
I was very surprised to see that this bizarre looking code actually worked and that too in the first attempt. This is the story most of the time. It works when you actually didn't expect it to work at all and sometimes it abruptly fails when you are dead sure that it will work.
I think I was able to solve the problem because of spirals. Hhaha!
(-n, -n) => 2x1+2x2+2x3+...+4n = 2(1+2+...+2n) = 2n(1 + 2n)
(-22, -22) => 2(1+...+44) = 44(1+44) = 1980
D = (12, -22) - (-22, -22) = (34, 0)
Z = 1980 + 34 = 2014
Spiral sp=new Spiral(0,0,100); int x=sp.getX(); int y=sp.getY(); int z=0;
for(int i=1;i<=sp.getZ();i++)
{
for(int j=1;j<=i;j++)
{
z++;
x++;
if(x==12 && y==-22)
{
System.out.println("Z = "+z);
}
}
for(int j=1;j<=i;j++)
{
z++;
y++;
}
i++;
for(int j=1;j<=i;j++)
{
z++;
x--;
if(x==12 && y==-22)
{
System.out.println("Z = "+z);
}
}
for(int j=1;j<=i;j++)
{
z++;
y--;
}
}
Problem Loading...
Note Loading...
Set Loading...