0

I have the following code:

def primes(n):
  acc = []
  return do_primes(range(2,n,1), acc)

def do_primes(xs, acc):
  if xs:
    head, tail = xs[0], xs[1:]
    acc.append(head)
    do_primes([x for x in xs if x % head != 0], acc)
  else:
    return acc

Invoking the code yields to None:

>>> primes(10)
None

1 Answer 1

4

You need to return once more:

def do_primes(xs, acc):
  if xs:
    head, tail = xs[0], xs[1:]
    acc.append(head)
    return do_primes([x for x in xs if x % head != 0], acc)
  else:
    return acc
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.