Skip to main content
replaced http://math.stackexchange.com/ with https://math.stackexchange.com/
Source Link

So it turns out there is a better algorithm for this. The algorithm is \$10^n - 9^n\$ and you can find an explanation of it over on Mathematics ExchangeMathematics Exchange. The improved algorithm completely removed my need for a loop, so I separated the test (printing) logic from the actual function and "ruby-ized" that loop instead.

def count_of_numbers_containing_5(power)
  (10**power)-(9**power)
end 

def test_it(pwr)
  (1..pwr).each {|i| puts count_of_numbers_containing_5(i)}
end

test_it 20

So it turns out there is a better algorithm for this. The algorithm is \$10^n - 9^n\$ and you can find an explanation of it over on Mathematics Exchange. The improved algorithm completely removed my need for a loop, so I separated the test (printing) logic from the actual function and "ruby-ized" that loop instead.

def count_of_numbers_containing_5(power)
  (10**power)-(9**power)
end 

def test_it(pwr)
  (1..pwr).each {|i| puts count_of_numbers_containing_5(i)}
end

test_it 20

So it turns out there is a better algorithm for this. The algorithm is \$10^n - 9^n\$ and you can find an explanation of it over on Mathematics Exchange. The improved algorithm completely removed my need for a loop, so I separated the test (printing) logic from the actual function and "ruby-ized" that loop instead.

def count_of_numbers_containing_5(power)
  (10**power)-(9**power)
end 

def test_it(pwr)
  (1..pwr).each {|i| puts count_of_numbers_containing_5(i)}
end

test_it 20
deleted 21 characters in body
Source Link
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174

So it turns out there is a better algorithm for this. The algorithm is \$10^n - 9^n\$ and you can find an explanation of it over on Mathematics Exchange. The improved algorithm completely removed my need for a loop, so I separated the test (printing) logic from the actual function and "ruby-ized" that loop instead.

def count_of_numbers_containing_5(power_of_tenpower)
  (10**power_of_ten10**power)-(9**power_of_ten9**power)
end 

def test_it(pwr)
  (1..pwr).each {|i| puts count_of_numbers_containing_5(i)}
end

test_it 20

So it turns out there is a better algorithm for this. The algorithm is \$10^n - 9^n\$ and you can find an explanation of it over on Mathematics Exchange. The improved algorithm completely removed my need for a loop, so I separated the test (printing) logic from the actual function and "ruby-ized" that loop instead.

def count_of_numbers_containing_5(power_of_ten)
  (10**power_of_ten)-(9**power_of_ten)
end 

def test_it(pwr)
  (1..pwr).each {|i| puts count_of_numbers_containing_5(i)}
end

test_it 20

So it turns out there is a better algorithm for this. The algorithm is \$10^n - 9^n\$ and you can find an explanation of it over on Mathematics Exchange. The improved algorithm completely removed my need for a loop, so I separated the test (printing) logic from the actual function and "ruby-ized" that loop instead.

def count_of_numbers_containing_5(power)
  (10**power)-(9**power)
end 

def test_it(pwr)
  (1..pwr).each {|i| puts count_of_numbers_containing_5(i)}
end

test_it 20
Source Link
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174

So it turns out there is a better algorithm for this. The algorithm is \$10^n - 9^n\$ and you can find an explanation of it over on Mathematics Exchange. The improved algorithm completely removed my need for a loop, so I separated the test (printing) logic from the actual function and "ruby-ized" that loop instead.

def count_of_numbers_containing_5(power_of_ten)
  (10**power_of_ten)-(9**power_of_ten)
end 

def test_it(pwr)
  (1..pwr).each {|i| puts count_of_numbers_containing_5(i)}
end

test_it 20