So, why does it not work? Does it compile? What error messages do you get? What did you expect and how does the real thing differ from what you expected?
Try to imagine, with a concrete example, what happens when your code executes. For example, take base = 5, n = 3. Go through the code line by line, keeping the values of the variables in mind. Does it compute 5 * 4 * 3 or does it compute something else? What do you need to change to make it compute 5 * 4 * 3?
well it doesn't seem to be computing the factorial how i want it too :/
Ugh it's weird, i basically have no idea how to make the factoring stop at a given number.
So for (9,5), my result is something like 60466176 when i want it to be like 9*8*7*6*5
mike sin wrote:well it doesn't seem to be computing the factorial how i want it too :/
Ugh it's weird, i basically have no idea how to make the factoring stop at a given number.
So for (9,5), my result is something like 60466176 when i want it to be like 9*8*7*6*5
Couldn't you just have your for loop's exit condition be set to the number you want it to stop at?
You've got the loop header correct, it's just the body that's wrong.
If you pass 5 and 3, the following will be calculated:
- i == 1: result = 1 * 6 * 2 // result == 12
- i == 2: result = 12 * 6 * 3 // result == already too large ;)
...
What you want to do is this:
- i == 1: result = 1 * 6 // result * f(base)
- i == 2: result = 6 * 5 // result * f(base)
- i == 3: result = 30 * 4 // result * f(base)
That f(base) is a function that takes base (6) and returns 6 when i == 1, 5 when i == 2 and 4 when i == 3. I'm sure you can replace it with something appropriate.
Sridhar Santhanakrishnan wrote:For (9,5), the output should be 9*8*7*6*5 and for (9,4) it should be 9*8*7*6. Fred, that loop would actually multiply on 5 and 4 as well.
Atleast I hope thats what mike needs.
Hopefully this should work.
Omg
that worked :|
Now perhaps someone can explain to me how just base-- makes it work?
Thanks!