-4

An integer is a lucky number if each number in it is 3 or 7. To give an example, the numbers 7, 73, and 33777 are lucky numbers. Your task is to calculate the number of lucky numbers between int(a) and int(b).

How could one achieve this efficiently (in less than 1 sec) without going through all the numbers? I have attempted many solutions in Java however none of them are fast enough.

  • 2
    Whats the range? What code have you tried? What attempt is not fast enough? – Jason Aug 30 '20 at 13:40
  • The range would be 1 < 10^9 – user14191409 Aug 30 '20 at 13:45
  • I have tried code where I precount amounts. Say I precount that the range between 1 and 10^8 contains 256 such cases etc – user14191409 Aug 30 '20 at 13:46
  • hm all numbers between 1 and 10^9 that only have digits 3 and 7 should be something in the magnitude of 2^11. Brute-force should be sufficient. – Turing85 Aug 30 '20 at 13:48
  • Just did a quick recursive brute-force test. It took `1594500` nanoseconds (or `0.0015945` seconds) to construct all lucky number from 1 to 10^9. – Turing85 Aug 30 '20 at 14:00
  • @Turing85 How long to brute-force count iterate the numbers from `4000000000` to `9000000000` and check if they are lucky? I get 9.7 seconds. – Andreas Aug 30 '20 at 14:07
  • @Andreas Not exaclty sure what you mean by "*brute-force count iterate*", but my program takes `0.003` s, and finds `512` results. – Turing85 Aug 30 '20 at 14:12
  • @Turing85 Brute force: `for (long i = a; i < b; i++) { if (isLucky(i)) { count++; } }` --- That is the brutest and simplest forced algorithm I can think of. – Andreas Aug 30 '20 at 14:25
  • @Andreas I chose a constructive brute-force approach =) – Turing85 Aug 30 '20 at 14:35
  • You could literally just count in binary, but 3 instead of 0, and 7 instead one 1 – RIVERMAN2010 Aug 30 '20 at 14:35
  • @RIVERMAN2010 So how do you count lucky numbers between `12345678` and `98765432` "in binary"? – Andreas Aug 30 '20 at 14:43
  • @Andreas I think you could just get the number of digits and set all of the numbers to the minimun (3s) so 1234567 would become 3333333 and continue from there, if you were to have something like 9999 that would convert to 33333. – RIVERMAN2010 Aug 30 '20 at 14:49
  • @RIVERMAN2010 So the input number doesn't matter, only the length of it? Counting for range 36-86 is the same as counting for range 33-86? Nope, because the first finds 3 lucky numbers (37, 73, 77) while the second finds 4 lucky numbers (33, 37, 73, 77). And how is that "counting in binary"? – Andreas Aug 30 '20 at 14:54
  • the range does matter, its really hard to explain what I mean, but it rounds *up* to the next 3 or 7. – RIVERMAN2010 Aug 30 '20 at 14:57
  • its not exactly *binary* but it is quite close to how it counts, because we are using a form of base 2, for example we can start at zero and count up, its simply a form of base 2 : ````3, 7, 30, 33, 37, 73, 77, 333```` – RIVERMAN2010 Aug 30 '20 at 15:00
  • 3
    Please do not deface your question. Reversed. – DontKnowMuchBut Getting Better Aug 30 '20 at 18:48
  • 2
    Please do **not** vandalize your question, especially after an answer has been provided. – desertnaut Aug 30 '20 at 18:48

1 Answers1

4

Since you have to count the numbers and not list the numbers, you can use permutation and combination to find the answer.

Eg let say find between 1 and 999 where you can use 3, 7

Then you have 3 lengths single, double and triple digits with constraints on single and triple digits. For single since minimum number is 1 and 3, 7 both are greater there 2 numbers. For double digits you have no constraints hence you have 2 * 2 = 4 combinations Similarly for 3 digits as max number allowed is 9 in each place and 3,7 are lesser than them there will be 2 * 2 * 2 = 8

So answer is 14 after summing them all... This algorithm will run fast as it depends on the size of the numbers to generate ie o(n) time complexity where n is max length of number.

Saurabh Nigam
  • 795
  • 6
  • 12