Show More
@@ -967,7 +967,7 b'' | |||||
967 | "metadata": {}, |
|
967 | "metadata": {}, | |
968 | "output_type": "display_data", |
|
968 | "output_type": "display_data", | |
969 | "text": [ |
|
969 | "text": [ | |
970 |
"<IPython.core.display.Javascript at 0x2 |
|
970 | "<IPython.core.display.Javascript at 0x278b9d0>" | |
971 | ] |
|
971 | ] | |
972 | } |
|
972 | } | |
973 | ], |
|
973 | ], | |
@@ -1071,6 +1071,128 b'' | |||||
1071 | "level": 2, |
|
1071 | "level": 2, | |
1072 | "metadata": {}, |
|
1072 | "metadata": {}, | |
1073 | "source": [ |
|
1073 | "source": [ | |
|
1074 | "Prime Factor Finder" | |||
|
1075 | ] | |||
|
1076 | }, | |||
|
1077 | { | |||
|
1078 | "cell_type": "markdown", | |||
|
1079 | "metadata": {}, | |||
|
1080 | "source": [ | |||
|
1081 | "Find the prime numbers inside a large integer" | |||
|
1082 | ] | |||
|
1083 | }, | |||
|
1084 | { | |||
|
1085 | "cell_type": "code", | |||
|
1086 | "collapsed": false, | |||
|
1087 | "input": [ | |||
|
1088 | "def is_int(number):\n", | |||
|
1089 | " return int(number) == number\n", | |||
|
1090 | "\n", | |||
|
1091 | "def factor_int(number):\n", | |||
|
1092 | " return [i + 1 for i in range(number) if is_int(number / (float(i) + 1.0))] " | |||
|
1093 | ], | |||
|
1094 | "language": "python", | |||
|
1095 | "metadata": {}, | |||
|
1096 | "outputs": [], | |||
|
1097 | "prompt_number": 13 | |||
|
1098 | }, | |||
|
1099 | { | |||
|
1100 | "cell_type": "code", | |||
|
1101 | "collapsed": false, | |||
|
1102 | "input": [ | |||
|
1103 | "import time\n", | |||
|
1104 | "BACKGROUND = '#F7EBD5'\n", | |||
|
1105 | "PARENT_COLOR = '#66635D'\n", | |||
|
1106 | "FACTOR_COLOR = '#6CC5C1'\n", | |||
|
1107 | "EDGE_COLOR = '#000000'\n", | |||
|
1108 | "PRIME_COLOR = '#E54140'\n", | |||
|
1109 | "\n", | |||
|
1110 | "existing_graphs = []\n", | |||
|
1111 | "\n", | |||
|
1112 | "def add_unique_node(graph, value, **kwargs):\n", | |||
|
1113 | " index = len(graph.node)\n", | |||
|
1114 | " graph.add_node(index, charge=-50, strokewidth=0, value=value, **kwargs)\n", | |||
|
1115 | " return index\n", | |||
|
1116 | "\n", | |||
|
1117 | "def plot_primes(graph, number, parent, start_number, delay=0.0):\n", | |||
|
1118 | " if delay > 0.0:\n", | |||
|
1119 | " time.sleep(delay)\n", | |||
|
1120 | " \n", | |||
|
1121 | " factors = factor_int(number)\n", | |||
|
1122 | " if len(factors) > 2:\n", | |||
|
1123 | " for factor in factors:\n", | |||
|
1124 | " if factor != number:\n", | |||
|
1125 | " factor_size = max(float(factor) / start_number * 30.0,3.0)\n", | |||
|
1126 | " parent_factor_size = max(float(number) / start_number * 30.0,3.0)\n", | |||
|
1127 | " index = add_unique_node(graph, number, fill=FACTOR_COLOR, r='%.2fpx' % factor_size)\n", | |||
|
1128 | " graph.add_edge(index, parent, distance=parent_factor_size+factor_size, stroke=EDGE_COLOR)\n", | |||
|
1129 | " plot_primes(graph, factor, parent=index, start_number=start_number, delay=delay)\n", | |||
|
1130 | " else:\n", | |||
|
1131 | " # Prime, set parent color to prime color.\n", | |||
|
1132 | " graph.node[parent]['fill'] = PRIME_COLOR\n", | |||
|
1133 | "\n", | |||
|
1134 | "def graph_primes(number, delay):\n", | |||
|
1135 | " if len(existing_graphs) > 0:\n", | |||
|
1136 | " for graph in existing_graphs:\n", | |||
|
1137 | " graph.close()\n", | |||
|
1138 | " del existing_graphs[:]\n", | |||
|
1139 | " \n", | |||
|
1140 | " floating_container = widgets.ContainerWidget(default_view_name='ModalView')\n", | |||
|
1141 | " floating_container.description = \"Factors of %d\" % number\n", | |||
|
1142 | " floating_container.button_text = str(number)\n", | |||
|
1143 | " floating_container.set_css({\n", | |||
|
1144 | " 'width': '620px',\n", | |||
|
1145 | " 'height': '450px'}, selector='modal')\n", | |||
|
1146 | " \n", | |||
|
1147 | " graph = EventfulGraph()\n", | |||
|
1148 | " d3 = ForceDirectedGraphWidget(graph, parent=floating_container)\n", | |||
|
1149 | " floating_container.set_css('background', BACKGROUND)\n", | |||
|
1150 | " d3.width = 600\n", | |||
|
1151 | " d3.height = 400\n", | |||
|
1152 | " display(floating_container)\n", | |||
|
1153 | " existing_graphs.append(floating_container)\n", | |||
|
1154 | " \n", | |||
|
1155 | " index = add_unique_node(graph, number, fill=PARENT_COLOR, r='30px')\n", | |||
|
1156 | " plot_primes(graph, number=number, parent=index, start_number=number, delay=delay)" | |||
|
1157 | ], | |||
|
1158 | "language": "python", | |||
|
1159 | "metadata": {}, | |||
|
1160 | "outputs": [], | |||
|
1161 | "prompt_number": 14 | |||
|
1162 | }, | |||
|
1163 | { | |||
|
1164 | "cell_type": "code", | |||
|
1165 | "collapsed": false, | |||
|
1166 | "input": [ | |||
|
1167 | "box = widgets.ContainerWidget()\n", | |||
|
1168 | "box.vbox()\n", | |||
|
1169 | "box.align_center()\n", | |||
|
1170 | "box.pack_center()\n", | |||
|
1171 | "header = widgets.StringWidget(parent=box, default_view_name=\"LabelView\", value=\"<h1>Number Factorizer</h1><br>\")\n", | |||
|
1172 | "subbox = widgets.ContainerWidget(parent=box)\n", | |||
|
1173 | "subbox.hbox()\n", | |||
|
1174 | "subbox.align_center()\n", | |||
|
1175 | "subbox.pack_center()\n", | |||
|
1176 | "number = widgets.IntWidget(value=100, parent=subbox)\n", | |||
|
1177 | "button = widgets.ButtonWidget(description=\"Calculate\", parent=subbox)\n", | |||
|
1178 | "speed = widgets.FloatRangeWidget(parent=box, min=0.0, max=0.5, value=0.4, step=0.01)\n", | |||
|
1179 | "display(box)\n", | |||
|
1180 | "box.add_class('well well-small')\n", | |||
|
1181 | "\n", | |||
|
1182 | "def handle_caclulate():\n", | |||
|
1183 | " graph_primes(number.value, 0.5-speed.value)\n", | |||
|
1184 | "button.on_click(handle_caclulate)\n" | |||
|
1185 | ], | |||
|
1186 | "language": "python", | |||
|
1187 | "metadata": {}, | |||
|
1188 | "outputs": [], | |||
|
1189 | "prompt_number": 15 | |||
|
1190 | }, | |||
|
1191 | { | |||
|
1192 | "cell_type": "heading", | |||
|
1193 | "level": 2, | |||
|
1194 | "metadata": {}, | |||
|
1195 | "source": [ | |||
1074 | "Twitter Tweet Watcher" |
|
1196 | "Twitter Tweet Watcher" | |
1075 | ] |
|
1197 | ] | |
1076 | }, |
|
1198 | }, | |
@@ -1094,7 +1216,7 b'' | |||||
1094 | "language": "python", |
|
1216 | "language": "python", | |
1095 | "metadata": {}, |
|
1217 | "metadata": {}, | |
1096 | "outputs": [], |
|
1218 | "outputs": [], | |
1097 |
"prompt_number": 1 |
|
1219 | "prompt_number": 16 | |
1098 | }, |
|
1220 | }, | |
1099 | { |
|
1221 | { | |
1100 | "cell_type": "code", |
|
1222 | "cell_type": "code", | |
@@ -1113,7 +1235,7 b'' | |||||
1113 | "language": "python", |
|
1235 | "language": "python", | |
1114 | "metadata": {}, |
|
1236 | "metadata": {}, | |
1115 | "outputs": [], |
|
1237 | "outputs": [], | |
1116 |
"prompt_number": 1 |
|
1238 | "prompt_number": 17 | |
1117 | }, |
|
1239 | }, | |
1118 | { |
|
1240 | { | |
1119 | "cell_type": "code", |
|
1241 | "cell_type": "code", | |
@@ -1208,7 +1330,7 b'' | |||||
1208 | "language": "python", |
|
1330 | "language": "python", | |
1209 | "metadata": {}, |
|
1331 | "metadata": {}, | |
1210 | "outputs": [], |
|
1332 | "outputs": [], | |
1211 |
"prompt_number": 1 |
|
1333 | "prompt_number": 18 | |
1212 | }, |
|
1334 | }, | |
1213 | { |
|
1335 | { | |
1214 | "cell_type": "code", |
|
1336 | "cell_type": "code", | |
@@ -1227,7 +1349,7 b'' | |||||
1227 | ] |
|
1349 | ] | |
1228 | } |
|
1350 | } | |
1229 | ], |
|
1351 | ], | |
1230 |
"prompt_number": 1 |
|
1352 | "prompt_number": 19 | |
1231 | } |
|
1353 | } | |
1232 | ], |
|
1354 | ], | |
1233 | "metadata": {} |
|
1355 | "metadata": {} |
General Comments 0
You need to be logged in to leave comments.
Login now