Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get all numbers 3 digits, divisible by 3 that follow this formula, a + 3b + c where a != 0
# Get all numbers 3 digits, divisible by 3 that follow this formula
# a + 3b + c where a != 0
library(tidyr)
count <- 0
n <- c()
for( i in 100:999) {
digits <- as.integer(substring(i, seq(nchar(i)), seq(nchar(i))))
formula <- (digits[1] + 3*digits[2] + digits[3])
if ( ( formula %% 3 ) == 0 ) {
count = count + 1
n[count] <- i
}
}
print(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment