Class: UlamSequence
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(size) ⇒ UlamSequence
constructor
A new instance of UlamSequence.
- #origin ⇒ Object
Constructor Details
#initialize(size) ⇒ UlamSequence
Returns a new instance of UlamSequence.
8 9 10 11 12 |
# File 'lib/ulam_sequence.rb', line 8 def initialize(size) @size = size.to_i @max = @size**2 @sieve = PrimeSieve.new(@max) end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
6 7 8 |
# File 'lib/ulam_sequence.rb', line 6 def max @max end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
6 7 8 |
# File 'lib/ulam_sequence.rb', line 6 def size @size end |
Instance Method Details
#each ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ulam_sequence.rb', line 14 def each return to_enum { size } unless block_given? points = Enumerator.new do |yielder| walk_cycle = [ lambda { |x, y| [x + 1, y] }, # east lambda { |x, y| [x, y - 1] }, # north lambda { |x, y| [x - 1, y] }, # west lambda { |x, y| [x, y + 1] }, # south ].cycle x, y = origin i = 1 advance_point = walk_cycle.next loop do ((i + 1) / 2).times do yielder << [x, y] x, y = advance_point.call(x, y) end advance_point = walk_cycle.next i += 1 end end 1.upto(max) do |n| x, y = points.next yield [x, y, n, sieve.prime?(n)] end end |
#origin ⇒ Object
46 47 48 49 50 51 |
# File 'lib/ulam_sequence.rb', line 46 def origin [ (size - 1) / 2, size / 2, ] end |