Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created December 1, 2015 02:30
Show Gist options
  • Save primaryobjects/cab28cf373157ee68739 to your computer and use it in GitHub Desktop.
Save primaryobjects/cab28cf373157ee68739 to your computer and use it in GitHub Desktop.
[2015-11-30] Challenge #243 [Easy] Abundant and Deficient Numbers
#
# https://www.reddit.com/r/dailyprogrammer/comments/3uuhdk/20151130_challenge_243_easy_abundant_and/
#
divisors <- function(x){
# Vector of numberes to test against
y <- seq_len(x)
# Modulo division. If remainder is 0 that number is a divisor of x so return it
y[ x%%y == 0 ]
}
abundef <- function(n) {
div <- divisors(n)
sumDiv <- sum(div)
n2 <- 2 * n
if (sumDiv < n2) {
# Deficient.
paste(n, 'deficient')
}
else {
paste(n, 'abundant by', sumDiv - n2)
}
}
input <- c(111, 112, 220, 69, 134, 85)
sapply(input, abundef)
111 deficient
112 abundant by 24
220 abundant by 64
69 deficient
134 deficient
85 deficient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment