views:

77

answers:

1

it factors a # 25=5

+2  A: 

Unfortunately I don't know how to code in the "facebook" language as your tags indicate :-) And, in any case, it's looks suspiciously like homework to which I usually only supply pseudo-code. Here's some to start you on your way:

def factorial (n):
    error if  n < 1
    answer = 1
    do while n > 1:
        answer = answer * n
        n = n - 1
    return answer

It's your job to convert it to your actual language (C according to the title) and to understand why it works.

To that end, get yourself a piece of paper and make two columns labelled n and answer.

Then run through that pseudo-code in your head with various starting values of n, checking and changing the values of n and answer as you go. You'd be surprised how quickly you can begin to think like a computer with a bit of practice. Start with 5, I'll even make it easy for you:

+-------+--------+
|   n   | answer |
+-------+--------+
|   5   |        |
|       |        |
|       |        |
|       |        |
|       |        |
|       |        |
|       |        |
|       |        |
|       |        |
|       |        |
|       |        |
+-------+--------+

If I've misunderstood your requirements (your title says factorial but your text seems to indicate factor generation, two totally different things), the following pseudo-code may help. Same deal, run it through your head with columns for n, tester, temp and another column for the output from the print statements:

def factors (n):
    error if n < 1
    tester = 2
    do while tester <= n:
        temp = int (n / tester)
        temp = temp * tester
        if temp = n:
            print tester
            n = n / tester
        tester = tester + 1
paxdiablo
Tag's fixed. Enjoy. :)
cHao