Created
January 27, 2011 22:11
-
-
Save heronmedeiros/799392 to your computer and use it in GitHub Desktop.
Implementacao dos metodos na classe fixnum usando class_eval
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
Fixnum.class_eval do | |
def primo? | |
return true if self == 1 #"primo" | |
return false if self.nil? | |
number = self.abs | |
div = 0 | |
number.downto(1) do |n| | |
if number % n == 0 then | |
div = div+1 | |
end | |
end | |
if div == 2 then | |
true #number#"#{number}"# é primo" | |
else | |
false | |
end | |
end | |
def divisores | |
div=[] | |
aux = 1 | |
k = 0 | |
2.upto(self) do |m| | |
aux = aux +1 | |
div[m] = aux if self % aux == 0 | |
end | |
div.uniq - [nil] | |
end | |
def divisores_primos | |
ar =[] | |
a = self.divisores #-[nil] | |
a.each { |e| ar << e if e.primo? } | |
ar | |
end | |
end | |
p 80.divisores | |
p 80.divisores_primos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment