R,158个 163 157 153 143 141字节
我认为仍然有打高尔夫球的空间。
编辑:替换if(b<n)'<'else if(b>n)'>'else'=='
为c('<'[b<n],'>'[b>n],'=='[b==n])
。将paste(...)
替换为rbind(...)[-1]
。感谢@plannapus多了两个字节。
n=scan();a=2:n-1;b=sum(w<-a[!n%%a]);cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)
不打高尔夫球
n<-scan() # get number from stdin
w<-which(!n%%1:(n-1)) # build vector of divisors
b=sum(w) # sum divisors
cat('I am', # output to STDOUT with a space separator
'not'[b!=n], # include not if b!=n
'a perfect number, because',
rbind('+',w)[-1], # create a matrix with the top row as '+', remove the first element of the vector
'=',
b, # the summed value
c( # creates a vector that contains only the required symbol and ==
'<'[b<n], # include < if b<n
'>'[b>n], # include > if b>n
'=='
)[1], # take the first element
n # the original number
)
测试运行
> n=scan();b=sum(w<-which(!n%%1:(n-1)));cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)
1: 6
2:
Read 1 item
I am a perfect number, because 1 + 2 + 3 = 6 == 6
> n=scan();b=sum(w<-which(!n%%1:(n-1)));cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)
1: 12
2:
Read 1 item
I am not a perfect number, because 1 + 2 + 3 + 4 + 6 = 16 > 12
> n=scan();b=sum(w<-which(!n%%1:(n-1)));cat('I am','not'[b!=n],'a perfect number, because',rbind('+',w)[-1],'=',b,c('<'[b<n],'>'[b>n],'==')[1],n)
1: 13
2:
Read 1 item
I am not a perfect number, because 1 = 1 < 13
>