programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# find factorial

mike sin
Greenhorn
Posts: 11

I had this code so I can find the factorial for only first few/given numbers. So (5,3) would be 5*4*3, (6,2) 6*5 etc
Couldn't get it work :/

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Welcome to JavaRanch.

Well... ItDoesntWorkIsUseless

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?

mike sin
Greenhorn
Posts: 11
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

W. Joe Smith
Ranch Hand
Posts: 710
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?

Rob Spoor
Sheriff
Posts: 21135
87
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.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
For example, try base = 9, n = 5

1) result = 1, i = 1
2) result = 1 * (9 * (5 - 1)) = 9 * 4 = 36, i = 2
3) result = 36 * (9 * (5 - 1)) = 36 * 36 = 1296, i = 3
4) result = 1296 * (9 * (5 - 1)) = 1296 * 36 = 46656, i = 4
5) result = 46656 * (9 * (5 - 1)) = 46656 * 36 = 1679616, i = 5
6) result = 1679616 * 36 = 60466176, i = 6
7) loop stops because i <= n is false

It's calculating (base * (n - 1)) to the power of n, which is something completely different than what you want.

mike sin
Greenhorn
Posts: 11
Hmm, thanks
and yuh, I want it to do something like this for 9,5 either:
result = 9*(9-1)*(9-2)*(9-3)*(9-4)
or
result = 9*8*7*6*5

Just to make it more clearer, for 9,4 it woud be
result= 9*8*7*6
They are both basically the same thing, not sure which is easier to do

Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
removed

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
people often forget that a loop can count down as well as up:

Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
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.

mike sin
Greenhorn
Posts: 11
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!

David Newton
Author
Rancher
Posts: 12617
Pretend you're the JVM: trace the code out by hand until you understand.

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Sridhar Santhanakrishnan wrote:Fred, that loop would actually multiply on 5 and 4 as well.

I wasn't trying to give the solution. I was trying to illustrate a point.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.