Lucky Number 13 Part 2

Imagine you arrange the thirteen integers 1 1 to 13 13 in a row and label them a 1 , a 2 , , a 13 a_1, a_2, \ldots, a_{13} from left to right under the following conditions:

  • a n + 2 > a n a_{n+2} > a_n for n = 1 , 2 , , 11. n = 1, 2, \ldots, 11.
  • a n a_n is either n 1 , n , n - 1, n, or n + 1 n + 1 for all n = 1 , 2 , , 13. n=1, 2, \ldots, 13.

How many such arrangements are possible?


Inspiration


The answer is 377.

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.

5 solutions

As Fabricio notes, only the second constraint need be considered, as the first follows naturally: a n n + 1 a n + 2 a_n \le n + 1 \le a_{n + 2} , and equality cannot hold on both sides. Therefore, such an arrangement is a permutation of the normal numerical order in which each number is either in its original position or has been swapped with its neighbor on either side. For example,

( 1 , 2 , 4 , 3 , 5 , 7 , 6 , 9 , 8 , 11 , 13 , 12 ) (1, 2, 4, 3, 5, 7, 6, 9, 8, 11, 13, 12) .

More generally, the question then becomes: how many disjoint sets of pairs of consecutive numbers can be chosen from the set { 1 , 2 , , n } \{1, 2, \dots, n\} ? These will be the pairs that are swapped to create an arrangement. (In our situation n = 13 n = 13 , of course.) Denote this number by P n P_n . By direct computation we have P 1 = 1 , P 2 = 2 P_1 = 1, P_2 = 2 . For n > 2 n > 2 , such a permutation either fixes 1 1 or swaps it with 2 2 . If 1 1 is fixed, there are P n 1 P_{n - 1} possible arrangements for the remaining n 1 n - 1 numbers. If 1 1 and 2 2 are swapped, there are P n 2 P_{n - 2} possible arrangements for the remaining n 2 n - 2 numbers. This means

P n = P n 1 + P n 2 P_n = P_{n - 1} + P_{n - 2} ,

i.e. this sequence is nothing more than our old friend the Fibonacci sequence! Specifically P n = F n + 1 P_n = F_{n + 1} . Therefore the solution for the given problem is P 13 = F 14 = 377 P_{13} = F_{14} = 377 . \quad \square

Ah, just what I was thinking, William... Nice explanation. You beat me to it! :-)

Geoff Pilling - 2 years, 8 months ago

Great solution!

David Vreken - 2 years, 8 months ago

Yay, thanks for acknowledging me :D

Fabricio Kolberg - 2 years, 8 months ago

Log in to reply

Credit where it's due!

William Allbritain - 2 years, 8 months ago

Nice solution!

Varsha Dani - 2 years, 8 months ago

I got the correct answer 377. I did some unnecessary thing.

Srikanth Tupurani - 2 years, 8 months ago
Fabricio Kolberg
Oct 3, 2018

Consider the second constraint only. We won't need the first, because it is implied by the second:

The maximum value a n a_n can take, for any n = 1 , 2 , . . . , 11 n = 1,2,...,11 , is n + 1 = n + 2 1 n+1=n+2-1 , which, once taken, limits the possible values for a n + 2 a_{n+2} to n + 2 n+2 and n + 2 + 1 n+2+1 , implying a n + 2 a_{n+2} > a n a_n .

Note that:

  • There are two possible positions for the number 1. Also, if a 2 = 1 a_2=1 , then a 1 = 2 a_1=2 necessarily.

  • There are two possible positions for 2 if a 1 = 1 a_1=1 , and one if a 2 = 1 a_2=1 .

In general, after the positions for the first k k numbers are picked, we have k k numbers distributed among the first k + 1 k+1 spaces of the list, and two things may
happen:

  • a k a_k is not occupied, in which case, there is only one possibility for k + 1 k+1 , as it has to be in a k a_k otherwise a k a_k will be empty (as all its possible values will be taken).
  • a k a_k is occupied, in which case, there are two possibilities for k + 1 k+1 ( a k + 1 a_{k+1} and a k + 2 a_{k+2} ). That is, until we reach 13, in which case, there can be only one spot left.

How, then, can we find a closed expression for the number of possibilities with such a strange tree of ramifications? I don't know for certain, so I wrote a bit of code to count for me, which gave me 377. The code itself is not the prettiest, but hopefully it is understandable.

 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int possibilities=0;
int a[13];    
void count_possibilities(int item);

int main(){
    int i; // start by initializing the array
    for(i=0;i<13;i++) a[i]=-1;
    count_possibilities(1); // first recursive call
    printf("%d",possibilities); // prints result
}

void count_possibilities(int item){
   // there are two possibilities for number 1
    if(item == 1){
        a[0]=1;
        count_possibilities(2);
        a[0]=-1;
        a[1]=1;
        count_possibilities(2);
        a[1]=-1;
    }else{
        if(item == 13){
            possibilities++; // there is only one possibility for 13 if you got this far
        }else{
            if(a[item-2]==-1){ // if the position ak-1 is not occupied, then k needs to go to ak-1, 
                                        // otherwise ak-1 will remain empty 
                a[item-2]=item;
                count_possibilities(item+1);
                a[item-2]=-1;
            }else{ // testing the two possibilities that remain when ak-1 is taken
                a[item-1]=item;
                count_possibilities(item+1);
                a[item-1]=-1;
                a[item]=item;
                count_possibilities(item+1);
                a[item]=-1;
            }
        }       
    }
} 

Try altering your code to find the number of possibilities when there is only 1 integer in the list (instead of 13), then 2, then 3, then 4, and so on, and hopefully a familiar sequence will present itself.

David Vreken - 2 years, 8 months ago

Log in to reply

You're right. Sorry, I was lazy af.

Fabricio Kolberg - 2 years, 8 months ago

Log in to reply

Lazy? That program looks like more work than I did.

Jeremy Galvagni - 2 years, 8 months ago

Log in to reply

@Jeremy Galvagni I was mentally lazy, so I adopted a mechanical solution. But yeah, it depends on how laziness is defined.

Fabricio Kolberg - 2 years, 7 months ago

Log in to reply

@Fabricio Kolberg I guess that shows how programmers see the world.

Jeremy Galvagni - 2 years, 7 months ago

Log in to reply

@Jeremy Galvagni Not sure if I should be ashamed or proud xD

Fabricio Kolberg - 2 years, 7 months ago
Varsha Dani
Oct 4, 2018

Can you say Fibonacci?

Yup. Brilliant problem. Brilliant Allbritain solution/explanation (above). The problem should have immediately triggered a thought something like, "Fibonacci something something...but wtf, how exactly?" But of course it didn't until someone pointed it out.

Douglas Wham - 2 years, 7 months ago
Harsh Parikh
Oct 18, 2018

As a CS grad student, I start every problem with the naive solution and optimize it to get efficient ones.

As a habit, I first thought about the solution in the following way :

Generate all possible permutations (13!) and check for the constraints whether they are satisfied or not and increment a counter every time it does.

As you can see,It is horrifyingly inefficient.

So, then I solved the Inspiration problem first, without taking help of any Intel chips. and reached the answer : 1716

Now all I had to do is generate all these (1716) possibilities and check for the constraints which could be done in O(1).

The following is the code which shall do (Output : all the valid arrangements followed by the count of it).

import itertools as it
A = list(it.combinations(range(1,14),7))
count=0
for a in A:
    i=1
    for aa in a:
        if(aa == (i-1) or aa == (i) or aa == (i+1)):
            i=i+2
        else:
            break
    if(i==15):
        count=count+1
        print (a)
print (count)
K T
Oct 19, 2018

Consider the general case for m integers 1..m, with boundary conditions n=1...m-2 in the first and n=1...m in the second condition. Let f(m) be the number of such arrangements. if we fix a m = m a_m=m , we observe that there are f(m-1) possibilities for the arrangement before it. The only other option is a m = m 1 a_m=m-1 , implying that a m 1 = m a_{m-1}=m , so that there are f(m-2) possibilities for the arrangement before that. So f(m)=f(m-2)+f(m-1). Together with the starting condition f(1)=1, f(2)=2 it is the fibonacci sequence, so f(13)=377.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...