Last active
December 14, 2015 22:58
-
-
Save gabrielslau/5162000 to your computer and use it in GitHub Desktop.
Lista 05 de programação em Ruby. Funções
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
def intervalo_a_b (n1,n2) | |
if(n1.class != Fixnum or n2.class != Fixnum) then | |
-1 | |
else | |
if(n1 < n2) then | |
(n1..n2).to_a | |
else | |
(n2..n1).to_a | |
end | |
end | |
end |
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
def primos_entre_si? (n1=nil, n2=nil) | |
if( n1 == nil or n2 == nil ) then return false end | |
n1 = n1.to_i | |
n2 = n2.to_i | |
sequencia_n1 = if(n1 >= 1) then (1..n1).to_a else (n1..1).to_a end | |
sequencia_n2 = if(n2 >= 1) then (1..n2).to_a else (n2..1).to_a end | |
divisores_n1 = sequencia_n1.select do |i| | |
i!=0 and n1%i==0 | |
end | |
divisores_n2 = sequencia_n2.select do |i| | |
i!=0 and n2%i==0 | |
end | |
divisores_em_comum = 0 | |
for dn1 in divisores_n1 do | |
for dn2 in divisores_n2 do | |
if(dn1 == dn2) then divisores_em_comum += 1 end | |
end | |
end | |
divisores_em_comum == 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment