Skip to content

Instantly share code, notes, and snippets.

@piotrekwitkowski
Last active July 7, 2019 15:10
Show Gist options
  • Save piotrekwitkowski/3a2e0352edc22e7ebd4e15dc821369b2 to your computer and use it in GitHub Desktop.
Save piotrekwitkowski/3a2e0352edc22e7ebd4e15dc821369b2 to your computer and use it in GitHub Desktop.
### Task 10
iterative_sum <- function (n) {
sum <- 0
for (i in 1: n) {
sum = sum + i
}
return(sum)
}
recursive_sum <- function (n) {
sum <- 0
if (n > 0) {
return(n + recursive_sum(n - 1))
} else {
return(0)
}
return(sum)
}
cat("Iterative sum:", iterative_sum(100), "\n")
cat("Recursive sum:", recursive_sum(100), "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment