Home > Back-end >  How to create a function that given a positive integer, returns a vector with the numbers multiples
How to create a function that given a positive integer, returns a vector with the numbers multiples

Time:11-16

I have to create a mult5 function that, given a positive integer, returns a vector with the numbers multiples of 5 that exist less than or equal to that number. For example, for the number 17, mult5 (17) should return the vector (0, 5, 10, 15). I can't use any type of loop or sapply / lapply.

I think i can do it with the seq function but I don't know how. That's what I've tried:

mult5 <- function(numero){
  modulo = numero %% 5 == 0
  seq = seq(from = 0, to = numero, by = modulo)
}

But I think I can't put variables inside the sequence function and throw me an error. Can someone explain or tell me what I could do?

The test:

is.list(mult5(24)) == FALSE
all(mult5(24) == c(0, 5, 10, 15, 20))
check.not.command("for", mult5)
check.not.command("while", mult5)

I cant use for/while.

Error msg:

 Error in seq.default(from = 0, to = numero, by = modulo) : 
  invalid '(to - from)/by' 

CodePudding user response:

The seq function already has the behaviour you need if numero is not a multiple of 5, so you can streamline your code to:

mult5 <- function(numero){
  seq(from = 0, to = numero, by = 5)
}

CodePudding user response:

Thats mine example:

def check(number: int):
    lst = list(range(0, number, 5))
    print(lst)

Edited with @diggusbickus suggestion

  • Related