Last active
December 14, 2015 05:08
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
commented
Feb 25, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment