2

I just completed some coding competition, and I was asked to solves this:

Imagine you are playing a board game. You roll a $6$-faced dice and move forward the same number of spaces that you rolled. If the finishing point is $n$ spaces away from the starting point, please implement a program that calculates how many possible ways there are to arrive exactly at the finishing point.

Can anyone explain how to solve this with a mathematical algorithm?

Masacroso
  • 30,417
  • What did you try/what kind of solution are you looking for? It would be fairly simple to solve this recursively, for example. Or are you looking for a closed-form solution? – Tez LaCoyle Jul 28 '17 at 14:54
  • Compare this to the question of perfectly tiling an $1\times n$ board using $1\times 1, 1\times 2, 1\times 3,\dots, 1\times 6$ tiles. This should be a very standard problem solved with recurrence relations. Let $a_k$ be the number of solutions for if the finishing point were $k$ spaces away. You have then the recurrence $a_k=a_{k-1}+a_{k-2}+\dots+a_{k-6}$ with seed values $a_0=1,a_k=0$ for all $k<0$. – JMoravitz Jul 28 '17 at 14:54
  • is the 6 faced die using the numbers 1 to 6 ? or some other set of 6 numbers ? –  Jul 28 '17 at 14:55
  • @T.Linnell If possible can you please help me and give me both recursive approach as well as the closed form solution, I am having trouble thinking how to approach these kind of problems. – Padmaja Ganesh Jul 28 '17 at 14:55
  • @RoddyMacPhee Its using numbers from 1 to 6 only. – Padmaja Ganesh Jul 28 '17 at 14:56
  • find the partitions of n using the numbers 1-6 ... –  Jul 28 '17 at 14:58
  • Can you give a source/name of the coding competition? – paw88789 Jul 28 '17 at 14:59
  • The recursive solution is as @JMoravitz described: you use the recurrence relation, since to reach square $n$, you must first land on a square from $n-6$ to $n-1$ (assuming a die with faces 1-6). The sequence of numbers $a_{n}$ is a generalisation of the Fibonacci sequence, in this case sometimes called "Hexanacci" because the previous six numbers are summed. There is a closed-form solution, but it isn't that simple - I'm still trying to wrap my head around it. I personally find the recursive solution more intuitive, but it won't be as efficient if you're trying to compute, say, $a_{1000000}$. – Tez LaCoyle Jul 28 '17 at 15:02
  • For reference, your output should look like OEIS sequence A001592, without the initial zeroes and the first 1 (link: https://oeis.org/A001592) – Tez LaCoyle Jul 28 '17 at 15:08
  • 1
    @paw88789 Hackerank – Padmaja Ganesh Jul 28 '17 at 15:10
  • @T.Linnell Thanks a lot, will try recursive approach and post my output. – Padmaja Ganesh Jul 28 '17 at 15:10
  • @Roddy not partitions, just a kind of restricted compositions of $n$. – Masacroso Jul 28 '17 at 15:17
  • First solve it for $n=1$. Then for $n=2$. Then for $n=3$. and so on. Suppose you had already solved it for all values $1, 2, 3, 4, ..., n-1$. How would you then solve it for the value $n$? – Jaap Scherphuis Jul 28 '17 at 15:27

2 Answers2

1

Denote by $H(m)$ the number of histories $$(x_1,x_2,\ldots, x_r),\qquad x_i\in[6]\quad (1\leq i\leq r)$$ of arbitrary length $r\geq1$ that sum up to $m$, i.e., $\sum_{i=1}^r x_i=m$. Then $$H(m)=0\quad(m<0),\quad H(0)=1$$ and $$H(m)=\sum_{k=1}^6 H(m-k)\qquad(m\geq1)\ .$$

1

After a lot of calculation I found a solution that it creates a Hexanacci series. Now let me explain Hexanacci series a little bit. In the Hexanacci series each element is the summation of previous 6 elements. So I achieved this in Objective-C which can be easily convert to any language:

-(void)getHaxanassiSeriesOf:(NSInteger)number
 {
  static unsigned long ways;

  unsigned long first = 0;
  unsigned long second = 0;
  unsigned long third = 0;
  unsigned long fourth = 0;
  unsigned long fifth = 0;
  unsigned long sixth = 1;

  for (int i = 0; i<= number; i++) {

    ways = first + second + third + fourth + fifth + sixth;

    if (i>0) {
        first = second;
        second = third;
        third = fourth;
        fourth = fifth;
        fifth = sixth;
        sixth = ways;
    }

    NSLog(@"%d : -> %ld",i,ways);
}
return ways;}

// Result:

[self getHaxanassiSeriesOf:20];

0 : -> 1
1 : -> 1
2 : -> 2
3 : -> 4
4 : -> 8
5 : -> 16
6 : -> 32
7 : -> 63
8 : -> 125
9 : -> 248
10 : -> 492
11 : -> 976
12 : -> 1936
13 : -> 3840
14 : -> 7617
15 : -> 15109
16 : -> 29970
17 : -> 59448
18 : -> 117920
19 : -> 233904
20 : -> 463968