Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many times does a zero occur on an odometer

I am solving how many times a zero occus on an odometer. I count +1 everytime I see a zero.

  • 10 -> +1
  • 100-> +2 because in 100 I see 2 zero's
  • 10004 -> +3 because I see 3 zero's

So I get,

  • 1 - 100 -> +11
  • 1 - 500 -> +91
  • 1 - 501 -> +92
  • 0 - 4294967295-> +3825876150

I used rubydoctest for it. I am not doing anything with begin_number yet. Can anyone explain how to calculate it without a brute force method?

I did many attempts. They go well for numbers like 10, 1000, 10.000, 100.000.000, but not for numbers like 522, 2280. If I run the rubydoctest, it will fail on # >> algorithm_count_zero(1, 500)

    # doctest: algorithm_count_zero(begin_number, end_number)
    # >> algorithm_count_zero(1, 10)
    # => 1
    # >> algorithm_count_zero(1, 1000)
    # => 192
    # >> algorithm_count_zero(1, 10000000)
    # => 5888896
    # >> algorithm_count_zero(1, 500)
    # => 91
    # >> algorithm_count_zero(0, 4294967295)
    # => 3825876150
    def algorithm_count_zero(begin_number, end_number)
      power = Math::log10(end_number) - 1
      if end_number < 100
        return end_number/10
      else
        end_number > 100
        count = (9*(power)-1)*10**power+1
      end
      answer = ((((count / 9)+power)).floor) + 1
    end

end_number = 20000
begin_number = 10000
puts "Algorithm #{algorithm_count_zero(begin_number, end_number)}"
like image 392
Romano Avatar asked Dec 08 '25 05:12

Romano


1 Answers

As noticed in a comment, this is a duplicate to another question, where the solution gives you correct guidelines.

However, if you want to test your own solution for correctness, i'll put in here a one-liner in the parallel array processing language Dyalog APL (which i btw think everyone modelling mathemathics and numbers should use).

Using tryapl.org you'll be able to get a correct answer for any integer value as argument. Tryapl is a web page with a backend that executes simple APL code statements ("one-liners", which are very typical to the APL language and it's extremely compact code).

The APL one-liner is here:

{+/(c×1+d|⍵)+d×(-c←0=⌊(a|⍵)÷d←a×+0.1)+⌊⍵÷a←10*⌽⍳⌈10⍟⍵} 142857

Copy that and paste it into the edit row at tryapl.org, and press enter - you will quickly see an integer, which is the answer to your problem. In the code row above, you can see the argument rightmost; it is 142857 this time but you can change it to any integer.

As you have pasted the one-liner once, and executed it with Enter once, the easiest way to get it back for editing is to press [Up arrow]. This returns the most recently entered statement; then you can edit the number sitting rightmost (after the curly brace) and press Enter again to get the answer for a different argument.

Pasting teh code row above will return 66765 - that many zeroes exist for 142857.

If you paste this 2 characters shorter row below, you will see the individual components of the result - the sum of these components make up the final result. You will be able to see a pattern, which possibly makes it easier to understand what happens.

Try for example

      {(c×1+d|⍵)+d×(-c←0=⌊(a|⍵)÷d←a×+0.1)+⌊⍵÷a←10*⌽⍳⌈10⍟⍵} 1428579376

0 100000000 140000000 142000000 142800000 142850000 142857000 142857900 142857930 142857937

... and see how the intermediate results contain segments of the argument 1428579376, starting from left! There are as many intermediate results as there are numbers in the argument (10 this time).

The result for 1428579376 will be 1239080767, ie. the sum of the 10 numbers above. This many zeroes appear in all numbers between 1 and 1428579376 :-).

like image 149
Stormwind Avatar answered Dec 11 '25 00:12

Stormwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!