Problem 29

Statement
Consider all integer combinations of a for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

2²=4, 2³=8, 2⁴=16, 2⁵=32
3²=9, 3³=27, 3⁴=81, 3⁵=243
4²=16, 4³=64, 4⁴=256, 4⁵=1024
5²=25, 5³=125, 5⁴=625, 5⁵=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by a^b for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

Solution

class SquareSequence
  def initialize(max)
    @sequence = []
    @min = 2
    @max = max
    (@min..@max).each do |i|
      seq = []
      (@min..@max).each do |j|
        seq << i**j
      end
      @sequence = @sequence | seq
    end
  end

  def length
    return @sequence.length
  end
end

seq = SquareSequence.new(100)

puts seq.length
No tags for this post.

SPEAK / ADD YOUR COMMENT
Comments are moderated.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>