Project

General

Profile

Feature #10354 ยป prime_opt.patch

marcandre (Marc-Andre Lafortune), 10/10/2014 03:22 AM

View differences:

lib/prime.rb
272 272
    end
273 273
    # Iterates the given block for each prime number.
274
    def each(&block)
275
      return self.dup unless block
274
    def each
275
      return self.dup unless block_given?
276 276
      if @ubound
277 277
        last_value = nil
278 278
        loop do
279 279
          prime = succ
280 280
          break last_value if prime > @ubound
281
          last_value = block.call(prime)
281
          last_value = yield prime
282 282
        end
283 283
      else
284 284
        loop do
285
          block.call(succ)
285
          yield succ
286 286
        end
287 287
      end
288 288
    end
......
350 350
    end
351 351
    def succ
352
      loop do
353
        if (@step)
354
          @prime += @step
355
          @step = 6 - @step
356
        else
357
          case @prime
358
          when 1; @prime = 2
359
          when 2; @prime = 3
360
          when 3; @prime = 5; @step = 2
361
          end
352
      if (@step)
353
        @prime += @step
354
        @step = 6 - @step
355
      else
356
        case @prime
357
        when 1; @prime = 2
358
        when 2; @prime = 3
359
        when 3; @prime = 5; @step = 2
362 360
        end
363
        return @prime
364 361
      end
362
      return @prime
365 363
    end
366 364
    alias next succ
367 365
    def rewind
......
479 477
    #
480 478
    # Iterates the given block over all prime numbers. Note that enumeration
481 479
    # starts from the current position of internal pointer, not rewound.
482
    def each(&block)
480
    def each
483 481
      return @generator.dup unless block_given?
484 482
      loop do
485 483
        yield succ