Skip to content

Instantly share code, notes, and snippets.

@teamon
Created January 7, 2009 16:19
Show Gist options
  • Save teamon/44320 to your computer and use it in GitHub Desktop.
Save teamon/44320 to your computer and use it in GitHub Desktop.
# zad5 http://dl-client3.getdropbox.com/u/70986/infa/matura2007/inf_pr_cz2.pdf
class Fixnum
def first?
!(2..Math.sqrt(self)).any? {|i| self % i == 0}
end
def sum_is_first?
self.to_s.split(//).inject(0) {|s, e| s+=e.to_i}.first?
end
def binary_sum_is_first?
self.to_s(2).split(//).inject(0) {|s, e| s+=e.to_i}.first?
end
def super_first?
first? && sum_is_first?
end
def super_b_first?
super_first? && binary_sum_is_first?
end
end
puts "a)"
[(2..1000), (100..10000), (1000..100000)].each do |range|
puts " #{range} => #{range.reject{|e| !e.super_b_first?}.size}"
end
puts "b) " + (100..10000).reject{|e| !e.sum_is_first?}.size.to_s
sum = (100..10000).reject{|e| !e.super_b_first?}.inject(0){|s, e| s+=e}
puts "c) #{sum} => #{sum.first?}"
# a)
# 2..1000 => 51
# 100..10000 => 249
# 1000..100000 => 1262
# b) 2976
# c) 1006623 => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment