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)

15th Apr 2023, 1:01 PM
Cédric De Craim
Cédric De Craim - avatar
3 Réponses
+ 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
15th Apr 2023, 1:07 PM
A͢J
A͢J - avatar
+ 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)) }
15th Apr 2023, 1:31 PM
A͢J
A͢J - avatar
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)
15th Apr 2023, 1:10 PM
Cédric De Craim
Cédric De Craim - avatar