-
Notifications
You must be signed in to change notification settings - Fork 0
/
numero_primo.rb
56 lines (48 loc) · 971 Bytes
/
numero_primo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#encoding: UTF-8
=begin
Determina la cantidad de números primos que hay desde 1
hasta un número dado.
Por ejemplo si ingreso 6 me deberá dar 4 ya que 1, 2, 3, 5 son primos.
=end
def es_primo?(numero)
contador = 0
for i in 1..numero
if numero % i == 0
contador = contador + 1
end
end
if contador > 2
return false
else
return true
end
end
def cantidad_primos(numero)
contador = 0
for i in 1..numero
if es_primo?(i) == true
contador = contador + 1
end
end
return contador
end
#--- zona de test ----
def test_es_primo?
print validate(true, es_primo?(7))
print validate(false, es_primo?(4))
end
def test_cantidad_primos
print validate(4, cantidad_primos(5))
print validate(5, cantidad_primos(9))
end
def validate (expected, value)
expected == value ? "." : "F"
end
def test
puts "Test de prueba del programa"
puts "---------------------------"
test_es_primo?
test_cantidad_primos
puts " "
end
test