Tuesday, October 30, 2018

Kariakoo Algorithm

Kariakoo dance asks every performer to follow a precise sequence of steps:
• Stage 0 : First, get away from obstacles by setting up your starting point at position 0
• Stage 1 : Take one step forward (+1 step)
• Stage 2 : Take two steps backward (-2 steps)
• To follow, the steps as well as the direction you will have to take in your next move will each time be obtained thanks to a specific calculation: the number of steps you took on the previous stage minus the number of steps you took on the penultimate stage.
That is, on stage 3, a dancer will have to take 3 steps backwards (-2 - 1).
At stage 3, the dancer is at position -4.
stage(n) = stage(n-1) - stage(n-2)
pos(n) = pos(n-1) + stage(n)
At stage 4, the dancer is at position -5.
enter image description here

Solution:

public class Kariakoo {
static final int NUMBER_OF_POSSIBLE_POSITIONS = 6;
final static int[] POSSIBLE_POSITIONS = { 0, 1, -1, -4, -5, -3 };
/**
* Computes the position of the Kariakoo​​​​​​​‌​‌‌​​​​​​‌​​‌‌​‌​​‌‌‌​‌
* dancer.
*/
static int getPositionAt(int n) {
return POSSIBLE_POSITIONS[n % NUMBER_OF_POSSIBLE_POSITIONS];
}
public static void main(String[] args) {
System.out.println(Kariakoo.getPositionAt(3)); // -4
System.out.println(Kariakoo.getPositionAt(100000)); // -5
System.out.println(Kariakoo.getPositionAt(2147483647)); // 1
}
}

No comments:

Post a Comment