0
Sum in range in R
Language R Here is my answer but it is wrong can you help me ? input <- readLines('stdin') x <- as.integer(input[1]) y <- as.integer(input[2]) rangeSum <-function(x,y) { for (input in x:y){ return (x+y)} } result <- rangeSum(x, y) print(result)
3 Antworten
+ 6
Cédric De Craim
You are returning value inside the loop so after first iteration loop will be break and first sum value will be return
+ 5
Cédric De Craim
If you use 'sum' function then for loop is useless
You can just do this:
rangeSum <-function(x,y)
{
return (sum(x:y))
}
0
I found the solution just now
input <- readLines('stdin')
x <- as.integer(input[1])
y <- as.integer(input[2])
rangeSum <-function(x,y)
{
for (input in x:y){
return (sum(x:y))}
x=x+y
}
result <- rangeSum(x, y)
print(result)