maraigue (owner)

Revisions

gist: 47886 Download_button fork
public
Description:
年子平方数
Public Clone URL: git://gist.github.com/47886.git
Embed All Files: show embed
toshigo.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Integer
  def toshigo?
    # return nil if self <= 0
    
    s = self.to_s
    l = s.length
    return false if l % 2 != 0
    
    ((s[0...(l/2)].to_i) - (s[(l/2)..-1].to_i)).abs == 1
  end
end
 
i = 1
c = 0
while c < 21
  t = i * i
  if t.toshigo?
    puts "#{i}^2 = #{t}"
    c += 1
  end
  i += 1
end