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 Answers
+ 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