I am trying to write a function that takes a positive real number and continues to add members of the harmonic series until the total sum exceeds the original argument. I need my function to display the total number of series terms that have been added.
Here is my code:
harmonic<-function(n){
x<-c(0,1)
while (length(x) < n) {
position <- length(x)
new <- 1/(x[position] + x[position-1])
x <- c(x,new)
}
return(x)
}
I apologize for the errors in my code, unfortunately, I only worked with R for a month, and this is the first time I use a while loop, and I could not find any useful information.
Thank you, I am very grateful for your help.
source
share