Last active
February 22, 2019 11:43
-
-
Save Vaguery/9099635 to your computer and use it in GitHub Desktop.
A brute force approach to find multipliers for the first part of Peter Winkler's math puzzle 'Zeros, ones or twos', in his book _Mathematical Puzzles_
This file contains 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
# A horrible awful brute-force approach to Peter Winkler's 'Zeroes, ones and twos' problem, part 1 | |
(1..100).each do |p| | |
found = false | |
q = 0 | |
until found | |
q += 1 | |
product = p * q | |
if !(product.to_s =~ /[^01]/) || q > 1000000000 | |
found = true | |
p "#{p} * #{q} = #{product}" | |
end | |
end | |
end |
This file contains 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
# A slightly less brute-force approach to Peter Winkler's 'Zeroes, ones and twos' problem, part 1 | |
# this time it only checks each multiplier for the remaining undiscovered numbers | |
require 'pp' | |
pairs = {} | |
my_range = (1..1000) | |
partners = my_range.to_a | |
q = 0 | |
until partners.empty? || q > 100000000000 | |
q += 1 | |
hits , misses = partners.partition {|p| !((p * q).to_s =~ /[^01]/)} | |
hits.each do |p| | |
pairs[p]=[q,p*q] | |
p "#{pairs.length}: #{p} * #{q} = #{p*q}" | |
end | |
partners = misses | |
end | |
pp pairs.sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And the smallest
k
, such thatn*k
contains only even digits: