APPLIED STATISTICS 2
USE R CODE ! SHOW R CODE!
Write a function to calculate the sum of cubes from 1 to n, but skip the multiple of 5. Say, if n=10, the result is 1^3+2^3+3^3+4^3+6^3+7^3+8^3+9^3. The input is the value of n, the output is the summation. (you need if statement to check whether a number is a multiple of 5.In R, a%%b is the remainder for a divided by b. So, we have 10%%5=0)
APPLIED STATISTICS 2
USE R CODE ! SHOW R CODE!
#Function name =Cube_excl_mult_5
Cube_excl_mult_5 = function(n){
x=1:n # vector of number from 1to n
y=which(x%%5!=0) #Identifying position where we do not
have multiple of 5
x_new=x[y] # considering only those where we do not
have multiple of 5
#Output
return(sum(x_new^3))
}
____________________________________
##E.g
> Cube_excl_mult_5(4)
[1] 100
> Cube_excl_mult_5(5)
[1] 100
____________________________________________________________
If you have any doubt please let me know through comment
Please give positive vote if you find this solution helpful. Thank
you!
Get Answers For Free
Most questions answered within 1 hours.