# 5. factorial

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

```python

def factorial(x):
num = 1
while x >=1:
num = num * x
x -= 1
return num

``````<do not remove the three backticks above>

My question is more related to the process of learning programming. The code above is correct.

So, I spent about an hour or more trying to write a function to get the Factorial of a number. After great difficulty and numerous failures, I decided to look up what a skilled programmer would do to factor a number in python.  I did not copy and paste the code, I studied it for about 5 minutes until I understood the logic (logic I could not arrive at on my own). Then I walked away from the computer for about 10 minutes to make sure I didn't remember exactly what the code looked like.

Then I came back and typed in the code above AND understood.

So I guess my question would be:
Is this a good way to learn programming? Should I have spent some more time trying to come up with that answer (or a similar one) entirely on my own? Or over time will just making sure I understand the intuition behind it be enough to make me learn programming?``````

#2

I think itâ€™s important for you to understand logic, with practice youâ€™ll eventually be able to find logic on your own, Iâ€™m still in process of learning, but more I practice Iâ€™m more able to find some logic on my own, sometimes itâ€™s wrong but I think itâ€™s important that you can come up with something

#3

Yeah, Iâ€™m stuck on the next one too. Checking prime numbers. This one is difficult for me also.

#4

Well it depends, and it depends on many things, the kind of problem and the difficulties you have with it, the kind of learning personality you are and so on. Some learn best when they are faced with a problem hands on, some learn best when they get a tool set before even getting close to a problem. As long as your difficulty is not â€śWhat am I supposed to do hereâ€ť you should definitely try it yourself first. If this is the difficulty, take a deep breath and maybe a break, read the instructions carefully, google the related terms to get a feeling for it, break it down to smaller problems and then solve them. And maybe revisit your tool set. Knowing that you have a thorough understanding of all that is necessary to do it might already help a bit.

In case of factorial and prime numbers the problems are rather mathematical and the problem with maths is that it is a highly compressed and efficient language, meaning that the basic logic is mostly unbearably stupid but writing it efficient and stacking rules that seem to be stupid on there own lets you do stuff that comes close to magic. I mean think about it, we can formalize the rules of nature letting us predict events in the near future, that is literally some sort of wizardry. And it is derived from really basic and simple rules.

And in terms of prime numbers it the same. Prime numbers are just numbers that are only divisible by itself and by 1 (excluding 1 as both conditions need to apply). Thatâ€™s it, nothing more. Fun fact: Almost all modern cryptography is based on them (but you probably donâ€™t need to know this to program)

So to check for prime numbers is rather simple. You know by default that any number is divisible by 1 and itself so the only thing you need to make sure is that the only-part is fulfilled. Meaning you check any number from 2 to number-1
and if any of them divides the number without rest (does this ring a bell?) the number is not a prime, otherwise it is a prime.

PPS: As I just read that this is already said in the instruction. Maybe I just emphasize the two key points. You need to go through all the numbers between 2 and number-1. So what structure does this normally.
And the other thing is that you want to check if one number divides another number evenly or in other words you want to see if the remainder of a division is 0. What operator letâ€™s you do this?

If you dig a bit deeper into maths you could verify that you in fact need much less then all numbers from 2 to number-1, which increases the speed a lot, but keep in mind the primary goal is to have something that works, when this is done you can improve it.

For the factorial part I actually donâ€™t like the instructions. The definition about factorials is actually.

``````0! is 1
and for any integer n bigger than 0:
n! or factorial(n) is the same as n*factorial(n-1)
``````

which makes a lot more sense when using the hint. Because this can be almost literally translated into code using a recursion. Youâ€™ve seen recursion yet, do you? if not that is what people mean when they wrongly reference inception
Meaning you call a function inside of a function opening another instance of this function. Factorial is actually the generic example too teach how recursions work so youâ€™ll find countless examples how this works if you google for it. Or just ask, to understand them is not difficult but might be a little tedious.

Hope this helps a bit.

PS: As said for the exercises Iâ€™d more or less recommend to try to do them on your own as long as they arenâ€™t too frustrating, most of them are not intended to be. But for real projects the problem is probably that there are no â€śsolutionsâ€ť as you might be one of the first to do it or better you need to find out what youâ€™re actually searching for and there of course you donâ€™t have to reinvent the wheel if you find a good solution use it, you can find problems anywhere solutions are more difficult to find

#5
``````def is_prime(x):
for num in range(2,x-1):
if x

###print statement for testing
print is_prime(9)

``````

Basically everything I am trying past â€śif xâ€¦â€ť just isnâ€™t working. I understand how to determine whether or not a number is prime, but I just canâ€™t translate it into a computer language.

This problem seems like it should be easier for me though.

Edit: In response to your comment, I am working with the modulus operator (%) and I think logically I may be on the right track, but my logic never works with ALL numbers.

#6

Yes modulo should be the right track could you show the updated code? Common misconception among all beginners is to question their logic just because of small typos in the syntax

#7

I think I might just need a break for now. Iâ€™ll keep in mind that recursion is a great (possibly necessary) tool for the problem and will look at it with fresh eyes later.

My code though that obviously wasnt working was:

``````def is_prime(x):
for num in range(2,x-1):
if x % num == 0:
return False
else:
return True
``````

it was obviously not anywhere close though. I had sort of just literally translated the instructions.

edit: my problem is almost NEVER syntax, is always semantics or logic
edit2: I really do need a break though, my mind is frozen right now. Thank you for the help. I will review all of this when I return later.

#8

Well then take a break and read this later, no need to get overly frustrated.

Wellâ€¦ Actually recursion weâ€™re meant as a hint for the last exercise about factorials â€¦ and to be honest they are in fact an awful and unnecessary tool that should be avoided whenever it is possible, when you want to write fast and efficient code. That is why the version with the while is actually better. The point is just that they may provide a more intuitive approach to the problem as you can almost literally translate the definition of factorials to a recursion but you need some more though power to convert it to a while loop and that was an easy example have you heard of the â€śtowers of hanoiâ€ť problem? The recursive version is actually straight forward once youâ€™ve understood it but the iterative one (loop) is more difficult.

And about the prime code, youâ€™re actually pretty close the problem is that this will leave the function in the first loop.

#9
``````def is_prime(x):
if x < 2:
return False
elif x == 2:
return True
else:
for num in range(2,x):
if x % num == 0:
return False
return True
``````

This looks correct, will it work with all prime numbers? Looks like it to me.

So once a function returns as either True or False, it breaks the loop, unless you are assigning a variable such as `n = True` inside the loop or function?

If there are any other teaching opportunities here I would greatly appreciate it before I move on to the next problem set.

Also, thank you so much for your detailed explanation.

#10

I actually donâ€™t understand what you mean here.
But the code is probably what the author had in mind writing this exercise, nothing to add here.