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. Lucky Numbers
  5. Discussions

Lucky Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 70 Discussions, By:

recency

Please Login in order to post a comment

  • vikasandnet
    5 years ago+ 6 comments

    why 23 in between 21 and 25 is not lucky number,

    as we are getting digit sum 5, and sum of square of digits are 13.

    14|
    Permalink
    View more Comments..
  • thecodingsoluti2
    4 months ago+ 0 comments

    Here is Lucky Numbers problem solution in Python Java C++ and C programming - https://programs.programmingoneonone.com/2021/07/hackerrank-lucky-numbers-problem-soluton.html

    0|
    Permalink
  • thecodingsoluti2
    4 months ago+ 0 comments

    Here is Lucky Numbers problem solution in Python Java C++ and C programming - https://programs.programmingoneonone.com/2021/07/hackerrank-lucky-numbers-problem-soluton.html

    0|
    Permalink
  • vinny_sing
    7 months ago+ 0 comments

    This problem is obscenely difficult, and I spent days researching just to get fairly close to the correct solution, then I spent days more tweaking my approach to conform to the extra constraints created by the problem itself. In order to complete this in a reasonable time, you have to dig into number theory by realizing that, at least for a=1 to b=(some number of consecutive 9's), the problem resembles a permutation problem (since we take the sums/squares of these numbers, the question becomes 'how many combinations of k digits result in a prime number between 1 and 999999999999999999 when (summed) or (squared)?' and 'how can you exclude permutations that are less than a, and greater than b?'

    Honestly, I'm still not even finished trying to make the damn thing work, I just know the approach WILL work, once I've debugged everything.

    0|
    Permalink
  • satyamgawade
    1 year ago+ 1 comment

    can anyone let me know how i can optimize it further

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'luckyNumbers' function below.

    #

    The function is expected to return a LONG_INTEGER.

    The function accepts following parameters:

    1. LONG_INTEGER a

    2. LONG_INTEGER b

    #

    def luckyNumbers(a, b): # Write your code here temp = 0 total = 0

    for i in range(a,b+1):
        temp = i
        totalsum = 0
        totalsumsq = 0
        while temp != 0:
            digit = temp % 10
            temp = temp // 10
            totalsum = totalsum + digit
            totalsumsq = totalsumsq + (digit**2)
    
        def isprime(n):
            if n > 1:
                for k in range(2,n):
                    if (n % k) == 0:
                        return False
                return True
            else:
                return False 
    
    
        isprime(totalsum)
        isprime(totalsumsq)
    
        if isprime(totalsum) and isprime(totalsumsq):
            total = total + 1
    
    return total        
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())
    
    for t_itr in range(t):
        first_multiple_input = input().rstrip().split()
    
        a = int(first_multiple_input[0])
    
        b = int(first_multiple_input[1])
    
        result = luckyNumbers(a, b)
    
        fptr.write(str(result) + '\n')
    
    fptr.close()
    
    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