Thread: Hello M.h.f - New member in need of some help.

1. Hello M.h.f - New member in need of some help.

Hello everyone,

As the title says this is my first post, as my ability in math grows I hope to give help and not just ask for it! Anyway...

Long story short I have recently began self study into computer programming and enjoying it very much. It quickly came to my attention that math (discrete math) is at work here. My travels through the net also lead me to believe that some take the "well you can still do alot without math!" approach (not pointing fingers).

However I want to learn and understand the math and am prepared to work at it. To that extent I've purchased a book (bet you didn't see that coming) explaining the math to help me get started. I'm rather annoyed (with myself?) however to say that not far in and the problems have begun...but not wanting to give up I have come to get some help.

The question is below and is algebraic in nature I believe;
This is in pseudo code by the way but hopefully wont hinder much;

if; a) n = 3
b) n = 5

begin
f := 1;
input n;
for i := 1 to n do
f := f * i
Output f;
end

The book gives answers of f = 6 and f = 120 respectively.

If someone could explain just how these answers are being reached as I feel as though I understand then upon checking with the answers that conception goes up in smoke.

Thank-you in advance for any help,
- Jamie.

2. Originally Posted by Skeith
Hello everyone,

As the title says this is my first post, as my ability in math grows I hope to give help and not just ask for it! Anyway...

Long story short I have recently began self study into computer programming and enjoying it very much. It quickly came to my attention that math (discrete math) is at work here. My travels through the net also lead me to believe that some take the "well you can still do alot without math!" approach (not pointing fingers).

However I want to learn and understand the math and am prepared to work at it. To that extent I've purchased a book (bet you didn't see that coming) explaining the math to help me get started. I'm rather annoyed (with myself?) however to say that not far in and the problems have begun...but not wanting to give up I have come to get some help.

The question is below and is algebraic in nature I believe;
This is in pseudo code by the way but hopefully wont hinder much;

if; a) n = 3
b) n = 5

begin
f := 1;
input n;
for i := 1 to n do
f := f * i
Output f;
end

The book gives answers of f = 6 and f = 120 respectively.

If someone could explain just how these answers are being reached as I feel as though I understand then upon checking with the answers that conception goes up in smoke.

Thank-you in advance for any help,
- Jamie.
Hi Skeith,

The value of f is initially set at 1.

The input command allows for n = 3

The loop command 'for i = 1 to n do f:= f*i' does the following:

First pass: f = 1, i = 1, f*i = 1
Second pass: f = 1, i = 2, f*i = 2
Third pass: f = 2, i = 3, f*i = 6

f is replaced each pass with the the value of the new product. The final product is 6. Thus, output 6.

The same thing happens for an input of 5, except the loop has 5 passes

First pass: f = 1, i = 1, f*i = 1
Second pass: f = 1, i = 2, f*i = 2
Third pass: f = 2, i = 3, f*i = 6
Fourth pass: f = 6, i = 4, f*i = 24
Fifth pass: f = 24, i = 5, f*i = 120

3. Thankyou for the reply masters! I feel I have a better grip of it already, I believe I also see the flaw in my approach. I was not allowing for the value of f to be updated thus it was staying at one (silly I know). I'll write that post out and slip it in my book to re-read - just wanted to thank you for the response!