ruby - Why does the reduce implementation return undefined method + for nil:NilClass once a boolean check returns true -


I need to find the sum of all the digits which are equal to the sum of the factors of their digits. I have an algorithm:

  def equal_to_sum_of_factorial_digits? (N) n == n.to_s.scan (/ \ d /). Map {| S | s.to_i} .reduce {| ACC, Jay. (J == 0 || J == 1)? 1: ACC + (1.j). Injection (: *)} and start = time Now Answer = (10..145) .readuce {| ACC, Jay. ACC + J. if equals_to_soom_off_factorial_diggs? (J)} says "the answer is # {answer} and it has got # {(time.now-start) * 1000} milliseconds."   

here equal_to_sum_of_factorial_digits? creates a number in separate digits, then adds value to 1 if it is 1 or 0 (1 or 0) The originator of is 1) or if it is 2 above 9 , then its actual value is. The answer then sums up all the values ​​in that property.

As long as I do not go beyond 145, the code works (145) The first value is the property, which is its factual number: 145 = 1! + 4! + 5!) This error returns undefined method for zero '+': NilClass .

I do not know why the code after answer = is equal_to_sum_of_factorial_digits? Returns the method true Can someone illuminate me?

The reason for this is that in the last iteration in less , The reason was caused by the block being evaluated for zero due to zero . Note that the value of ACC is the evaluation value of the block for each walk.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -