Red John is Back

Problem Statement :

Red John has committed another murder. This time, he doesn't leave a red smiley behind. Instead he leaves a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows.

There is a wall of size 4xn in the victim's house. The victim has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks. First we must calculate the total number of ways in which the bricks can be arranged so that the entire wall is covered. The following diagram shows how bricks might be arranged to cover walls where 1 <= n <= 4:


There is one more step to the puzzle. Call the number of possible arrangements M. Patrick must calculate the number of prime numbers P in the inclusive range 0-M.

As an example, assume n=3. From the diagram above, we determine that there is only one configuration that will cover the wall properly. 1 is not a prime number, so P=0.

A more complex example is n = 5. The bricks can be oriented in  total configurations that cover the wall. The two primes 2 and 3 are less than or equal to 3, so P =2.


Function Description

Complete the redJohn function in the editor below. It should return the number of primes determined, as an integer.

redJohn has the following parameter(s):

n: an integer that denotes the length of the wall

Input Format

The first line contains the integer t, the number of test cases.
Each of the next t lines contains an integer n, the length of the 4*n wall.

1 <= t <= 20
1 <= n <= 40

Output Format

Print the integer P on a separate line for each test case.

Solution :


                            Solution in C :

In C++ :

#include <iostream>
#define FOR(i,a)    for(int i = 0;i < (a);i++)
#define REP(i,a,b)  for(int i = (a);i < (b);i++)
#define SZ(a)   ((signed)a.size())
#define PB(a)   push_back(a)
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstdio>

using namespace std;

int l[1000001],k = 0;
bool prime[1000001] = {0};

void preprocess(){
    for(long long i = 2;i < 1000001;i++){
            l[k++] = i;
            for(long long j = i*i ; j <= 1000000;j += i)    prime[j] = 1;

void solve(){
    long long arr[101];
    arr[1] = 1,arr[2] = 1,arr[3] = 1,arr[4] = 2;
    arr[0] = 1;
    REP(i,5,41)    arr[i] = (arr[i - 1] + arr[i - 4]);
    int c = 0;
    int v;
        if(l[i] <= arr[v])  c++;
        else    break;

int main(){
    int t;
    cin >> t;
    FOR(i,t)    solve();
    return 0;

In Java :

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

 * Created with IntelliJ IDEA.
 * User: Sandesh
 * Date: 7/27/13
 * Time: 10:47 PM
 * To change this template use File | Settings | File Templates.
public class Solution {
    static long dp[];
    static HashSet<Integer> hs;
    public static void main(String arg[]){
        hs=new HashSet<Integer>();
        dp=new long[41];
        Scanner sc=new Scanner(;
        int N=sc.nextInt();
        for (int j=0;j<N;j++){
            int val=sc.nextInt();
            int x=(int)(new Solution().get(val));
            int ans=0;
            for (int i=2;i<=x;i++){
    public long get(int N){
            return dp[N];
        long ans=1;
        else {
        return ans;
    static boolean isPrime(int a){
        for (int i=2;i*i<=a;i++){
                return false;
        return true;
    static void setPrime(){
        for (int i=2;i<=217286;i++){



In C :

  int sieve[300000][2];
int main()
    int t,i,j,k,n;
    int a[41],x;
   // printf("%d\n",a[40]);


In Python3 :

nbCombMem = ([],[])
def nbComb(N) :
   if N in nbCombMem[0] :
      return nbCombMem[1][nbCombMem[0].index(N)]
   if N < 0 : return 0
   c = 1
   for i in range(0,N-3) :
      c += nbComb(N-4-i)
   return c
# return the primes (strictly) under n. Use a sieve of erathostene.
def getPrimesUnder(n) :
   r = [2]
   n2 = n // 2
   l = list(True for i in range(0,n2))
   l[0] = False
   for i in range(1,n2) :
      if l[i] :
         for m in range(2*i*(i+1),n2,2*i+1) :
            l[m] = False
   return r
# main
T = int(input())
Cs = [nbComb(int(input())) for t in range(0,T)]

Ps = getPrimesUnder(max(Cs)+1)

for t in range(0,T) :
    c = 0
    while c < len(Ps) and Ps[c] <= Cs[t] : c += 1

View More Similar Problems

Left Rotation

A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. Given an integer, d, rotate the array that many steps left and return the result. Example: d=2 arr=[1,2,3,4,5] After 2 rotations, arr'=[3,4,5,1,2]. Function Description: Complete the rotateLeft function in the editor below. rotateLeft has the following parameters: 1. int d

View Solution →

Sparse Arrays

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun

View Solution →

Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →