ABC

  abc.go.com
  abc.go.com

Interview Question

Systems Analyst Interview

Write a function that checks if a number's divisors sum up

  to that number, i.e. whether it's a perfect number or not.
Answer

Interview Answer

1 Answer

0

This is what my function ended up looking like. I know it's a naive implementation but I figured that solving it was more important than efficiency. How wrong I was...

bool isPerfect(int value) {
    int i, total = 0;
    for (i = 1; i <= value; i++) {
        if (value % i == 0) {
            total += i;
        }
    }
    if (total == value) {
        return TRUE;
    } else {
        return FALSE;
    }
}

When I was done, the guy asked me what I could do to improve the efficiency of the programme. For example, how would you make it so you could calculate, say, 4,000,000 trivially? I stumbled on this part and, after a few minutes of no progress, he decided to give me a different question.

Interview Candidate on Apr 20, 2011

Add Answers or Comments

To comment on this, Sign In or Sign Up.