Skip to content

Instantly share code, notes, and snippets.

@gabrielslau
Last active December 14, 2015 22:58
Show Gist options
  • Save gabrielslau/5162000 to your computer and use it in GitHub Desktop.
Save gabrielslau/5162000 to your computer and use it in GitHub Desktop.
Lista 05 de programação em Ruby. Funções
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
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