Last active
August 29, 2015 14:19
-
-
Save auduchinok/5644a27e3f3c5141ade6 to your computer and use it in GitHub Desktop.
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
# Eugene Auduchinok, 2015 | |
# Solution for some Stepic task | |
require 'prime' | |
def count_primes | |
count = 0 | |
(0...512).each do |index| | |
bits = to_bits index | |
nums = map_to_task_nums(bits) | |
count += 1 if rows_and_cols(nums).all?{|x| Prime.prime?(x)} | |
end | |
count | |
end | |
def to_bits(number) | |
(number + 512).to_s(2).split("").drop(1).map(&:to_i) | |
end | |
def map_to_task_nums(bits) | |
nums = bits.map do |bit| | |
case bit | |
when 0 | |
1 | |
when 1 | |
5 | |
end | |
end | |
end | |
def rows_and_cols(nums) | |
result = [] | |
(0..2).each do |index| | |
result << (nums[index] + nums[index + 3] + nums[index + 6]) | |
result << (nums[index * 3] + nums[(index * 3) + 1] + nums[(index * 3) + 2]) | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment