Created
September 12, 2021 10:16
-
-
Save hemantkchitale/a9443c2b48983432a480ccdac6d293c5 to your computer and use it in GitHub Desktop.
Collatz Conjecture (Jupyter Python Notebook)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "d722e635", | |
"metadata": {}, | |
"source": [ | |
"## Collatz Conjecture\n", | |
"\n", | |
"#### For an odd number, multiply by 3 and add 1\n", | |
"#### For an even number, divide by 2\n", | |
"\n", | |
"#### The conjecture is that whatever seed you start with, it ends with 4,2,1\n", | |
"\n", | |
"#### The curious thing is that the number of steps to read \"... 4,2,1\" wildly fluctuates\n", | |
"\n", | |
"#### This code computes the number of steps for a given \"max seed\"\n", | |
"#### and then computes the number of steps for all seed values from 1 to \"max seed\"\n", | |
"#### and plots a line chart" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "28247d99", | |
"metadata": {}, | |
"source": [ | |
"##Print all the values beginning with the seed" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "04d2fadd", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "db0705a7", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"n = int(input(\"what is the max seed ?\"))\n", | |
"seed=n\n", | |
"steps_counter=0\n", | |
"while n != 1:\n", | |
" if n%2 == 0:\n", | |
" n=int(n/2)\n", | |
" print(n)\n", | |
" steps_counter=steps_counter+1\n", | |
" else:\n", | |
" n=(n*3)+1\n", | |
" #printing the value is optional, you may comment out the print\n", | |
" print(n)\n", | |
" steps_counter=steps_counter+1\n", | |
"print(\"Totally \", steps_counter, \" steps for this max seed value\")\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "8c23c836", | |
"metadata": {}, | |
"source": [ | |
"##Loop from 1 to max seed and print number of steps" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "b0e363cb", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"seed_steps_df=pd.DataFrame(columns=('seed', 'steps'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f8e20707", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"i=1\n", | |
"while i <=seed:\n", | |
" n=i\n", | |
" steps_counter=0\n", | |
" while n != 1:\n", | |
" if n%2 == 0:\n", | |
" n=n/2\n", | |
" #print(n)\n", | |
" steps_counter=steps_counter+1\n", | |
" else:\n", | |
" n=(n*3)+1\n", | |
" #print(n)\n", | |
" steps_counter=steps_counter+1\n", | |
" ##optional : Print the number of steps at each incremental seed value\n", | |
" print(\"Totally \", steps_counter, \" steps\", \"for seed being \", i)\n", | |
" values_to_add = {'seed':i,'steps':steps_counter} \n", | |
" row_to_add = pd.Series(values_to_add)\n", | |
" seed_steps_df = seed_steps_df.append(row_to_add,ignore_index=True)\n", | |
" i=i+1\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d853d7a9", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [ | |
"seed_steps_df.plot(x=\"seed\",y=\"steps\",xlabel=\"Seed Value\",ylabel=\"Steps for Collatz Conjecture\",title=\"Collatz Conjecture\",figsize=(12,6))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "77dc4060", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment