Skip to content

Instantly share code, notes, and snippets.

@track8
track8 / simple.rb
Created March 4, 2015 19:41
toRuby 99まで
class Number < Struct.new(:value)
def to_s
value.to_s
end
def inspect
"<<#{self}>>"
end
def reducible?
@track8
track8 / hello_yarv.rb
Created March 18, 2015 14:12
Hello YARV
code = <<END
puts "Hello World!"
END
puts RubyVM::InstructionSequence.compile(code).disasm
def formula_ptn(ptn)
ope = ptn.to_s(3).rjust(8, '0').gsub('0', 'n').gsub('1', '+').gsub('2', '-')
(1..9).map(&:to_s).each_with_index.inject('') do |result, (num, index)|
result += num
if ope[index] && ope[index] != 'n'
result += ope[index]
end
result
end
end
def euler_62
n = 1
hash = Hash.new {|h, k| h[k] = []}
current_length = 1
while true
cubic = n**3
key = cubic.to_s.split('').sort.join
hash[key] << cubic
if key.length > current_length
require 'mathn'
car, *cdr = (1..9).map{|i| ['', '+', '-', '*', '/'].map { |o| o + i.to_s }}
exps = car.product(*cdr).map {|exp| exp.join}.delete_if{|e| e =~ /^[\+|\*|\/]/}
komachies = exps.select{|e| eval(e) == 100}
komachies.each {|k| puts k}
puts "#{komachies.count} / #{exps.count}"
require './minruby'
def evaluate(tree, genv, lenv)
case tree[0]
when 'lit'
tree[1]
when '+'
lenv['plus_count'] ||= 0
lenv['plus_count'] += 1
left = evaluate(tree[1], genv, lenv)