Skip to content

Instantly share code, notes, and snippets.

@acook
Last active December 14, 2015 05:08
Show Gist options
  • Save acook/5032661 to your computer and use it in GitHub Desktop.
Save acook/5032661 to your computer and use it in GitHub Desktop.
Attempt to implement the "math hack" described here: http://9gag.com/gag/6663551 WiP, this is just a quick kludge and doesn't work 100% yet.
#99 x 99 = 9312 |
def reset
"\e[0m"
end
def mult l, r
lb = 10 ** l.to_s.size
rb = 10 ** r.to_s.size
ls = lb - l
rs = rb - r
sig = [lb, rb].max - (ls + rs)
ins = ls * rs
ins = "#{('0' * (rb.to_s.size - ins.to_s.size - 1))}#{ins}" if ins.to_s.length < rb.to_s.size
"#{sig}#{ins}".to_i
end
def display lnumber, rnumber, result
color = result == lnumber * rnumber ? "\e[32m" : "\e[31m"
print "#{color}#{lnumber}x#{rnumber} = #{result}"
print "#{reset} | "
end
def test
lnum = 97
rnum = 96
result = mult lnum, rnum
display lnum, rnum, result
puts
exit
end
def test_loop
100.times do |lnumber|
100.times do |rnumber|
result = mult lnumber, rnumber
display lnumber, rnumber, result
end
end
puts
end
test_loop
@universal
Copy link

def math_hack(a,b)
  c = 100 -a
  d = 100 -b
  r1 = 100 - c - d
  r2 = c * d
  r1 * 100 + r2
end

(1..100).map do |i|
  (1..100).map do |j|
    i*j - math_hack(i,j)
  end
end.flatten.all?{|i| i == 0}

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