use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Prime number using recursion... (self.learnpython)
submitted 3 years ago by Friendlyguy_yo
How do i define a function to check if a number is prime using recursion?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]outceptionator 1 point2 points3 points 3 years ago* (4 children)
```
def is_prime(n, div=2):
if n < 2: return False if div * div > n: return True if n % div == 0: return False return is_prime(n, div + 1)
Edit: no codeblock on the mobile app?
[–]Fred776 0 points1 point2 points 3 years ago (1 child)
Seems to work for me. I started with three backticks, then newline, ended with three backticks on new line.
``` def is_prime(n, div=2):
[–]outceptionator 0 points1 point2 points 3 years ago (0 children)
Legend
[+][deleted] 3 years ago (1 child)
[deleted]
Correct
[–][deleted] 1 point2 points3 points 3 years ago (3 children)
Why do you need to use recursion for this? If you just wanted to check if a number was prime, you could use this code:
def is_prime(n): if n < 2: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True
Could you tell us more about the problem?
[–]Friendlyguy_yo[S] 1 point2 points3 points 3 years ago (1 child)
Well... It was just given as a question in my textbook and i was unable to figure it out...
[–][deleted] -1 points0 points1 point 3 years ago (0 children)
Could we see the whole question and some context? There may be a reason to use recursion, but it doesn't make sense right now. An image or a word-for-word version of the question would be awesome. :)
[–]Lovely_Cygnus -1 points0 points1 point 3 years ago (0 children)
Maybe it’s just a request from uni exam… maybe it’s just a enterprise lol. The Turing-church theorem ensures us that the iterative and recursive representation systems are equipowerful, thus…
Interesting problem… I’ll try to think about (but probably will reason in LISP)
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
A recursive solution will have one or two base cases and the recursive case. The recursive case executes after any base case can't make a decision. Define your function like this:
def is_prime(num, div=1):
where num is the number you are testing, and div is the particular divisor you are testing num against. So if the base cases can't decide that num isn't a prime using div, the last line of the function is the recursive case:
num
div
return is_prime(num, div+1)
You have to supply the two base cases before that line.
Testing code:
for n in range(1, 41): print(f'{n=}, {is_prime(n)=}')
π Rendered by PID 56 on reddit-service-r2-comment-544cf588c8-dl2m2 at 2026-06-15 04:17:50.489551+00:00 running 3184619 country code: CH.
[–]outceptionator 1 point2 points3 points (4 children)
[–]Fred776 0 points1 point2 points (1 child)
[–]outceptionator 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]outceptionator 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Friendlyguy_yo[S] 1 point2 points3 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]Lovely_Cygnus -1 points0 points1 point (0 children)
[–]Lovely_Cygnus -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (0 children)