Back

t(t(a[((!outer(a,a,“%%”))%*%a)>=2*a]))

Code Golf

The goal in code golf is to produce a program that solves a problem with as few characters as possible. Here is a code golf problem where R does pretty well.

Abundant Numbers

This problem comes from Here.

An abundant number is a number for which the sum of its proper divisors (divisors not including the number itself) is greater than the number itself. For example **12* is abundant because its proper divisors are 1, 2, 3, 4, and 6 which add up to 16.*

Print all the abundant numbers from **1* to 200 inclusive, each on their own line.*

Code: (46 Characters)

This code takes advantage of R's matrix functions and operators.

a=1:200
t(t(a[((!outer(a,a,"%%"))%*%a)>=2*a]))
##       [,1]
##  [1,]    6
##  [2,]   12
##  [3,]   18
##  [4,]   20
##  [5,]   24
##  [6,]   28
##  [7,]   30
##  [8,]   36
##  [9,]   40
## [10,]   42
## [11,]   48
## [12,]   54
## [13,]   56
## [14,]   60
## [15,]   66
## [16,]   70
## [17,]   72
## [18,]   78
## [19,]   80
## [20,]   84
## [21,]   88
## [22,]   90
## [23,]   96
## [24,]  100
## [25,]  102
## [26,]  104
## [27,]  108
## [28,]  112
## [29,]  114
## [30,]  120
## [31,]  126
## [32,]  132
## [33,]  138
## [34,]  140
## [35,]  144
## [36,]  150
## [37,]  156
## [38,]  160
## [39,]  162
## [40,]  168
## [41,]  174
## [42,]  176
## [43,]  180
## [44,]  186
## [45,]  192
## [46,]  196
## [47,]  198
## [48,]  200