We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Red John is Back
  5. Discussions

Red John is Back

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 140 Discussions, By:

recency

Please Login in order to post a comment

  • macdonald_l_kyn
    2 months ago+ 0 comments

    I have the least time execution dealing with prime numbers

    def isprime(n):
        prime = [True for i in range(n + 1)]
        p = 2
        while p * p <= n:
            if prime[p]:
                for i in range(p * 2, n + 1, p):
                    prime[i] = False
            p += 1
        prime_numbers = []
        for p in range(2, n):
            if prime[p]:
                prime_numbers.append(p)
        return len(prime_numbers)
    
    def redJohn(n):
        m = 0
        for i in range(n // 4 + 1):
            pl = n - (i * 3)
            m += int(math.factorial(pl) / (math.factorial(i) * math.factorial(pl - i)))
        return isprime(m+1)
    
    0|
    Permalink
  • thecodingsoluti2
    4 months ago+ 0 comments

    Here is Red John is Back problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-red-john-is-back-problem-solution.html

    0|
    Permalink
  • mandarbhatye
    6 months ago+ 0 comments
    public static int redJohn(int n) {
            int countWays = numberOfWays(n);
            int totalPrimes = sieve(countWays);
            
            return totalPrimes;
        }
    		
    public static int numberOfWays(int N){
        if (N<=3) return 1;
        if (N==4) return 2;
        return numberOfWays(N-1) + numberOfWays(N-4);
    }
    
    public static int sieve(int n) {
        int count = 0;
        boolean prime[] = new boolean[n + 1];
        for (int i = 0; i <= n; i++)
            prime[i] = true;
    
        for (int p = 2; p * p <= n; p++)
            if (prime[p] == true)
                for (int i = p * 2; i <= n; i += p)
                    prime[i] = false;
    
        for (int i = 2; i <= n; i++)
            if (prime[i] == true)
                count++;
    
        return count;
    
    0|
    Permalink
  • mandarbhatye
    6 months ago+ 0 comments

    public static int redJohn(int n) { int countWays = numberOfWays(n); int totalPrimes = sieve(countWays);

        return totalPrimes;
    }
    
    public static int numberOfWays(int N){
        if (N<=3) return 1;
        if (N==4) return 2;
        return numberOfWays(N-1) + numberOfWays(N-4);
    }
    
    public static int sieve(int n) {
        int count = 0;
        boolean prime[] = new boolean[n + 1];
        for (int i = 0; i <= n; i++)
            prime[i] = true;
    
        for (int p = 2; p * p <= n; p++)
            if (prime[p] == true)
                for (int i = p * 2; i <= n; i += p)
                    prime[i] = false;
    
        for (int i = 2; i <= n; i++)
            if (prime[i] == true)
                count++;
    
        return count;
    }
    
    0|
    Permalink
  • cagils
    8 months ago+ 0 comments

    js

    function redJohn(n) {
        const r = x => Math.round(x)
        const s = x => Math.sqrt(x)
        
        const fact = (n) => n <= 1 ? 1 : fact(n - 1) * n
        
        const prime = (n) => { 
            if (n % 2 == 0 && n > 2) 
                return false
            for (let i=3; i <= r(s(n)); i+=2)
                if (n % i == 0)
                    return false
            return true
        }
        
        let M = 0
        for (let i=0; i <= n/4; i++) {
            M += r(fact(n-i*3) / (fact(i) * fact(n-i*4)))
        }
        let res = 0
        for (let i=2; i <= M; i++)
            if (prime(i))
                res++
        return res
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy