Created
April 26, 2013 11:37
-
-
Save AlexNisnevich/5466595 to your computer and use it in GitHub Desktop.
Calculates lucky numbers up to a given limit, and prints the list of unlucky numbers removed for each lucky number
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
limit = 1000 | |
nums = (1..limit).to_a | |
([1] + nums).each do |a| | |
lucky_num = nums[a] | |
unlucky_nums = nums.values_at(* nums.each_index.select {|i| (i+1) % lucky_num == 0}) | |
if unlucky_nums.size == 0 | |
break | |
else | |
puts "#{lucky_num}-unlucky numbers: #{unlucky_nums}" | |
end | |
nums.each_index.select {|i| (i+1) % lucky_num == 0}.reverse.each {|i| nums.delete_at i} | |
end | |
puts "Lucky numbers from 1 to #{limit}: #{nums}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment