Am I perfect (number)?
Java, 255 270 bytes (Still FF in base 17)
class C{public static void main(String[]a){int i=new Integer(a[0]),k=0,l=0;a[0]=" ";for(;++k<i;)if(i%k<1){l+=k;a[0]+=k+" ";}}System.out.print("I am "+(l==i?"":"not ")+"a perfect number, because "+a[0].trim().replace(" "," + ")+" = "+l+(l==i?" == ":l<i?" < ":" > ")+i);}}
And a more readable version:
class C {
public static void main(String[] a) {
int i = new Integer(a[0]), k = 0, l = 0;
a[0] = " ";
for(; ++k<i ;){
if (i % k == 0) {
l += k;
a[0] += k + " ";
}
}
System.out.print("I am " + (l == i ? "" : "not ") + "a perfect number, because " + a[0].trim().replace(" "," + ") + " = " + l + (l == i ? " == " : l < i ? " < " : " > ") + i);
}
}
Previously didn't work for odd numbers, so I had to tweak a few things. At least I got lucky with the byte count again. :)
R, 158 163 157 153 143 141 bytes
Still room to golf this I think.
Edit: Replaced if(b<n)'<'else if(b>n)'>'else'=='
with c('<'[b<n],'>'[b>n],'=='[b==n])
. The paste(...)
is replaced with an rbind(...)[-1]
. Thanks @plannapus for a couple more bytes.
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)
Ungolfed
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
)
Test run
> 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
>
Python 2, 183 173 170 bytes
b=input();c=[i for i in range(1,b)if b%i<1];d=sum(c);print'I am %sa perfect number because %s = %d %s %d'%('not '*(d!=b),' + '.join(map(str,c)),d,'=<>='[cmp(b,d)%3::3],b)
Examples:
$ python perfect_number.py <<< 6
I am a perfect number because 1 + 2 + 3 = 6 == 6
$ python perfect_number.py <<< 12
I am not a perfect number because 1 + 2 + 3 + 4 + 6 = 16 > 12
$ python perfect_number.py <<< 13
I am not a perfect number because 1 = 1 < 13
$ python perfect_number.py <<< 100
I am not a perfect number because 1 + 2 + 4 + 5 + 10 + 20 + 25 + 50 = 117 > 100
$ python perfect_number.py <<< 8128
I am a perfect number because 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064 = 8128 == 8128
Thanks to xnor for saving 13 bytes!