Lucky numbers?

Given an input 'n' from the user, you need to find all lucky numbers from 1 to n. Lucky numbers are those which have either 4 or 7 or both as their digits. What is the most efficient method to solve this problem?

Shravan Kumar at Quora Visit the source

Was this solution helpful to you?

Other answers

Theoretically, you could create a compile time table if upper bound of n is known. Each item of the table holds lucky numbers seen till then. Index in to the table with input number.

Pratik Ashar

The other solution can be: Step 1- count the number of digits of 'n'. Say it is 'd'. Step 2- now for i=1 to d, generate the numbers from 0 to (2^i) -1 in binary and replace 0 with 4 and 1 with 7. Also if(i>=1 and i<= d-1) then print the numbers. and when i=d, check whether the number is smaller than n, then print it else ignore.

Shefali Bansal

Let X be any lucky number, then you can generate another two lucky numbers from X. i.e. "10*X+4" and "10*X+7"..!! You can do it anyway, but here is the recursive solution..!! I hope this will help... Link To The Solution: http://paste.ubuntu.com/7632294/ (Solution by kakajan on codeforces) Codeforces Blog: http://212.193.33.25/blog/entry/12659

Swapnil Walke

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.