1)
a)
menor_2 = function(a,b){
if(a < b) return(a)
else return(b)
}
menor_2(5,10)
## [1] 5
b)
menor_3 = function(a,b,c){
if(a < b && a < c) return(a)
else if(b < a && b < c) return(b)
else return(c)
}
menor_3(100,20,3)
## [1] 3
c)
menor_c = function(vet){
aux = vet[1]
tamanho_vet = length(vet)
for(numero in vet[2:tamanho_vet]){
if(numero < aux){
aux = numero
}
}
return(aux)
}
menor_c(c(3,0.1,1))
## [1] 0.1
2)
triangulo = function(a,b,c){
if(a + b > c && a + c > b && c + b > a){
if(a == b && b == c) {
print("O triângulo é equilátero")
}
else if(a == b && b != c || a == c && c != b|| b == c && b != a){
print("O triângulo é isósceles")
}
else{
print("O triângulo é escaleno")
}
} else{
stop("Não é possível formar um triângulo")
}
}
triangulo(10,6,5)
## [1] "O triângulo é escaleno"
## [1] "O triângulo é equilátero"
## [1] "O triângulo é isósceles"
3)
v = c(1.0,3.2,-2.1,10.6,0.0,-1.7,-0.5)
positivos = function(vet){
aux = 0
for (numero in vet) {
if(numero > 0) aux = aux + 1
}
return(aux)
}
positivos(v)
## [1] 3
4)
menor_a = function(vet,num){
aux = 0
for (numero in vet) {
if(numero < num) aux = aux +1
}
return(aux)
}
menor_a(v,0)
## [1] 3
5)
a)
multiplos_1 = function(n,m){
if((n<=0)||(n%%1 != 0)){
stop("n tem que ser um natural")
}
if((m<=0)||(m%%1 != 0)){
stop("m tem que ser um natural")
}
else{
aux = c()
for(i in 1:n){
aux[i] = m*i
}
return(aux)
}
}
multiplos_1(5,10)
## [1] 10 20 30 40 50
## Error in multiplos_1(5.5, 10): n tem que ser um natural
b)
multiplos_2 = function(m,k){
if(m > k) return(c())
aux = c(m)
while(TRUE){
n = length(aux)
prox = aux[n] + m
if(prox >= k){
return(aux)
}else{
aux[n+1] = prox
}
}
}
multiplos_2(10,9)
## NULL
## [1] 10 20 30 40 50 60 70 80
c)
multiplos_3 = function(m,k){
if(m > k) return(0)
aux = c(m)
qtd = 1
while(TRUE){
n = length(aux)
prox = aux[n] + m
if(prox >= k){
return(qtd)
}else{
aux[n+1] = prox
qtd = qtd + 1
}
}
}
multiplos_3(10,20)
## [1] 1
## [1] 0
d)
#a)
#Argumentos : n,m
#Variáveis locais : aux
#b)
#Argumentos : m,k
#Variáveis locais: aux, n, prox
#c)
#Argumentos : m,k
#Variáveis locais: aux, n, prox, qtd
#Todas variáveis locais foram iniciadas dentro das funções
6)
a)
cria_matriz_1 = function(n){
if((n<=0)||(n%%1 != 0)){
stop("n tem que ser um natural")
}
matriz = matrix(0,nrow= n, ncol = n)
for(i in 1:n){
if(i%%2 == 0){
matriz[i,] = 2
}else{
matriz[i,] = 1
}
}
return(matriz)
}
cria_matriz_1(3)
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 2 2 2
## [3,] 1 1 1
b)
cria_matriz_2 = function(n){
if((n<=0)||(n%%1 != 0)){
stop("n tem que ser um natural")
}
matriz = matrix(0,nrow= n, ncol = n)
for(i in 1:n){
matriz[,i] = i
}
return(matriz)
}
cria_matriz_2(3)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 1 2 3
## [3,] 1 2 3
c)
cria_matriz_3 = function(n){
if((n<=0)||(n%%1 != 0)){
stop("n tem que ser um natural")
}
matriz = matrix(0,nrow= n, ncol = n)
for(i in 1:n){
matriz[i,i] = i
}
return(matriz)
}
cria_matriz_3(3)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 2 0
## [3,] 0 0 3
7)
a)
diagonaliza = function(vet){
if(!is.numeric(vet)) stop("O vetor precisa ser somente de números reais")
n = length(vet)
matriz = matrix(0,ncol = n, nrow = n)
for(i in 1:n){
matriz[i,i] = vet[i]
}
return(matriz)
}
v = c(1.0,3.2,-2.1,10.6,0.0,-1.7,-0.5)
diagonaliza(v)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1 0.0 0.0 0.0 0 0.0 0.0
## [2,] 0 3.2 0.0 0.0 0 0.0 0.0
## [3,] 0 0.0 -2.1 0.0 0 0.0 0.0
## [4,] 0 0.0 0.0 10.6 0 0.0 0.0
## [5,] 0 0.0 0.0 0.0 0 0.0 0.0
## [6,] 0 0.0 0.0 0.0 0 -1.7 0.0
## [7,] 0 0.0 0.0 0.0 0 0.0 -0.5
b)
cria_matriz_4 = function(vet){
if(!is.numeric(vet)) stop("O vetor precisa ser somente de números reais")
n = length(vet)
matriz = matrix(0,ncol = n, nrow = n)
for(i in 1:n){
matriz[,i] = vet
}
return(matriz)
}
cria_matriz_4(v)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1.0 1.0 1.0 1.0 1.0 1.0 1.0
## [2,] 3.2 3.2 3.2 3.2 3.2 3.2 3.2
## [3,] -2.1 -2.1 -2.1 -2.1 -2.1 -2.1 -2.1
## [4,] 10.6 10.6 10.6 10.6 10.6 10.6 10.6
## [5,] 0.0 0.0 0.0 0.0 0.0 0.0 0.0
## [6,] -1.7 -1.7 -1.7 -1.7 -1.7 -1.7 -1.7
## [7,] -0.5 -0.5 -0.5 -0.5 -0.5 -0.5 -0.5
c)
cria_matriz_5 = function(vet){
if(!is.numeric(vet)) stop("O vetor precisa ser somente de números reais")
n = length(vet)
matriz = matrix(0,ncol = n, nrow = n)
for(i in 1:n){
matriz[i,] = vet
}
return(matriz)
}
cria_matriz_5(v)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1 3.2 -2.1 10.6 0 -1.7 -0.5
## [2,] 1 3.2 -2.1 10.6 0 -1.7 -0.5
## [3,] 1 3.2 -2.1 10.6 0 -1.7 -0.5
## [4,] 1 3.2 -2.1 10.6 0 -1.7 -0.5
## [5,] 1 3.2 -2.1 10.6 0 -1.7 -0.5
## [6,] 1 3.2 -2.1 10.6 0 -1.7 -0.5
## [7,] 1 3.2 -2.1 10.6 0 -1.7 -0.5
8)
a)
pa_1 = function(x0){
if(!is.numeric(x0)) stop("X0 não é um número real")
vet = c(x0)
for(i in 2:10){
vet[i] = vet[i-1] + 3
}
return(vet)
}
pa_1(5)
## [1] 5 8 11 14 17 20 23 26 29 32
b)
pa_2 = function(x0,r){
if(!is.numeric(x0)) stop("X0 não é um número real")
vet = c(x0)
for(i in 2:10){
vet[i] = vet[i-1] + r
}
return(vet)
}
pa_2(5,5)
## [1] 5 10 15 20 25 30 35 40 45 50
c)
pa = function(x0,r,n){
if(!is.numeric(x0)) stop("X0 não é um número real")
if(n%%1 != 0) stop("n deve ser um número inteiro")
vet = c(x0)
for(i in 2:n){
vet[i] = vet[i-1] + r
}
return(vet)
}
pa(5,5,3)
## [1] 5 10 15
d)
soma_pa = function(x0,r,n){
valores = pa(x0,r,n)
return(sum(valores))
}
soma_pa(6,3,5)
## [1] 60
e)
#pa
#Argumentos : x0,r,n
#Variáveis locais : vet
#soma_pa
#Argumentos : x0,r,n
#Variáveis locais: valores
#Todas variáveis locais foram iniciadas dentro das funções
9)
a)
fibo = function(n){
if((n<=0)||(n%%1 != 0)){
stop("n tem que ser um natural")
}
vet = c(1,1)
if(n <=2) return(1)
for(i in 3:n){
vet[i] = vet[i-1]+vet[i-2]
}
return(vet)
}
fibo(10)
## [1] 1 1 2 3 5 8 13 21 34 55
b)
fibo_2 = function(k){
if((k<=0)||(k%%1 != 0)){
stop("k tem que ser um natural")
}
vet = c(1,1)
i = 3
while(vet[i-1]+vet[i-2] < k){
vet[i] = vet[i-1]+vet[i-2]
i = i +1
}
return(vet)
}
fibo_2(15)
## [1] 1 1 2 3 5 8 13
c)
fibo_3 = function(k){
if((k<=0)||(k%%1 != 0)){
stop("k tem que ser um natural")
}
vet = c(1,1)
if(k == 1) return(0)
i = 3
qtd = 2
while(vet[i-1]+vet[i-2] < k){
vet[i] = vet[i-1]+vet[i-2]
qtd = qtd + 1
i = i +1
}
return(qtd)
}
fibo_3(10)
## [1] 6
10)
a)
#v = c(0,0,0,0,0)
#vet = (1,2,3,0,0)
b)
#Iniciaria localmente a variável v para que a ela fosse criada corretemente
f <- function(n){
v = c()
for(i in 1:n){
v[i] <- i
}
return(v)
}
v <- c(0,0,0,0,0)
vet <- f(3) ; vet
## [1] 1 2 3
11)
a)
pg_1 = function(x0,q,n){
if(!is.numeric(x0)) stop("X0 não é um número real")
if(n%%1 != 0) stop("n deve ser um número inteiro")
vet = c(x0)
for(i in 2:n){
vet[i] = vet[i-1] * q
}
return(vet)
}
pg_1(5,2,10)
## [1] 5 10 20 40 80 160 320 640 1280 2560
b)
pg_2 = function(x0,q,m){
if(!is.numeric(x0)) stop("X0 não é um número real")
if((m<=0)||(m%%1 != 0)) stop("m deve ser um número natural")
vet = c(x0)
for(i in 2:m){
vet[i] = vet[i-1] * q
}
return(sum(vet))
}
pg_2(5,2,10)
## [1] 5115
e)
#Converge pois é uma série geométrica com |p| < 1 se n -> +Inf e i começa em 0
#Converge para (1/(1 - 1/2)) - 1
1/(1- 1/2) - 1
## [1] 1
f)
options(digits = 22)
pg_2(1/2,1/2,30)
## [1] 0.99999999906867743
## [1] 0.99999999997089617
## [1] 0.99999999999999911
12)
b)
verifica_primo = function(k){
aux = 0
for(i in 1:k){
if(k%%i==0){
aux = aux +1
}
}
if(aux==2) return(TRUE)
else return(FALSE)
}
verifica_primo(1)
## [1] FALSE
## [1] TRUE
## [1] FALSE
## [1] TRUE
c)
n_primos = function(n){
primos = c()
i = 1
while(length(primos) < n){
if(verifica_primo(i)==TRUE) primos = c(primos,i)
i = i + 1
}
return(primos)
}
n_primos(10)
## [1] 2 3 5 7 11 13 17 19 23 29
a)
fat_primos = function(k){
if((k<=0)||(k%%1 != 0)) stop("k deve ser um número natural")
divisoes = c(0)
primos = c()
while(divisoes[length(divisoes)] != 1){
for(i in 1:k){
if(k%%i == 0){
if(verifica_primo(i)==TRUE) {
primos = c(primos,i)
k = k/i
divisoes = c(divisoes, k)
break
}
}
}
}
return(primos)
}
fat_primos(3)
## [1] 3
## [1] 2
## [1] 3 7 17