Skip to content

Instantly share code, notes, and snippets.

@takano32
Last active July 19, 2016 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takano32/1fe8860377a7b6592244c2b3acceb10c to your computer and use it in GitHub Desktop.
Save takano32/1fe8860377a7b6592244c2b3acceb10c to your computer and use it in GitHub Desktop.
階乗フィーバー数問題
#!/usr/bin/env ruby
# http://riverplus.net/39/
# https://twitter.com/riverplus/status/754322322220158977
#
class Factorial
@@f = 1
@@now = 0
def self.next
@@now += 1
@@f *= @@now
end
def self.index
@@now + 1
end
end
1.upto(12) do |i|
puts "#{Factorial.index}: #{Factorial.next}"
end
def solve?(f)
ns = f.to_s[0...6].split ''
return false if ns.size < 6
(ns - [ns.first]).empty?
end
i = 0
loop do
i += 1
f = Factorial.next
if solve? f
puts i
end
end
@takano32
Copy link
Author

$ time ruby main.rb
main.rb:8:in `factorial': stack level too deep (SystemStackError)
    from main.rb:11:in `factorial'
    from main.rb:11:in `factorial'
    from main.rb:11:in `factorial'
    from main.rb:11:in `factorial'
    from main.rb:11:in `factorial'
    from main.rb:11:in `factorial'
    from main.rb:11:in `factorial'
    from main.rb:11:in `factorial'
     ... 10068 levels...
    from main.rb:17:in `solve'
    from main.rb:25:in `block in <main>'
    from main.rb:23:in `loop'
    from main.rb:23:in `<main>'
ruby main.rb  134.20s user 21.95s system 99% cpu 2:37.37 total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment