LoginSignup
0
1

More than 5 years have passed since last update.

回文数の数

Last updated at Posted at 2017-09-02

今週のアルゴリズムで既出の問題ですね…
cf: https://github.com/cielavenir/codeiq_solutions/blob/master/thisweek_masuipeo/tyama_codeiq575_mod.rb

#!/usr/bin/env ruby
#http://nabetani.sakura.ne.jp/hena/orde17palin/
#http://qiita.com/Nabetani/items/aa15539c8dc2c610c1a0
def solve(t,b=10)
    return to_enum(:solve,t,b) if !block_given?
    (0...b).each{|i|return if i>t;yield i}
    start=1
    digit=1
    loop{
        start.step(start*b-1){|i|
            s=i.to_s(b)
            n=(s+s.reverse).to_i(b)
            return if n>t
            yield n
        }
        start.step(start*b-1){|i|
            s=i.to_s(b)
            (0...b).each{|mid|
                n=(s+mid.to_s(b)+s.reverse).to_i(b)
                return if n>t
                yield n
            }
        }
        start*=b
        digit+=b
    }
end

while gets
    x,y,b=$_.split(',').map &:to_i
    p solve(y-1,b).to_a.size-solve(x-1,b).to_a.size
    STDOUT.flush
end
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1