"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"for i in range(2,1000):\n",
" j=2\n",
" for j in range(2,i/j): #Nested for loop\n",
" if i%j == False: #Check for prime no.\n",
" break\n",
" if j>(i/j):\n",
" print i,\" is prime.\" #Result\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2 is prime.\n",
"3 is prime.\n",
"11 is prime.\n",
"13 is prime.\n",
"17 is prime.\n",
"19 is prime.\n",
"23 is prime.\n",
"29 is prime.\n",
"31 is prime.\n",
"37 is prime.\n",
"41 is prime.\n",
"43 is prime.\n",
"47 is prime.\n",
"53 is prime.\n",
"59 is prime.\n",
"61 is prime.\n",
"67 is prime.\n",
"71 is prime.\n",
"73 is prime.\n",
"79 is prime.\n",
"83 is prime.\n",
"89 is prime.\n",
"97 is prime.\n",
"101 is prime.\n",
"103 is prime.\n",
"107 is prime.\n",
"109 is prime.\n",
"113 is prime.\n",
"127 is prime.\n",
"131 is prime.\n",
"137 is prime.\n",
"139 is prime.\n",
"149 is prime.\n",
"151 is prime.\n",
"157 is prime.\n",
"163 is prime.\n",
"167 is prime.\n",
"173 is prime.\n",
"179 is prime.\n",
"181 is prime.\n",
"191 is prime.\n",
"193 is prime.\n",
"197 is prime.\n",
"199 is prime.\n",
"211 is prime.\n",
"223 is prime.\n",
"227 is prime.\n",
"229 is prime.\n",
"233 is prime.\n",
"239 is prime.\n",
"241 is prime.\n",
"251 is prime.\n",
"257 is prime.\n",
"263 is prime.\n",
"269 is prime.\n",
"271 is prime.\n",
"277 is prime.\n",
"281 is prime.\n",
"283 is prime.\n",
"293 is prime.\n",
"307 is prime.\n",
"311 is prime.\n",
"313 is prime.\n",
"317 is prime.\n",
"331 is prime.\n",
"337 is prime.\n",
"347 is prime.\n",
"349 is prime.\n",
"353 is prime.\n",
"359 is prime.\n",
"367 is prime.\n",
"373 is prime.\n",
"379 is prime.\n",
"383 is prime.\n",
"389 is prime.\n",
"397 is prime.\n",
"401 is prime.\n",
"409 is prime.\n",
"419 is prime.\n",
"421 is prime.\n",
"431 is prime.\n",
"433 is prime.\n",
"439 is prime.\n",
"443 is prime.\n",
"449 is prime.\n",
"457 is prime.\n",
"461 is prime.\n",
"463 is prime.\n",
"467 is prime.\n",
"479 is prime.\n",
"487 is prime.\n",
"491 is prime.\n",
"499 is prime.\n",
"503 is prime.\n",
"509 is prime.\n",
"521 is prime.\n",
"523 is prime.\n",
"541 is prime.\n",
"547 is prime.\n",
"557 is prime.\n",
"563 is prime.\n",
"569 is prime.\n",
"571 is prime.\n",
"577 is prime.\n",
"587 is prime.\n",
"593 is prime.\n",
"599 is prime.\n",
"601 is prime.\n",
"607 is prime.\n",
"613 is prime.\n",
"617 is prime.\n",
"619 is prime.\n",
"631 is prime.\n",
"641 is prime.\n",
"643 is prime.\n",
"647 is prime.\n",
"653 is prime.\n",
"659 is prime.\n",
"661 is prime.\n",
"673 is prime.\n",
"677 is prime.\n",
"683 is prime.\n",
"691 is prime.\n",
"701 is prime.\n",
"709 is prime.\n",
"719 is prime.\n",
"727 is prime.\n",
"733 is prime.\n",
"739 is prime.\n",
"743 is prime.\n",
"751 is prime.\n",
"757 is prime.\n",
"761 is prime.\n",
"769 is prime.\n",
"773 is prime.\n",
"787 is prime.\n",
"797 is prime.\n",
"809 is prime.\n",
"811 is prime.\n",
"821 is prime.\n",
"823 is prime.\n",
"827 is prime.\n",
"829 is prime.\n",
"839 is prime.\n",
"853 is prime.\n",
"857 is prime.\n",
"859 is prime.\n",
"863 is prime.\n",
"877"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" is prime.\n",
"881 is prime.\n",
"883 is prime.\n",
"887 is prime.\n",
"907 is prime.\n",
"911 is prime.\n",
"919 is prime.\n",
"929 is prime.\n",
"937 is prime.\n",
"941 is prime.\n",
"947 is prime.\n",
"953 is prime.\n",
"967 is prime.\n",
"971 is prime.\n",
"977 is prime.\n",
"983 is prime.\n",
"991 is prime.\n",
"997 is prime.\n"
]
}
],
"prompt_number": 30
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 4.19, Page Number: 78
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"import random\n",
"\n",
"#Variable declaration\n",
"magic = random.randint(0,100) #Number to be guessed\n",
"i=1\n",
"high=100\n",
"low=0\n",
"\n",
"#Function to play the magic number game\n",
"def play(m):\n",
" low=0\n",
" high=100\n",
" for t in range(100):\n",
" x = random.randint(low,high) #Number guessed by the user\n",
" if x==m:\n",
" print \"***Right***\"\n",
" return\n",
" else:\n",
" if(x=1 and option<=3:\n",
" break\n",
" if option==1:\n",
" magic=random.randint(0,100) #Number to be guessed\n",
" elif option==2:\n",
" play(magic) #Calls the function play\n",
" elif option==3:\n",
" print \"Goodbye\" \n",
" break\n",
" i+=1 #increments i such that the 3 options get selected sequentially\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1.Get a new magic number.\n",
"2.Play\n",
"3.Quit\n",
"1.Get a new magic number.\n",
"2.Play\n",
"3.Quit\n",
"Too low.\n",
"Too low.\n",
"Too low.\n",
"Too low.\n",
"Too high.\n",
"Too low.\n",
"Too high.\n",
"Too low.\n",
"Too high.\n",
"Too high.\n",
"Too high.\n",
"Too high.\n",
"Too low.\n",
"***Right***\n",
"1.Get a new magic number.\n",
"2.Play\n",
"3.Quit\n",
"Goodbye\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}