Skip to content

Instantly share code, notes, and snippets.

@kebbbnnn
Created October 28, 2016 12:24
Show Gist options
  • Save kebbbnnn/f15d5dfa251f1b261b194c1342caeafd to your computer and use it in GitHub Desktop.
Save kebbbnnn/f15d5dfa251f1b261b194c1342caeafd to your computer and use it in GitHub Desktop.
# Problem 1
plussum <- function(v){
r <- 0
for(i in 1:length(v)){
r <- r + if(v[i] > 0 ) v[i] else 0
}
return(r)
}
# test inputs for prob 1
print(plussum(c(1, -1, -1, 2)))
# Problem 2
intcount <- function(v, k){
counter <- 0
for(i in 1:length(v)){
counter = counter + if(v[i] == k) 1 else 0
}
return(counter)
}
# test inputs for prob 2
print(intcount(c(1,1,1,1,1,2,4,5,1), 1))
# Problem 3
troot <- function(a, b, c){
if(((b^2) - (4*a*c)) > 0) return("distinct roots")
else if((b^2) == (4*a*c)) return("coincidental roots")
else if((b^2) < (4*a*c)) return("complex roots")
}
# test inputs for prob 3
print(troot(1,2,3))
# Problem 4
div <- function(k){
counter <- 0
for(i in 2:k-1){
if(k %% i == 0){
print(i)
counter = counter + 1
}
}
return(counter)
}
# test inputs for prob 4
print( div(100))
# Problem 5
superfun <- function(n, m){
res <- 0
for(r in 1:n){
for(s in 1:m){
res = res + ((s^2) / (10 + (4*(r^3))))
}
}
return(res)
}
# test inputs for prob 5
print(superfun(10, 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment