Forums Register Login

find factorial

+Pie Number of slices to send: Send


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 :/
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
removed
+Pie Number of slices to send: Send
people often forget that a loop can count down as well as up:

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
Pretend you're the JVM: trace the code out by hand until you understand.

Normally we don't give answers--we help you come up with your own. Since it's too late I'm not deleting the freebie.
+Pie Number of slices to send: Send
 

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.
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 946 times.
Similar Threads
Introduction and a question about recursion
help in solving recursive problems??
Recursion
recursion situation baffles me
Is my prof trolling me? (factorials, loops)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:04:13.