Hi guys can u help i want to make number list prime in python but with no for loop do or while only with recursive function
closed
Hi guys can u help i want to make number list prime in python but with no for loop do or while only with recursive function
closed
Extending on your naive and inefficient approach, the simplest way to replace the outer loop by recursion is something like:
def print_primes(limit):
if limit < 2: # base case
return
print_primes(limit-1) # recursive call: reduce problem size
# inner loop only for the last case
for b in range (2, int(limit**0.5)+1, 1):
if not limit % b:
break
else: # using for-else, slightly more to the point
print(limit)
>>> print_primes(20)
2
3
5
7
11
13
17
19