# Factorial fυnction and combinations fυnction

#1

I saw there's already another topic on doing factorials in Hopscotch but I wanted to share this which uses the ideas for inputs and outputs for functions in Hopscotch too that I'd mentioned earlier.

### What's factorial?

Factorial of a number is when you multiply all numbers from 1 to that number itself. A notation used for factorial is by adding an exclamation mark to the end of a number.

``4! = 4*3*2*1 = 24 // 4 factorial``
More background information

### Doing factorial using iteration:

Pseudocode (planning in an informal hybrid of natural language and coding terms)

``````factorial(number):
result = 1
for i = 1 to i = number:
result = result * i
end``````

### Factorial using recursion:

Pseudcocode:

``````factorial_recursive(x):
if x <= 1 then
return 1
else
return x * factorial_recursive(x - 1)``````

That last line was a bit tricky to work out.

My thoughts:

'What's going on?' — I need to pass in `x-1` as an argument. And I need to use the result of the `factorial_recursive(x - 1)` in order to set the value of the result... argh this is confusing...

I 'undid' setting the value of the argument after I'd called the function recursively, just as I've used this before with other recursion projects in Hopscotch recently. (Added 1 to the argument, after having subtracted from it)

(I substituted a number for x e.g. 4 and manually went through it roughly before doing the code in Hopscotch, because I tend to get lazy with checking properly from just testing and trying random stuff, rather than checking it through)

Then I saw the return value would eventually hit 1, and then from there gradually be multiplied by all the previous numbers.

Here was what it came out to be in Hopscotch:

Then I got an idea to use this for triangle numbers (just change the operation in the last line to a + instead of *) and what happens if you change it into an exponent?? I'll leave that for another topic though

### Combinations:

Factorials can be used in probability and combinatorics (how many ways can I arrange five objects, how many ways can I pick 3 people from a group of 10)

They're used in the combinations formula too:

``n choose k = n! / k! * (n - k)!``

'n choose k' tells you how many ways you can choose `k` items from a group of `n` items.

More background information

I also implemented this in Hopscotch, following the functions idea from earlier.

However, I think it's inefficient and becomes rather noticeably lengthy in time.

Funnily, I was thinking that @BuildASnowman and @oio might be interested in this (even if they might not see this or be around right now) — and they were also tagged on the other topic But I know BAS was asking for arguments in Hopscotch too This was one way of doing it in a more structured format.

Sorry about some of the terms, I need to make my language more accessible XD

Recursion in Hopscotch and generally
Problem solving in general, algorithms, graph theory
custom factorial function
#2

Cool! This seems really complex, a tutorial is really helpful- Thanks!

#3

Thanks for your comment @LavenderArts that made me think about some of the background parts which I missed out on quite a bit of. So I'm going to grab some resources

#4

Cool!
I don't know what I could use this for?

#5

Yeah sometimes you don't know what you will use things for, and other times they come up when you least expect I was doing a test project for this one

#6

I worked out how to do factorial using recursion in Hopscotch so I've updated the topic now

#7

Here's another way to calculate factorials & combination factorials using clones to perform the calculation.

• Pro: it reaches a result in 2 frames (33.3ms @ 60fps) regardless of the valve of n or k
• Con: Only one call to the function is allowed at a time, meaning multiple objects can't simultaneously call the function.

First, create a series of clones. If you want the calculate n! or C(n,k) you'll need n clones. Each clone needs an index property that will be used to decide if it participates in the calculation & is the value that clone uses in the algorithm.

Example of creating the clones

For factorials, then add the event that calculates the result to the cloned object

Line 1: The global variable output is reset.
Line 2: Each participating clone multiplies the output by itselt. Remember that all clones are running the code, so for n=4 this results in 1 * 1 * 2 * 3 * 4
Line 3: The function is "turned off"

To call the function from any object, just set the value of n and call the function by setting calc=1

The result is then available in the global output variable

For combination factorials, you just need a few more events in the calculation clone object

This just breaks the C(n,k) calculation of n!/k!(n-k)! into 3 separate events

1. n!
2. /k!
3. /(n-k)!

The function is called in a similar fashion:

With the output used as desired

custom factorial function
#8

Woah that's awesome and really clear, thank you ThinBuffalo I actually got it just browsing right then – it's really clear.

Just for the cons at the top, I think it says:

Con: Only one call to the function is allowed at a time, meaning multiple objects can simultaneously call the function.

hmm I'm not sure but I think you might mean 'can't simultaneously call the function'?

– oh you fixed it, never mind – all good!

Topics with censored heading bug
#10

Haha very good, also difficult XD

Thanks for the credit