Closed JoshCheek closed 6 years ago
def fact!(n) (1..n).reduce(1.0, :*) end alias ! fact! # e is the sum of the inverses of the integers 0.upto(100).sum { |i| 1.0 / fact!(i) } # => Math::E # => Math::E ** 2 # => 0.upto(100).sum { |i| 2**i / fact!(i) } # => def sin(x) 1.step(to: 100, by: 4).sum { |i| x**(i) / fact!(i) - x**(i+2) / fact!(i+2) } 1.step(to: 100, by: 2).sum { |i| x**i / !(i) / (1-i[1]*2) } end (1).step(to: 10, by: 2) { |i| (-1)**(i%4/2) # => (i+2)%4-2 # => 1-2*i[1] # => 1-i[1]*2 # => (-i>>1&1)*2-1 # => } sin Math::PI / 10 # => Math.sin Math::PI / 10 # => # 0/0! + x/1! - 0x^2/2! - x^3/3! + 0x^4/4! + x^5/5! # 1x/1! - x^3/3! + x^5/5! - x^7/7! # !> /Users/josh/.gem/ruby/2.5.0/gems/seeing_is_believing-3.6.0/lib/seeing_is_believing/event_stream/producer.rb:79:in `record_exception': private method `!' called for nil:NilClass (NoMethodError) # !> \tfrom /Users/josh/.gem/ruby/2.5.0/gems/seeing_is_believing-3.6.0/lib/seeing_is_believing/the_matrix.rb:81:in `block in <top (required)>' # !> /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-40010-cajhs3/program.rb:18:in `block in sin': private method `!' called for 1:Integer (NoMethodError) # !> \tfrom /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-40010-cajhs3/program.rb:17:in `step' # !> \tfrom /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-40010-cajhs3/program.rb:17:in `each' # !> \tfrom /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-40010-cajhs3/program.rb:17:in `sum' # !> \tfrom /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-40010-cajhs3/program.rb:17:in `sin' # !> \tfrom /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-40010-cajhs3/program.rb:30:in `<main>' __END__ def divvy_up(jewels) unsplit = jewels.chars totals = unsplit.group_by(&:itself).map { |j, js| [j, js.count] }.to_h counts = totals.transform_values { 0 } first = 0 last = unsplit.length / 2 (first...last).each { |i| counts[unsplit[i]] += 1 } until counts.all? { |j, c| totals[j] == c*2 } counts[jewels[first]] -= 1 counts[jewels[last]] += 1 first += 1 last += 1 end [ unsplit[0...first], unsplit[first...last], unsplit[last..-1], ].reject(&:empty?) end chars = "RRRRGGGGGG".chars 10.times do str = chars.shuffle.join [str, divvy_up(str)] end def r(n=1, max) return n if n == max n + 1.0 / r(n+1, max) end r 1 # => 1 r 2 # => 1.5 r 3 # => 1.4285714285714286 r 4 # => 1.4333333333333333 r 100 # => 1.4331274267223117 def r(max) # ~> ArgumentError: wrong number of arguments (given 2, expected 1) n = 1 sum = 0 while n <= max n + 1.0 / r(n+1, max) n += 1 end end r 1 # => r 2 # => r 3 # => # ~> ArgumentError # ~> wrong number of arguments (given 2, expected 1) # ~> # ~> /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-39264-owea8a/program.rb:57:in `r' # ~> /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-39264-owea8a/program.rb:61:in `r' # ~> /var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20180520-39264-owea8a/program.rb:66:in `<main>' __END__ 1 + 1.0/2 # => 1.5 1 + 1.0/(2+1.0/3) # => 1.4285714285714286 1 + 1.0/(2+1.0/(3+1.0/4)) # => 1.4333333333333333 1 + 1.0/(2+1.0/(3+1.0/(4+1.0/5))) # => 1.4331210191082802 1 + 1.0/(2+1.0/(3+1.0/(4+1.0/(5+1.0/6)))) # => 1.433127572016461 Math.sqrt 2 # => 1.4142135623730951 __END__ r = c + n/d r-c = n/d d = n/(r-c) d_old = n/(r_old-c) r(n) = n + 1/(n+1) (r(n)-n)(n+1) = 1 n+1 = 1/(r(n)-n) n+1/(n+1) = 1/(r(n)-n) (r(n)-n)(n+1)/(n+1) = 1 (r(n)-n)(n+1) = (n+1) n + 1/d = r nd + 1 = rd 1 = (rd - nd) 1 = d(r-n) 1 = ((n+1)+1/d2)(r-n) r(n+1) = d+m = n/(r-c) (d+m)(r-c) = n r-c = n/(d+m) r = c+n/(d+m)