{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Week 5 Bonus task: Prime Numbers\n",
"[Prime numbers](https://en.wikipedia.org/wiki/Prime_number) are natural numbers greater than 1 which are not divisible by any number beside 1 and the number itself. In other words, the number cannot be composed as a product of two natural numbers other than 1 and the number itself. There are infinite prime numbers and the first ones are: \n",
"\n",
" 2, 3, 5, 7, 11, ...\n",
"\n",
"## Your Task\n",
"Write a program, that gets an integer through input and creates a list containing all prime numbers until this input. To do so, two functions have to be implemented:\n",
"\n",
"- The function `isprime()` gets an integer as input and returns `True` if this integer is prime, and `False` if the integer is not prime. \n",
"- The function `prime_list()` gets an integer as input and checks each number from 2 to input, if it is prime by calling the above function. If a number is prime, it is appended to a list. This list is given back as the return value of `prime_list()`.\n",
"\n",
"The program finally outputs the list of all prime numbers.\n",
"\n",
"## Example\n",
"Example 1:\n",
"\n",
" Up to which number do you want all prime numbers: 100\n",
" [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n",
"\n",
"Example 2:\n",
"\n",
" Up to which number do you want all prime numbers: 13\n",
" [2, 3, 5, 7, 11, 13]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def isprime(cand):\n",
" for i in range(2, cand):\n",
" if cand % i == 0:\n",
" return False\n",
" return True\n",
"\n",
"def prime_list(num):\n",
" list_primes = []\n",
" for i in range (2, num + 1):\n",
" if isprime(i):\n",
" list_primes.append(i)\n",
" return list_primes\n",
"\n",
"# Main Program\n",
"num = int(input(\"Up to which number do you want all prime numbers: \"))\n",
"print(prime_list(num))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}