5. factorial


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

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?


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


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


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 :slight_smile: (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 :slight_smile:
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 :slight_smile:


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.


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 :slight_smile:


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
            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 :frowning:
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.


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.


def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
        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.


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.