Skip to content

Instantly share code, notes, and snippets.

Created November 19, 2017 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/06543311f83cfaa898d34b5ba4039319 to your computer and use it in GitHub Desktop.
Save anonymous/06543311f83cfaa898d34b5ba4039319 to your computer and use it in GitHub Desktop.
function for checking if a number is a prime number
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"99 is not a prime number\n",
"3 x 33 = 99\n",
"\n",
"37 is a prime number\n",
"\n",
"19827371 is not a prime number\n",
"397 x 49943 = 19827371\n",
"\n",
"192341234818 is not a prime number\n",
"2 x 96170617409 = 192341234818\n",
"\n",
"15485863 is a prime number\n"
]
}
],
"source": [
"def isPrime(number):\n",
" if number > 1:\n",
" is_prime = 1 #if is_prime ends up being equal to 1, the number is prime. \n",
" for n in range(2,number):\n",
" if((number % n) == 0):\n",
" is_prime -= 1 \n",
" print(number,\"is not a prime number\")\n",
" print(n,\"x\",number//n,\"=\",number)\n",
" break\n",
" if is_prime == 1:\n",
" print(number, \"is a prime number\")\n",
" \n",
"#Some examples\n",
"isPrime(99)\n",
"print(\"\")\n",
"isPrime(37)\n",
"print(\"\")\n",
"isPrime(19827371)\n",
"print(\"\")\n",
"isPrime(192341234818)\n",
"print(\"\")\n",
"isPrime(15485863)"
]
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment