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

    You are viewing a single comment's thread. Return to all comments →

  • macdonald_l_kyn
    4 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
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy