# Captain Prime – HackerRank Solution

In this post, we will solve Captain Prime HackerRank Solution. This problem (Captain Prime) is a part of HackerRank Functional Programming series.

Captain Prime is going on a trip to Primeland and needs support of his troops to make this voyage successful. To prevent the wrath of evil spirits, he has to throw out some people from his troop into the sea. This decision will depend on the identification number of the troop member.

His ship is divided into three parts: Left, right, and central. Every person on the ship is assigned an identification number (referred as id), and according to their id they get to work in one part of the ship, or end up getting thrown out of the ship.

A person’s fate depends on the following conditions:

• CENTRAL: He will be working in central part if (a) his id is a prime number, (b) it doesn’t contain 0 as one of the digits, and (c) when the left digits are successively taken off, then all the resulting numbers are also prime. (d) And same goes for the digits on the right side. For example person with id 3137 will work in central part, as 3137, {313, 31, 3}, {137, 37, and 7} are all prime numbers.
• LEFT: He will be working in left part if (a) his id is a prime number, (b) and doesn’t contain 0 as one of the digits. (c) Also when the left digits are successively taken off, then all the resulting numbers are prime, but this doesn’t hold true for the right digits. For example, person with id 1367 will work here, since 1367, 367, 67 and 7 are all prime numbers. While 136 is not a prime number, which we get after removing one digit on the right.
• RIGHT: He will be working on right part if (a) his id is a prime number, (b) and doesn’t contain 0 digit as one of the digits. (c) Also on successively stripping right digits, all the resulting numbers are prime, but this does not hold true for the left digits. For example, person with id 2333 belongs to this category, as 2333, 233, 23 and 2 are all prime numbers, while 333 is not a prime number.
• DEAD: If a person is not eligible to work anywhere on the ship, then he will be thrown out of the ship. Sad!

## Input Format

The first line contains T, the number of persons on the ship, followed by the their id numbers in the next T lines.

## Output Format

Print `LEFT``RIGHT``CENTRAL`, or `DEAD` according to the fate of the person on the ship.

## Constraints

• 1 <= T <= 50
• 1 <= id <= 106

Sample Input #00

``````5
3137
1367
2333
101
12``````

Sample Output #00

``````CENTRAL
LEFT
RIGHT

Sample Input #01

``````4
43
23
66
29``````

Sample Output #01

``````LEFT
CENTRAL
RIGHT``````

## Solution – Captain Prime – HackerRank Solution

Scala

```import java.util.Scanner

object Solution {
def toDigits(p: Int): List[Int] = {
var current = p
(0 until 7).map(_ => {
val res = current % 10
current /= 10
res
})
.reverse
.dropWhile(_ == 0)
.toList
}

def fromDigits(seq: Seq[Int]): Int = {
def innerFromDigits(seq: Seq[Int]): Int = seq match {
case Seq(v) => v
case _ => 10 * innerFromDigits(seq.tail) + seq.head
}

innerFromDigits(seq.reverse)
}

@scala.annotation.tailrec
def isRight(n: Int): Boolean = n == 0 || n % 10 != 0 && BigInt(n).isProbablePrime(5) && isRight(n / 10)

def isLeft(n: Int): Boolean = n == 0 || {
@scala.annotation.tailrec
def innerIsLeft(digits: List[Int]): Boolean = digits.isEmpty ||
BigInt(fromDigits(digits)).isProbablePrime(5) && innerIsLeft(digits.tail)

val digits = toDigits(n)
!digits.contains(0) && innerIsLeft(digits)
}

def solve(n: Int): String = (isLeft(n), isRight(n)) match {
case (false, true) => "RIGHT"
case (true, false) => "LEFT"
case (true, true) => "CENTRAL"
}

def main(args: Array[String]): Unit = {
val sc = new Scanner(System.in)

val t = sc.nextInt
(0 until t).foreach(_ => {
val n = sc.nextInt
println(solve(n))
})
}
}
```

Note: This problem (Captain Prime) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.