Inspired by Tad

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 Z Z as shown in the image below.

Let f ( Z ) f(Z) be the sum of x x and y y coordinate of the point numbered Z Z . What is the value of f ( 6664001 ) + f ( 8875172 ) + f ( 4820669 ) f(6664001)+f(8875172)+f(4820669) ?

Clarification

As an explicit example, the point Z = 3 Z=3 is ( 0 , 1 ) (0,1) , hence f ( 3 ) = 0 + 1 = 1 f(3)=0+1=1 .


Inspiration


The answer is 4920.

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.

4 solutions

Mark Hennings
Jun 26, 2018

The pattern of the spiral (right 1, up 1, left 2, down 2, right 3, up 3, left 4, down 4, right 5, up 5, left 6, up 6, ...) is easy to solve. The N N th point has coordinates P ( N ) P(N) , where P ( 2 n ( 2 n + 1 ) + j ) = { ( n + j , n ) 0 j 2 n + 1 ( n + 1 , j 3 n 1 ) 2 n + 2 j 4 n + 2 ( 5 n + 3 j , n + 1 ) 4 n + 3 j 6 n + 4 ( n 1 , 7 n + 5 j ) 6 n + 5 j 8 n + 5 P\big(2n(2n+1)+j\big) \; = \; \left\{ \begin{array}{lll} (-n+j,-n) & \hspace{1cm}& 0 \le j \le 2n+1 \\ (n+1,j-3n-1) & & 2n+2 \le j \le 4n+2 \\ (5n+3-j,n+1) & & 4n+3 \le j \le 6n+4 \\ (-n-1,7n+5-j) & & 6n+5 \le j \le 8n+5 \end{array} \right. for all n 0 n \ge 0 . Thus, for n 0 n \ge 0 , f ( 2 n ( 2 n + 1 ) + j ) = { j 2 n 0 j 4 n + 2 6 n + 4 j 4 n + 3 j 8 n + 5 f\big(2n(2n+1) + j\big) \; = \; \left\{ \begin{array}{lll} j-2n & \hspace{1cm} & 0 \le j \le 4n+2 \\ 6n+4 - j & & 4n+3 \le j \le 8n+5 \end{array} \right. We calculate N n j f 6664001 1290 5021 2441 8875172 1489 3710 732 4820669 1097 4839 1747 \begin{array}{cccc}\hline N & n & j & f \\ \hline 6664001 & 1290 & 5021 & 2441 \\ 8875172 & 1489 & 3710 & 732 \\ 4820669 & 1097 & 4839 & 1747 \\ \hline \end{array} making the answer 4920 \boxed{4920} .

can you please explain how did you arrived at finding P(x)?

Siddharth Sharma - 4 weeks ago

Log in to reply

Look at the pattern. Starting at the origin, the line moves 1 right, 1 up, 2 left 2 down (total of 6), 3 right, 3 up, 4 left 4 down (total of 14, so now have had 20 moves), 5 right, 5 up, 6 left, 6 down (total of 22, so now we have had 42 moves), and so on.

Thus the n n th stage of this process (starting with n = 0 n=0 ) involves moving 2 n + 1 2n+1 right, 2 n + 1 2n+1 up, 2 n + 2 2n+2 left, 2 n + 2 2n+2 down. There have been 2 n ( 2 n + 1 ) 2n(2n+1) moves already, so by the end of the n n th stage there have been 2 n ( 2 n + 1 ) + 2 ( 2 n + 1 ) + 2 ( 2 n + 2 ) = ( 2 n + 2 ) ( 2 n + 3 ) 2n(2n+1) + 2(2n+1) + 2(2n+2) = (2n+2)(2n+3) moves. The n n th stage specifies P ( 2 n ( 2 n + 1 ) + j ) P(2n(2n+1)+j) for 1 j 8 n + 6 1 \le j \le 8n+6 although, for convenience, and to include the starting p[oint ( 0 , 0 ) (0,0) , the final move of any stage is shown as the starting j = 0 j=0 position of the following stage.

Mark Hennings - 4 weeks ago

Log in to reply

thanks for the detailed explaination!

Siddharth Sharma - 3 weeks, 6 days ago
Abdelhamid Saadi
Dec 21, 2016

Solution implemented in python 3

 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
"Inspired by Tad"

def add(a,b):
    return (a[0]+b[0],a[1]+b[1])

def solve():
    enws = [(1,0),(0,1),(-1,0),(0,-1)]
    target = [6664001,8875172,4820669]
    res = 0
    current = (0,0)
    n = max(target)
    pas = 1
    spas = 0
    sens = 0
    k = 1
    toggle = False
    while k <= n:
        if spas < pas:
            current = add(current,enws[sens])
            if k in target:
                res += sum(current)
            spas +=1
            k += 1
        else:
            spas=0
            if toggle:
                pas += 1
            toggle = not toggle
            sens =  (sens + 1)%4
    return res

print(solve())

Output

1
4920

As a programming problem:

δ = ( 1 0 0 1 1 0 0 1 ) \delta =\left( \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ -1 & 0 \\ 0 & -1 \\ \end{array} \right)

interesting = { 6664001 , 8875172 , 4820669 } \text{interesting}=\{6664001,8875172,4820669\}

max = max ( interesting ) \max =\max (\text{interesting})

position = { 0 , 0 } ; direction = 0 ; distance = 1 ; step = distance ; z = 1 ; first = True ; While [ z max , ++ z ; If [ MemberQ [ interesting , z ] , Print [ { z , position , Plus@@position } ] ] ; position = δ [ [ direction + 1 ] ] + position ; –step ; If [ step = 0 , direction = ( ( direction + 1 ) m o d 4 ) ; If [ ¬ first , ++distance ] ; first = ¬ first ; step = distance ; ] ] \text{position}=\{0,0\}; \\ \text{direction}=0; \\ \text{distance}=1; \\ \text{step}=\text{distance}; \\ z=-1; \\ \text{first}=\text{True}; \\ \text{While}[z\leq \max , \\ \text{++}z; \\ \text{If}[\text{MemberQ}[\text{interesting},z],\text{Print}[\{z,\text{position},\text{Plus}\text{@@}\text{position}\}]]; \\ \text{position}=\delta [[\text{direction}+1]]+\text{position}; \\ \text{--}\text{step}; \\ \text{If}[\text{step}=0, \\ \text{direction}=((\text{direction}+1) \bmod 4); \\ \text{If}[\neg \text{first},\text{++}\text{distance}]; \\ \text{first}=\neg \text{first}; \\ \text{step}=\text{distance};]]

Mykyta Shvets
Jul 12, 2018

We can find a pattern in the top-left corners of the diagram. Their numbers count 0 , 4 , 16 , 36 , 64 , 100... 0, 4, 16, 36, 64, 100... , which are the squares of even numbers 0 2 , 2 2 , 4 2 , 6 2 , 1 0 2 . . . 0^2, 2^2, 4^2, 6^2, 10^2... . Now, if we find the biggest even square S S less than Z Z , then the coordinates of the point S S will be ( 1 2 S , 1 2 S ) (-\frac{1}{2}\sqrt S, \frac{1}{2}\sqrt S) . Starting at this point we move around the rectangle for ( Z S ) (Z-S) moves and get to the point Z Z .

 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
import math

# A supplementary function, goes around and around from any point and square size
def ineff_f(Z, size=0, px=0, py=0):
    # 'size' is a side of the square already filled
    # (px, py) are coordinates of the pointer
    # Move the pointer Z times
    for n in range(0, Z):
        # Move around the square
        if py == -size and px < size + 1:
            px += 1
        elif px == -size and py != size + 1:
            py -= 1
        elif px == size + 1 and py < size + 1:
            py += 1
        else:
            px -= 1
        if px == -size - 1 and py == size + 1:
            # If a new square is done, increase the size
            size += 1
    # Return pointer coordinates
    return px, py


# The function which substracts the square from Z and uses ineff_f() for any moves left
def eff_f(Z):
    # Find the biggest even square less than Z
    root = math.floor(math.sqrt(Z))
    root = int(root % 2 == 0 and root or root - 1)
    # Run ineff_f() with initial parameters
    return ineff_f(Z - root**2, root // 2, - root // 2, root // 2)


# Finding the answer to the question
result = sum(eff_f(6664001)) + sum(eff_f(8875172)) + sum(eff_f(4820669))
print(result)

1
2
4920
[Finished in 0.2s]

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...