R语言coding exercises汇总1
plus exercises
These coding exercises are from course --“Marketing analysis”.
The answers were written by myself.
Try to write a blog to record learning content.
ex1
The natural numbers below 10 that are multiples of 3 or 5, are 3, 5, 6 and 9. The sum of these multiples is
23. Find the sum of all the multiples of 3 or 5 below 1000.
#solution1
ex1=list()
for(i in 1:1000){
if(i%%3==0|i%%5==0){
ex1[[length(ex1)+1]]=i
}
}
solution=sum(unlist(ex1))
print(solution)
#solution2
ex1_1=matrix(data=NA,nrow = 1000,ncol = 1)
for(i in 1:1000){
if(i%%3==0|i%%5==0){
ex1_1[i,1]=i
}
}
solution=sum(ex1_1[is.na(ex1_1)==FALSE])
print(solution)
ex2
Each new term in the Fibonacci sequence is generated by adding