Overview

 

Question

Consider all integer combinations of \(a^b\) for \(2 \leq a \leq 5\) and \(2 \leq b \leq 5\): $$\begin{aligned} & 2^2 = 4\text{, } 2^3 = 8\text{, } 2^4 = 16\text{, } 2^5 = 32 \\ & 3^2 = 9\text{, } 3^3 = 27\text{, } 3^4 = 81\text{, } 3^5 = 243 \\ & 4^2 = 16\text{, } 4^3 = 64\text{, } 4^4 = 256\text{, } 4^5 = 1024 \\ & 5^2 = 25\text{, } 5^3 = 125\text{, } 5^4 = 625\text{, } 5^5 = 3125 \end{aligned}$$ If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: $$4\text{, } 8\text{, } 9\text{, } 16\text{, } 25\text{, } 27\text{, } 32\text{, } 64\text{, } 81\text{, } 125\text{, } 243\text{, } 256\text{, } 625\text{, } 1024\text{, } 3125$$ How many distinct terms are in the sequence generated by \(a^b\) for \(2 \leq a \leq 100\) and \(2 \leq b \leq 100\)?

click for answer

9183

Solutions

 

Ruby

 exponentComb.rb https://raw.github.com/addamh/euler/master/029/exponentComb.rb download
#!/usr/bin/env ruby

arr = []
(2..100).each do |a|
  (2..100).each do |b|
    arr.push a**b
  end
end
arr.uniq!
puts arr.length
$ time ruby exponentComb.rb
real	0m0.908s
user	0m0.874s
sys	0m0.032s