Skip to content

Instantly share code, notes, and snippets.

@takano32 takano32/codeiq39.rb
Last active Jul 19, 2016

Embed
What would you like to do?
階乗フィーバー数問題
#!/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

This comment has been minimized.

Copy link
Owner Author

takano32 commented Jul 19, 2016

$ 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
You can’t perform that action at this time.