How to print numbers divisible by 7

Is there any function that allows me to check if x is divisible by any number? I need to write a repeat loop with integers from 1: 100, and also use if the function writes all numbers divisible by 7 from this range. Here is what I got so far:

x <- 1 repeat { print(x) x = x+1 if (x > 100) { break } } 

It prints only the first part of what I need.

+5
source share
1 answer

You do not have to do all this. Use the modulo %% operator and the beauty of the R vectorization.

 which(1:100 %% 7 == 0) # [1] 7 14 21 28 35 42 49 56 63 70 77 84 91 98 

Or, if you play golf code, make it even shorter ...

 which(!1:100 %% 7) # [1] 7 14 21 28 35 42 49 56 63 70 77 84 91 98 
+13
source

Source: https://habr.com/ru/post/1260901/


All Articles