start problem solving

There are 100 cats in a row all wearing hats. ๐Ÿฑ

Make 100 passes over the cats.

On the first pass, visit every cat and remove its hat. ๐Ÿ™€

On the second pass, visit every other cat and replace its hat. ๐Ÿ˜บ

On the third pass, visit every third cat. If the cat is hatted, remove its hat ๐Ÿ˜ฟ. If the cat is un-hatted, replace its hat. ๐Ÿ˜บ

On the 4th-100th passes, do the same. Add hats if they donโ€™t exist and remove hats if they do.

On the 100th pass, you should only visit the 100th cat (the last cat), removing or replacing its hat appropriately.

Write a program that outputs which cats have hats ๐Ÿ˜บ and which cats do not ๐Ÿ˜พ at the end of the 100th iteration.


Inย [4]:
module Cats
  def self.cats_in_hats(sequence)
    count = sequence.length
    count.times do |index|
      (index..count).step(index + 1) do |n|
          sequence[n] = !sequence[n]
        end
    end
    sequence
  end
end

go = Array.new(100, true)
Cats.cats_in_hats(go).select.with_index do |cat, i|
    puts "Cat ##{i + 1} has no hat" unless cat
end


Cat #1 has no hat
Cat #4 has no hat
Cat #9 has no hat
Cat #16 has no hat
Cat #25 has no hat
Cat #36 has no hat
Cat #49 has no hat
Cat #64 has no hat
Cat #81 has no hat
Cat #100 has no hat
Out[4]:
[]