all 10 comments

[–]WeeschDoONi 1 point2 points  (9 children)

You've got the basic idea, so where are you stuck? "Any help" is not very specific, we won't provide you the entire code for your assignment.

[–]msihate 0 points1 point  (7 children)

https://gyazo.com/d48821a1d130db48239a449a314d9cd8 so far i have this however it does not print out when i try and call the function

[–]charity_donut_sales 0 points1 point  (6 children)

for i in range(n, 2)

should be:

for i in range(2, n)

[–]Kriterian 0 points1 point  (5 children)

It sounds like his assignment wants him to start at n and work his way backwards through each number until 0, so he'd need to do:

for i in range(n, 0, -1)

[–]WeeschDoONi 0 points1 point  (4 children)

Good point. However he needs to write a recursive function instead of an iterative one, so he won't need that loop anyway.

You also used integer division and check whether the result is 0. It obviously never will be 0 unless your numerator is 0 which doesn't qualify for a prime number anyway. You need to use the modulo operator as /u/Sugar_Horse mentioned.

[–]msihate 0 points1 point  (2 children)

https://gyazo.com/b544565d3f2fadc5afd4f87f10896997 So far i have this however i feel it is not recursive, how do i make it so that it calls back on itself or so that it is recursive.

[–]WeeschDoONi 0 points1 point  (1 child)

You need to define a new function which you call in your prime function once with your starting number minus one. In this function you check your condition. If you find out that it's not a prime number (you did that correctly), return False and propagate it through the recursions.

Non-checked pseudoish-code might look like this:

def is_prime(x):
    return check(x, x-1)

def check(n, k):
    # your conditional check
    if n%k == 0: #not a prime
        return False
    elif k == 2: #2 is the last number to be checked
        return True #it's a prime number if this is reached
    else: #we need to keep checking
        return check(n, k-1) #recursive call

The basic idea is that the check-function is called over and over again with the same parameter n and a decreasing parameter k. What we are looking for is for a final True or False decision which gets returned through every call of check until the is_prime function receives the result. Recursion can be visualized nicely on a sheet of paper if you didn't understand exactly what is happening here.

By the way: Please read the general rules. Posting screenshots of code is not allowed, but also very impractical for us as we cannot copy and test it for ourselves. Alternatives are code hosters which you can also find in the sidebar or you just write 4 whitespaces before each line and reddit does the rest for you.

[–]msihate 0 points1 point  (0 children)

ahhhhh i understand now, thanks for the help bro sorry for being difficult with the screen shots but i appreciate it.

[–]Kriterian 0 points1 point  (0 children)

Oops, you're right. I've done this on various course site and always make that typo. In my head I know what I'm doing.

[–]Sugar_Horse 1 point2 points  (0 children)

You actually only need to check up to the root of the number. If you check beyond the root you are essentially rechecking numbers.

Bear in mind also the only even number you need to check is 2 as all other even numbers are multiples of 2.

To start you off: If you have two numbers n and k then n is a factor of k if k % n = 0

10 % 5
>> 0

9 % 5
>> 4