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