26
2
This is my first challenge!
Background
Perfect number is a positive integer, that is equal to the sum of all its divisors, except itself.
So 6 is perfect number, since 1 + 2 + 3 = 6.
On the other hand 12 is not, because 1 + 2 + 3 + 4 + 6 = 16 != 12.
Task
Your task is simple, write a program, which will, for given n, print one of these messages:
I am a perfect number, because
d1 + d2 + ... + dm = s == n
I am not a perfect number, becaused1 + d2 + ... + dm = s [<>] n
Where
d1, ... dm are all divisors of n except for n.
s is the sum of all divisors d1, ..., dm (again, without n).
[<>] is either < (if s < n) or > (if s > n).
Examples
For n being 6: "I am a perfect number, because 1 + 2 + 3 = 6 == 6"
For n being 12: "I am not a perfect number, because 1 + 2 + 3 + 4 + 6 = 16 > 12"
For n being 13: "I am not a perfect number, because 1 = 1 < 13"
Rules
nis not bigger than your language's standardint.- You can read
nfrom standard input, from command line arguments or from a file. - Output message has to be printed on standard output and no additional characters can appear in the output (it may have trailing whitespace or newline)
- You may not use any built-ins or library functions which would solve the task (or its main part) for you. No
GetDivisors()or something like that. - All other standard loopholes apply.
Winner
This is code-golf so shortest code in bytes wins!
@orlp It is not, I edited the challenge, thanks for that. – Zereges – 2015-09-15T21:57:15.903
7Why do you use
=and==in the same equation? That makes no sense. It should bed1 + d2 + ... + dm = s = nIMO. – orlp – 2015-09-15T22:06:16.053Could you give some example input and output, for example with inputs 6 and 12? – Zgarb – 2015-09-15T22:08:43.290
@orlp first
=is assignment. Second==is comparism. – Zereges – 2015-09-15T22:10:58.50014@Zereges That's nonsensical. There is nothing being assigned. Only compared. – orlp – 2015-09-15T22:12:53.110
1@orlp It is intended. – Zereges – 2015-09-15T22:24:18.307
Related: http://codegolf.stackexchange.com/questions/49718/calculate-the-first-n-perfect-numbers
– flawr – 2015-09-16T20:15:55.203@flawr Related, but closed as unclear. – Zereges – 2015-09-16T21:38:48.757
What is being assigned? I don't see any variables. I only see constants, and you can't assign anything to a constant. – corsiKa – 2015-09-16T22:43:19.610
@corsiKa Ok, so I'll tell it in other way. First
=is elementary school's, it means, computing the value of sum of divisors. The second==is to show off, that the value is the same. – Zereges – 2015-09-16T23:03:55.313Does it matter if there are extra spaces in the list of numbers, e.g.
I am a perfect number, because<space><space>1<space>+<space><space>2 +<space><space>4<space>+<space><space>7<space>+<space>14 == 28– Tom Carpenter – 2015-09-25T16:07:31.050@TomCarpenter Yes, it matters. You can have trailing whitespace/newline, but no whitespaces in the middle of output. – Zereges – 2015-09-25T17:33:33.797
I figured as much. Fortunately I found a way to combat it in my MATLAB code after I left the comment. :) – Tom Carpenter – 2015-09-25T17:37:03.537
I wrote a four byte answer... and then I realized how the output is supposed to work. – Unrelated String – 2019-03-06T07:04:50.540