Skip to content

Instantly share code, notes, and snippets.

@AlexNisnevich
Created April 26, 2013 11:37
Show Gist options
  • Save AlexNisnevich/5466595 to your computer and use it in GitHub Desktop.
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
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