Start free

Intermediate · Functions · 111 of 264

Primes up to n

Print all prime numbers up to and including n, on one line.

Input: a whole number n (n >= 2). Output: the primes, separated by spaces.

Examples

Input
20
Output
2 3 5 7 11 13 17 19
Input
2
Output
2
Input
50
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

One more test checks other inputs when you press Check.

Stuck? The idea behind functions

A function, made with def, gives a name to a piece of code, so it can be used again: its parameters are the values it is given, return hands an answer back.

A function that returns lets the caller decide what to do with the answer: print it, compare it, use it in a sum.

def is_even(n):
    return n % 2 == 0

print(is_even(4))
print(is_even(7))

Watch for: print inside a function is not return: the caller gets None. And the code after return in the same block never runs.

All the functions exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More functions exercises