2018-02-06 19:23:59 +01:00
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" / >
< title > EulerProblem021< / title >
< script src = "https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js" > < / script >
< script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js" > < / script >
< style type = "text/css" >
/*!
*
* Twitter Bootstrap
*
*//*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:bold}dfn{font-style:italic}h1{font-size:2em;margin:.67em 0}mark{background:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-0.5em}sub{bottom:-0.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}button,input,optgroup,select,textarea{color:inherit;font:inherit;margin:0}button{overflow:visible}button,select{text-transform:none}button,html input[type="button"],input[type="reset"],input[type="submit"]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}input{line-height:normal}input[type="checkbox"],input[type="radio"]{box-sizing:border-box;padding:0}input[type="number"]::-webkit-inner-spin-button,input[type="number"]::-webkit-outer-spin-button{height:auto}input[type="search"]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}input[type="search"]::-webkit-search-cancel-button,input[type="search"]::-webkit-search-decoration{-webkit-appearance:none}fieldset{border:1px solid #c0c0c0;margin:0 2px;padding:.35em .625em .75em}legend{border:0;padding:0}textarea{overflow:auto}optgroup{font-weight:bold}table{border-collapse:collapse;border-spacing:0}td,th{padding:0}/*! Source: https://github.com/h5bp/html5-boilerplate/blob/master/src/css/main.css */@media print{*,*:before,*:after{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a,a:visited{text-decoration:underline}a[href]:after{content:" (" attr(href) ")"}abbr[title]:after{content:" (" attr(title) ")"}a[href^="#"]:after,a[href^="javascript:"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}p,h2,h3{orphans:3;widows:3}h2,h3{page-break-after:avoid}select{background:#fff !important}.navbar{display:none}.btn>.caret,.dropup>.btn>.caret{border-top-color:#000 !important}.label{border:1px solid #000}.table{border-collapse:collapse !important}.table td,.table th{background-color:#fff !important}.table-bordered th,.table-bordered td{border:1px solid #ddd !important}}@font-face{font-family:'Glyphicons Halflings';src:url('../components/bootstrap/fonts/glyphicons-halflings-regular.eot');src:url('../components/bootstrap/fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),url('../components/bootstrap/fonts/glyphicons-halflings-regular.woff') format('woff'),url('../components/bootstrap/fonts/glyphicons-halflings-regular.ttf') format('truetype'),url('../components/bootstrap/fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg')}.glyphicon{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:normal;line-height:1;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.glyphicon-asterisk:before{content:"\2a"}.glyphicon-plus:before{content:"\2b"}.glyphicon-euro:before,.glyphicon-eur:before{content:"\20ac"}.glyphicon-minus:before{content:"\2212"}.glyphicon-cloud:before{content:"\2601"}.glyphicon-envelope:before{content:"\2709"}.glyphicon-pencil:before{content:"\270f"}.glyphicon-glass:before{content:"\e001"}.glyphicon-music:before{content:"\e002"}.glyphicon-search:before{content:
*
* Font Awesome
*
*//*!
* Font Awesome 4.3.0 by @davegandy - http://fontawesome.io - @fontawesome
* License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License)
*/@font-face{font-family:'FontAwesome';src:url('../components/font-awesome/fonts/fontawesome-webfont.eot?v=4.3.0');src:url('../components/font-awesome/fonts/fontawesome-webfont.eot?#iefix& v=4.3.0') format('embedded-opentype'),url('../components/font-awesome/fonts/fontawesome-webfont.woff2?v=4.3.0') format('woff2'),url('../components/font-awesome/fonts/fontawesome-webfont.woff?v=4.3.0') format('woff'),url('../components/font-awesome/fonts/fontawesome-webfont.ttf?v=4.3.0') format('truetype'),url('../components/font-awesome/fonts/fontawesome-webfont.svg?v=4.3.0#fontawesomeregular') format('svg');font-weight:normal;font-style:normal}.fa{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0)}.fa-lg{font-size:1.33333333em;line-height:.75em;vertical-align:-15%}.fa-2x{font-size:2em}.fa-3x{font-size:3em}.fa-4x{font-size:4em}.fa-5x{font-size:5em}.fa-fw{width:1.28571429em;text-align:center}.fa-ul{padding-left:0;margin-left:2.14285714em;list-style-type:none}.fa-ul>li{position:relative}.fa-li{position:absolute;left:-2.14285714em;width:2.14285714em;top:.14285714em;text-align:center}.fa-li.fa-lg{left:-1.85714286em}.fa-border{padding:.2em .25em .15em;border:solid .08em #eee;border-radius:.1em}.pull-right{float:right}.pull-left{float:left}.fa.pull-left{margin-right:.3em}.fa.pull-right{margin-left:.3em}.fa-spin{-webkit-animation:fa-spin 2s infinite linear;animation:fa-spin 2s infinite linear}.fa-pulse{-webkit-animation:fa-spin 1s infinite steps(8);animation:fa-spin 1s infinite steps(8)}@-webkit-keyframes fa-spin{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}100%{-webkit-transform:rotate(359deg);transform:rotate(359deg)}}@keyframes fa-spin{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}100%{-webkit-transform:rotate(359deg);transform:rotate(359deg)}}.fa-rotate-90{filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=1);-webkit-transform:rotate(90deg);-ms-transform:rotate(90deg);transform:rotate(90deg)}.fa-rotate-180{filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=2);-webkit-transform:rotate(180deg);-ms-transform:rotate(180deg);transform:rotate(180deg)}.fa-rotate-270{filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);-webkit-transform:rotate(270deg);-ms-transform:rotate(270deg);transform:rotate(270deg)}.fa-flip-horizontal{filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1);-webkit-transform:scale(-1, 1);-ms-transform:scale(-1, 1);transform:scale(-1, 1)}.fa-flip-vertical{filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=2, mirror=1);-webkit-transform:scale(1, -1);-ms-transform:scale(1, -1);transform:scale(1, -1)}:root .fa-rotate-90,:root .fa-rotate-180,:root .fa-rotate-270,:root .fa-flip-horizontal,:root .fa-flip-vertical{filter:none}.fa-stack{position:relative;display:inline-block;width:2em;height:2em;line-height:2em;vertical-align:middle}.fa-stack-1x,.fa-stack-2x{position:absolute;left:0;width:100%;text-align:center}.fa-stack-1x{line-height:inherit}.fa-stack-2x{font-size:2em}.fa-inverse{color:#fff}.fa-glass:before{content:"\f000"}.fa-music:before{content:"\f001"}.fa-search:before{content:"\f002"}.fa-envelope-o:before{content:"\f003"}.fa-heart:before{content:"\f004"}.fa-star:before{content:"\f005"}.fa-star-o:before{content:"\f006"}.fa-user:before{content:"\f007"}.fa-film:before{content:"\f008"}.fa-th-large:before{content:"\f009"}.fa-th:before{content:"\f00a"}.fa-th-list:before{content:"\f00b"}.fa-check:before{content:"\f00c"}.fa-remove:before,.fa-close:before,.fa-times:before{content:"\f00d"}.fa-search-plus:before{content:"\f00e"}.fa-search-minus:before{content:"\f010"}.fa-power-off:before{content:"\f011"}.fa-signal:before{content:"\f012"}.fa-gear:before,.fa-cog:before{content:"\f013"}.fa-trash-o:before{content:"\f014"}.fa-home:before{content:"\f015"}.fa-file-o:before{content:"\f016"}.fa-clock-o:before{content:"\f017"}.fa-road:before{content:"\f018"}.fa-download:before{content:"\f019"}.fa-arrow-circle-o-down:befo
*
* IPython base
*
*/.modal.fade .modal-dialog{-webkit-transform:translate(0, 0);-ms-transform:translate(0, 0);-o-transform:translate(0, 0);transform:translate(0, 0)}code{color:#000}pre{font-size:inherit;line-height:inherit}label{font-weight:normal}.border-box-sizing{box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box}.corner-all{border-radius:2px}.no-padding{padding:0}.hbox{display:-webkit-box;-webkit-box-orient:horizontal;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:horizontal;-moz-box-align:stretch;display:box;box-orient:horizontal;box-align:stretch;display:flex;flex-direction:row;align-items:stretch}.hbox>*{-webkit-box-flex:0;-moz-box-flex:0;box-flex:0;flex:none}.vbox{display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;display:flex;flex-direction:column;align-items:stretch}.vbox>*{-webkit-box-flex:0;-moz-box-flex:0;box-flex:0;flex:none}.hbox.reverse,.vbox.reverse,.reverse{-webkit-box-direction:reverse;-moz-box-direction:reverse;box-direction:reverse;flex-direction:row-reverse}.hbox.box-flex0,.vbox.box-flex0,.box-flex0{-webkit-box-flex:0;-moz-box-flex:0;box-flex:0;flex:none;width:auto}.hbox.box-flex1,.vbox.box-flex1,.box-flex1{-webkit-box-flex:1;-moz-box-flex:1;box-flex:1;flex:1}.hbox.box-flex,.vbox.box-flex,.box-flex{-webkit-box-flex:1;-moz-box-flex:1;box-flex:1;flex:1}.hbox.box-flex2,.vbox.box-flex2,.box-flex2{-webkit-box-flex:2;-moz-box-flex:2;box-flex:2;flex:2}.box-group1{-webkit-box-flex-group:1;-moz-box-flex-group:1;box-flex-group:1}.box-group2{-webkit-box-flex-group:2;-moz-box-flex-group:2;box-flex-group:2}.hbox.start,.vbox.start,.start{-webkit-box-pack:start;-moz-box-pack:start;box-pack:start;justify-content:flex-start}.hbox.end,.vbox.end,.end{-webkit-box-pack:end;-moz-box-pack:end;box-pack:end;justify-content:flex-end}.hbox.center,.vbox.center,.center{-webkit-box-pack:center;-moz-box-pack:center;box-pack:center;justify-content:center}.hbox.baseline,.vbox.baseline,.baseline{-webkit-box-pack:baseline;-moz-box-pack:baseline;box-pack:baseline;justify-content:baseline}.hbox.stretch,.vbox.stretch,.stretch{-webkit-box-pack:stretch;-moz-box-pack:stretch;box-pack:stretch;justify-content:stretch}.hbox.align-start,.vbox.align-start,.align-start{-webkit-box-align:start;-moz-box-align:start;box-align:start;align-items:flex-start}.hbox.align-end,.vbox.align-end,.align-end{-webkit-box-align:end;-moz-box-align:end;box-align:end;align-items:flex-end}.hbox.align-center,.vbox.align-center,.align-center{-webkit-box-align:center;-moz-box-align:center;box-align:center;align-items:center}.hbox.align-baseline,.vbox.align-baseline,.align-baseline{-webkit-box-align:baseline;-moz-box-align:baseline;box-align:baseline;align-items:baseline}.hbox.align-stretch,.vbox.align-stretch,.align-stretch{-webkit-box-align:stretch;-moz-box-align:stretch;box-align:stretch;align-items:stretch}div.error{margin:2em;text-align:center}div.error>h1{font-size:500%;line-height:normal}div.error>p{font-size:200%;line-height:normal}div.traceback-wrapper{text-align:left;max-width:800px;margin:auto}body{background-color:#fff;position:absolute;left:0;right:0;top:0;bottom:0;overflow:visible}#header{display:none;background-color:#fff;position:relative;z-index:100}#header #header-container{padding-bottom:5px;padding-top:5px;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box}#header .header-bar{width:100%;height:1px;background:#e7e7e7;margin-bottom:-1px}@media print{#header{display:none !important}}#header-spacer{width:100%;visibility:hidden}@media print{#header-spacer{display:none}}#ipython_notebook{padding-left:0;padding-top:1px;padding-bottom:1px}@media (max-width:991px){#ipython_notebook{margin-left:10px}}#noscript{width:auto;padding-top:16px;padding-bottom:16px;text-align:center;font-size:22px;color:red;font-weight:bold}#ipython_notebook img{height:28px}#site{width:100%;display:none;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;overflow:auto}@med
*
* IPython auth
*
*/.center-nav{display:inline-block;margin-bottom:-4px}/*!
*
* IPython tree view
*
*/.alternate_upload{background-color:none;display:inline}.alternate_upload.form{padding:0;margin:0}.alternate_upload input.fileinput{display:inline;opacity:0;z-index:2;width:12ex;margin-right:-12ex}.alternate_upload .btn-upload{height:22px}ul#tabs{margin-bottom:4px}ul#tabs a{padding-top:6px;padding-bottom:4px}ul.breadcrumb a:focus,ul.breadcrumb a:hover{text-decoration:none}ul.breadcrumb i.icon-home{font-size:16px;margin-right:4px}ul.breadcrumb span{color:#5e5e5e}.list_toolbar{padding:4px 0 4px 0;vertical-align:middle}.list_toolbar .tree-buttons{padding-top:1px}.dynamic-buttons{padding-top:3px;display:inline-block}.list_toolbar [class*="span"]{min-height:24px}.list_header{font-weight:bold;background-color:#eee}.list_placeholder{font-weight:bold;padding-top:4px;padding-bottom:4px;padding-left:7px;padding-right:7px}.list_container{margin-top:4px;margin-bottom:20px;border:1px solid #ddd;border-radius:2px}.list_container>div{border-bottom:1px solid #ddd}.list_container>div:hover .list-item{background-color:red}.list_container>div:last-child{border:none}.list_item:hover .list_item{background-color:#ddd}.list_item a{text-decoration:none}.list_item:hover{background-color:#fafafa}.action_col{text-align:right}.list_header>div,.list_item>div{padding-top:4px;padding-bottom:4px;padding-left:7px;padding-right:7px;line-height:22px}.list_header>div input,.list_item>div input{margin-right:7px;margin-left:14px;vertical-align:baseline;line-height:22px;position:relative;top:-1px}.list_header>div .item_link,.list_item>div .item_link{margin-left:-1px;vertical-align:baseline;line-height:22px}.new-file input[type=checkbox]{visibility:hidden}.item_name{line-height:22px;height:24px}.item_icon{font-size:14px;color:#5e5e5e;margin-right:7px;margin-left:7px;line-height:22px;vertical-align:baseline}.item_buttons{line-height:1em;margin-left:-5px}.item_buttons .btn-group,.item_buttons .input-group{float:left}.item_buttons>.btn,.item_buttons>.btn-group,.item_buttons>.input-group{margin-left:5px}.item_buttons .btn{min-width:13ex}.item_buttons .running-indicator{padding-top:4px;color:#5cb85c}.toolbar_info{height:24px;line-height:24px}input.nbname_input,input.engine_num_input{padding-top:3px;padding-bottom:3px;height:22px;line-height:14px;margin:0}input.engine_num_input{width:60px}.highlight_text{color:blue}#project_name{display:inline-block;padding-left:7px;margin-left:-2px}#project_name>.breadcrumb{padding:0;margin-bottom:0;background-color:transparent;font-weight:bold}#tree-selector{padding-right:0}#button-select-all{min-width:50px}#select-all{margin-left:7px;margin-right:2px}.menu_icon{margin-right:2px}.tab-content .row{margin-left:0;margin-right:0}.folder_icon:before{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);content:"\f114"}.folder_icon:before.pull-left{margin-right:.3em}.folder_icon:before.pull-right{margin-left:.3em}.notebook_icon:before{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);content:"\f02d";position:relative;top:-1px}.notebook_icon:before.pull-left{margin-right:.3em}.notebook_icon:before.pull-right{margin-left:.3em}.running_notebook_icon:before{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);content:"\f02d";position:relative;top:-1px;color:#5cb85c}.running_notebook_icon:before.pull-left{margin-right:.3em}.running_notebook_icon:before.pull-right{margin-left:.3em}.file_icon:before{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);content:"\f016";position:relative;top:-2px}.file_icon:before.pull-left{margin-right:.3em}.file_icon:before.pull-right
*
* IPython text editor webapp
*
*/.selected-keymap i.fa{padding:0 5px}.selected-keymap i.fa:before{content:"\f00c"}#mode-menu{overflow:auto;max-height:20em}.edit_app #header{-webkit-box-shadow:0 0 12px 1px rgba(87,87,87,0.2);box-shadow:0 0 12px 1px rgba(87,87,87,0.2)}.edit_app #menubar .navbar{margin-bottom:-1px}.dirty-indicator{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);width:20px}.dirty-indicator.pull-left{margin-right:.3em}.dirty-indicator.pull-right{margin-left:.3em}.dirty-indicator-dirty{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);width:20px}.dirty-indicator-dirty.pull-left{margin-right:.3em}.dirty-indicator-dirty.pull-right{margin-left:.3em}.dirty-indicator-clean{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);width:20px}.dirty-indicator-clean.pull-left{margin-right:.3em}.dirty-indicator-clean.pull-right{margin-left:.3em}.dirty-indicator-clean:before{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;transform:translate(0, 0);content:"\f00c"}.dirty-indicator-clean:before.pull-left{margin-right:.3em}.dirty-indicator-clean:before.pull-right{margin-left:.3em}#filename{font-size:16pt;display:table;padding:0 5px}#current-mode{padding-left:5px;padding-right:5px}#texteditor-backdrop{padding-top:20px;padding-bottom:20px}@media not print{#texteditor-backdrop{background-color:#eee}}@media print{#texteditor-backdrop #texteditor-container .CodeMirror-gutter,#texteditor-backdrop #texteditor-container .CodeMirror-gutters{background-color:#fff}}@media not print{#texteditor-backdrop #texteditor-container .CodeMirror-gutter,#texteditor-backdrop #texteditor-container .CodeMirror-gutters{background-color:#fff}}@media not print{#texteditor-backdrop #texteditor-container{padding:0;background-color:#fff;-webkit-box-shadow:0 0 12px 1px rgba(87,87,87,0.2);box-shadow:0 0 12px 1px rgba(87,87,87,0.2)}}/*!
*
* IPython notebook
*
*/.ansibold{font-weight:bold}.ansiblack{color:black}.ansired{color:darkred}.ansigreen{color:darkgreen}.ansiyellow{color:#c4a000}.ansiblue{color:darkblue}.ansipurple{color:darkviolet}.ansicyan{color:steelblue}.ansigray{color:gray}.ansibgblack{background-color:black}.ansibgred{background-color:red}.ansibggreen{background-color:green}.ansibgyellow{background-color:yellow}.ansibgblue{background-color:blue}.ansibgpurple{background-color:magenta}.ansibgcyan{background-color:cyan}.ansibggray{background-color:gray}div.cell{border:1px solid transparent;display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;display:flex;flex-direction:column;align-items:stretch;border-radius:2px;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;border-width:thin;border-style:solid;width:100%;padding:5px;margin:0;outline:none}div.cell.selected{border-color:#ababab}@media print{div.cell.selected{border-color:transparent}}.edit_mode div.cell.selected{border-color:green}@media print{.edit_mode div.cell.selected{border-color:transparent}}.prompt{min-width:14ex;padding:.4em;margin:0;font-family:monospace;text-align:right;line-height:1.21429em}@media (max-width:540px){.prompt{text-align:left}}div.inner_cell{display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;display:flex;flex-direction:column;align-items:stretch;-webkit-box-flex:1;-moz-box-flex:1;box-flex:1;flex:1}@-moz-document url-prefix(){div.inner_cell{overflow-x:hidden}}div.input_area{border:1px solid #cfcfcf;border-radius:2px;background:#f7f7f7;line-height:1.21429em}div.prompt:empty{padding-top:0;padding-bottom:0}div.unrecognized_cell{padding:5px 5px 5px 0;display:-webkit-box;-webkit-box-orient:horizontal;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:horizontal;-moz-box-align:stretch;display:box;box-orient:horizontal;box-align:stretch;display:flex;flex-direction:row;align-items:stretch}div.unrecognized_cell .inner_cell{border-radius:2px;padding:5px;font-weight:bold;color:red;border:1px solid #cfcfcf;background:#eaeaea}div.unrecognized_cell .inner_cell a{color:inherit;text-decoration:none}div.unrecognized_cell .inner_cell a:hover{color:inherit;text-decoration:none}@media (max-width:540px){div.unrecognized_cell>div.prompt{display:none}}@media print{div.code_cell{page-break-inside:avoid}}div.input{page-break-inside:avoid;display:-webkit-box;-webkit-box-orient:horizontal;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:horizontal;-moz-box-align:stretch;display:box;box-orient:horizontal;box-align:stretch;display:flex;flex-direction:row;align-items:stretch}@media (max-width:540px){div.input{display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;display:flex;flex-direction:column;align-items:stretch}}div.input_prompt{color:navy;border-top:1px solid transparent}div.input_area>div.highlight{margin:.4em;border:none;padding:0;background-color:transparent}div.input_area>div.highlight>pre{margin:0;border:none;padding:0;background-color:transparent}.CodeMirror{line-height:1.21429em;font-size:14px;height:auto;background:none}.CodeMirror-scroll{overflow-y:hidden;overflow-x:auto}.CodeMirror-lines{padding:.4em}.CodeMirror-linenumber{padding:0 8px 0 4px}.CodeMirror-gutters{border-bottom-left-radius:2px;border-top-left-radius:2px}.CodeMirror pre{padding:0;border:0;border-radius:0}.highlight-base{color:#000}.highlight-variable{color:#000}.highlight-variable-2{color:#1a1a1a}.highlight-variable-3{color:#333}.highlight-string{color:#ba2121}.highlight-comment{color:#408080;font-style:italic}.highlight-number{color:#080}.highlight-atom{color:#88f}.highlight-keyword{color:#008000;font-weight:bold}.highlight-builtin{color:#008000}.highlight-error{color:#f00}.highlight-operator
*
* IPython notebook webapp
*
*/@media (max-width:767px){.notebook_app{padding-left:0;padding-right:0}}#ipython-main-app{box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;height:100%}div#notebook_panel{margin:0;padding:0;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;height:100%}#notebook{font-size:14px;line-height:20px;overflow-y:hidden;overflow-x:auto;width:100%;padding-top:20px;margin:0;outline:none;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;min-height:100%}@media not print{#notebook-container{padding:15px;background-color:#fff;min-height:0;-webkit-box-shadow:0 0 12px 1px rgba(87,87,87,0.2);box-shadow:0 0 12px 1px rgba(87,87,87,0.2)}}div.ui-widget-content{border:1px solid #ababab;outline:none}pre.dialog{background-color:#f7f7f7;border:1px solid #ddd;border-radius:2px;padding:.4em;padding-left:2em}p.dialog{padding:.2em}pre,code,kbd,samp{white-space:pre-wrap}#fonttest{font-family:monospace}p{margin-bottom:0}.end_space{min-height:100px;transition:height .2s ease}.notebook_app #header{-webkit-box-shadow:0 0 12px 1px rgba(87,87,87,0.2);box-shadow:0 0 12px 1px rgba(87,87,87,0.2)}@media not print{.notebook_app{background-color:#eee}}.celltoolbar{border:thin solid #cfcfcf;border-bottom:none;background:#eee;border-radius:2px 2px 0 0;width:100%;height:29px;padding-right:4px;display:-webkit-box;-webkit-box-orient:horizontal;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:horizontal;-moz-box-align:stretch;display:box;box-orient:horizontal;box-align:stretch;display:flex;flex-direction:row;align-items:stretch;-webkit-box-pack:end;-moz-box-pack:end;box-pack:end;justify-content:flex-end}@media print{.celltoolbar{display:none}}.ctb_hideshow{display:none;vertical-align:bottom}.ctb_global_show .ctb_show.ctb_hideshow{display:block}.ctb_global_show .ctb_show+.input_area,.ctb_global_show .ctb_show+div.text_cell_input,.ctb_global_show .ctb_show~div.text_cell_render{border-top-right-radius:0;border-top-left-radius:0}.ctb_global_show .ctb_show~div.text_cell_render{border:1px solid #cfcfcf}.celltoolbar{font-size:87%;padding-top:3px}.celltoolbar select{display:block;width:100%;height:32px;padding:6px 12px;font-size:13px;line-height:1.42857143;color:#555;background-color:#fff;background-image:none;border:1px solid #ccc;border-radius:2px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,0.075);box-shadow:inset 0 1px 1px rgba(0,0,0,0.075);-webkit-transition:border-color ease-in-out .15s, box-shadow ease-in-out .15s;-o-transition:border-color ease-in-out .15s, box-shadow ease-in-out .15s;transition:border-color ease-in-out .15s, box-shadow ease-in-out .15s;height:30px;padding:5px 10px;font-size:12px;line-height:1.5;border-radius:1px;width:inherit;font-size:inherit;height:22px;padding:0;display:inline-block}.celltoolbar select:focus{border-color:#66afe9;outline:0;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, 0.6);box-shadow:inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, 0.6)}.celltoolbar select::-moz-placeholder{color:#999;opacity:1}.celltoolbar select:-ms-input-placeholder{color:#999}.celltoolbar select::-webkit-input-placeholder{color:#999}.celltoolbar select[disabled],.celltoolbar select[readonly],fieldset[disabled] .celltoolbar select{cursor:not-allowed;background-color:#eee;opacity:1}textarea.celltoolbar select{height:auto}select.celltoolbar select{height:30px;line-height:30px}textarea.celltoolbar select,select[multiple].celltoolbar select{height:auto}.celltoolbar label{margin-left:5px;margin-right:5px}.completions{position:absolute;z-index:10;overflow:hidden;border:1px solid #ababab;border-radius:2px;-webkit-box-shadow:0 6px 10px -1px #adadad;box-shadow:0 6px 10px -1px #adadad}.completions select{background:white;outline:none;border:none;padding:0;margin:0;overflow:auto;font-family:monospace;font-size:110%;color:#000;width:auto}.completions select option.context{color:#286090}#kernel_logo_widget{float:right !important;float:right}#kernel_logo_widget .current_kernel_logo{display:none;margin-top:-1px;margin-bottom:-1px;wid
< / style >
< style type = "text/css" >
.highlight .hll { background-color: #ffffcc }
.highlight { background: #f8f8f8; }
.highlight .c { color: #408080; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .ch { color: #408080; font-style: italic } /* Comment.Hashbang */
.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #BC7A00 } /* Comment.Preproc */
.highlight .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */
.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0044DD } /* Generic.Traceback */
.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008000 } /* Keyword.Pseudo */
.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #B00040 } /* Keyword.Type */
.highlight .m { color: #666666 } /* Literal.Number */
.highlight .s { color: #BA2121 } /* Literal.String */
.highlight .na { color: #7D9029 } /* Name.Attribute */
.highlight .nb { color: #008000 } /* Name.Builtin */
.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */
.highlight .no { color: #880000 } /* Name.Constant */
.highlight .nd { color: #AA22FF } /* Name.Decorator */
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0000FF } /* Name.Function */
.highlight .nl { color: #A0A000 } /* Name.Label */
.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #19177C } /* Name.Variable */
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #666666 } /* Literal.Number.Bin */
.highlight .mf { color: #666666 } /* Literal.Number.Float */
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
.highlight .sa { color: #BA2121 } /* Literal.String.Affix */
.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
.highlight .sc { color: #BA2121 } /* Literal.String.Char */
.highlight .dl { color: #BA2121 } /* Literal.String.Delimiter */
.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #BA2121 } /* Literal.String.Double */
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.highlight .sx { color: #008000 } /* Literal.String.Other */
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
.highlight .s1 { color: #BA2121 } /* Literal.String.Single */
.highlight .ss { color: #19177C } /* Literal.String.Symbol */
.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0000FF } /* Name.Function.Magic */
.highlight .vc { color: #19177C } /* Name.Variable.Class */
.highlight .vg { color: #19177C } /* Name.Variable.Global */
.highlight .vi { color: #19177C } /* Name.Variable.Instance */
.highlight .vm { color: #19177C } /* Name.Variable.Magic */
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
< / style >
< style type = "text/css" >
/* Overrides of notebook CSS for static HTML export */
body {
overflow: visible;
padding: 8px;
}
div#notebook {
overflow: visible;
border-top: none;
}
@media print {
div.cell {
display: block;
page-break-inside: avoid;
}
div.output_wrapper {
display: block;
page-break-inside: avoid;
}
div.output {
display: block;
page-break-inside: avoid;
}
}
< / style >
<!-- Custom stylesheet, it must be in the same directory as the html file -->
< link rel = "stylesheet" href = "custom.css" >
<!-- Loading mathjax macro -->
<!-- Load mathjax -->
< script src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" > < / script >
<!-- MathJax configuration -->
< script type = "text/x-mathjax-config" >
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true,
processEnvironments: true
},
// Center justify equations in code and markdown cells. Elsewhere
// we use CSS to left justify single line equations in code cells.
displayAlign: 'center',
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}},
linebreaks: { automatic: true }
}
});
< / script >
<!-- End of mathjax configuration -->
< / head >
< body >
< div tabindex = "-1" id = "notebook" class = "border-box-sizing" >
< div class = "container" id = "notebook-container" >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< h1 id = "Euler-Problem-21" > Euler Problem 21< a class = "anchor-link" href = "#Euler-Problem-21" > ¶ < / a > < / h1 > < p > Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.< / p >
< p > For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.< / p >
< p > Evaluate the sum of all the amicable numbers under 10000.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > We reuse the sieve of eratosthenes (which we called get_primes_smaller previously). What annoys me about the implementation is that we do not stop scanning for prospects once the current prime squared is greater than the limit. Let's test if it returns the same result and compare the execution time.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [1]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > get_primes_smaller< / span > < span class = "p" > (< / span > < span class = "n" > limit< / span > < span class = "p" > ):< / span >
< span class = "n" > primes< / span > < span class = "o" > =< / span > < span class = "p" > []< / span >
< span class = "n" > prospects< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > n< / span > < span class = "k" > for< / span > < span class = "n" > n< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "n" > limit< / span > < span class = "p" > )]< / span >
< span class = "k" > while< / span > < span class = "n" > prospects< / span > < span class = "p" > :< / span >
< span class = "n" > p< / span > < span class = "o" > =< / span > < span class = "n" > prospects< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ]< / span >
< span class = "n" > prospects< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > x< / span > < span class = "k" > for< / span > < span class = "n" > x< / span > < span class = "ow" > in< / span > < span class = "n" > prospects< / span > < span class = "k" > if< / span > < span class = "n" > x< / span > < span class = "o" > %< / span > < span class = "n" > p< / span > < span class = "o" > !=< / span > < span class = "mi" > 0< / span > < span class = "p" > ]< / span >
< span class = "n" > primes< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > p< / span > < span class = "p" > )< / span >
< span class = "k" > return< / span > < span class = "n" > primes< / span >
< span class = "k" > def< / span > < span class = "nf" > sieve_of_eratosthenes< / span > < span class = "p" > (< / span > < span class = "n" > limit< / span > < span class = "p" > ):< / span >
< span class = "n" > primes< / span > < span class = "o" > =< / span > < span class = "p" > []< / span >
< span class = "n" > prospects< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > n< / span > < span class = "k" > for< / span > < span class = "n" > n< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "n" > limit< / span > < span class = "p" > )]< / span >
< span class = "k" > while< / span > < span class = "n" > prospects< / span > < span class = "p" > :< / span >
< span class = "n" > p< / span > < span class = "o" > =< / span > < span class = "n" > prospects< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ]< / span >
< span class = "n" > prospects< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > x< / span > < span class = "k" > for< / span > < span class = "n" > x< / span > < span class = "ow" > in< / span > < span class = "n" > prospects< / span > < span class = "k" > if< / span > < span class = "n" > x< / span > < span class = "o" > %< / span > < span class = "n" > p< / span > < span class = "o" > !=< / span > < span class = "mi" > 0< / span > < span class = "p" > ]< / span >
< span class = "n" > primes< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > p< / span > < span class = "p" > )< / span >
< span class = "k" > if< / span > < span class = "n" > p< / span > < span class = "o" > *< / span > < span class = "n" > p< / span > < span class = "o" > > < / span > < span class = "n" > limit< / span > < span class = "p" > :< / span >
< span class = "k" > break< / span >
< span class = "n" > primes< / span > < span class = "o" > +=< / span > < span class = "n" > prospects< / span >
< span class = "k" > return< / span > < span class = "n" > primes< / span >
< span class = "kn" > import< / span > < span class = "nn" > timeit< / span >
< span class = "kn" > import< / span > < span class = "nn" > functools< / span >
< span class = "nb" > print< / span > < span class = "p" > (< / span > < span class = "n" > timeit< / span > < span class = "o" > .< / span > < span class = "n" > timeit< / span > < span class = "p" > (< / span > < span class = "n" > functools< / span > < span class = "o" > .< / span > < span class = "n" > partial< / span > < span class = "p" > (< / span > < span class = "n" > get_primes_smaller< / span > < span class = "p" > ,< / span > < span class = "mi" > 10000< / span > < span class = "p" > ),< / span > < span class = "n" > number< / span > < span class = "o" > =< / span > < span class = "mi" > 100< / span > < span class = "p" > ))< / span >
< span class = "nb" > print< / span > < span class = "p" > (< / span > < span class = "n" > timeit< / span > < span class = "o" > .< / span > < span class = "n" > timeit< / span > < span class = "p" > (< / span > < span class = "n" > functools< / span > < span class = "o" > .< / span > < span class = "n" > partial< / span > < span class = "p" > (< / span > < span class = "n" > sieve_of_eratosthenes< / span > < span class = "p" > ,< / span > < span class = "mi" > 10000< / span > < span class = "p" > ),< / span > < span class = "n" > number< / span > < span class = "o" > =< / span > < span class = "mi" > 100< / span > < span class = "p" > ))< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > get_primes_smaller< / span > < span class = "p" > (< / span > < span class = "mi" > 10000< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "n" > sieve_of_eratosthenes< / span > < span class = "p" > (< / span > < span class = "mi" > 10000< / span > < span class = "p" > ))< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< div class = "output_wrapper" >
< div class = "output" >
< div class = "output_area" > < div class = "prompt" > < / div >
< div class = "output_subarea output_stream output_stdout output_text" >
< pre > 5.062845234464119
0.3188800166435408
< / pre >
< / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > Okay, this difference is sick actually. Interesting to see what the difference between O(sqrt(n)) and O(n) can mean. Now we can use the primes to do the factorization. I will again apply some optimizations and see how it turns out< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [2]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > get_prime_factors_old< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > ):< / span >
< span class = "n" > prime_generator< / span > < span class = "o" > =< / span > < span class = "n" > sieve_of_eratosthenes< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "o" > //< / span > < span class = "mi" > 2< / span > < span class = "p" > )< / span >
< span class = "n" > remainder< / span > < span class = "o" > =< / span > < span class = "n" > number< / span >
< span class = "n" > factors< / span > < span class = "o" > =< / span > < span class = "p" > []< / span >
< span class = "k" > for< / span > < span class = "n" > p< / span > < span class = "ow" > in< / span > < span class = "n" > prime_generator< / span > < span class = "p" > :< / span >
< span class = "k" > while< / span > < span class = "n" > remainder< / span > < span class = "o" > %< / span > < span class = "n" > p< / span > < span class = "o" > ==< / span > < span class = "mi" > 0< / span > < span class = "p" > :< / span >
< span class = "n" > remainder< / span > < span class = "o" > //=< / span > < span class = "n" > p< / span >
< span class = "n" > factors< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > p< / span > < span class = "p" > )< / span >
< span class = "k" > if< / span > < span class = "n" > remainder< / span > < span class = "o" > ==< / span > < span class = "mi" > 1< / span > < span class = "ow" > or< / span > < span class = "n" > p< / span > < span class = "o" > *< / span > < span class = "n" > p< / span > < span class = "o" > > < / span > < span class = "n" > number< / span > < span class = "p" > :< / span >
< span class = "k" > break< / span >
< span class = "k" > if< / span > < span class = "n" > remainder< / span > < span class = "o" > !=< / span > < span class = "mi" > 1< / span > < span class = "p" > :< / span >
< span class = "n" > factors< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > remainder< / span > < span class = "p" > )< / span >
< span class = "k" > return< / span > < span class = "n" > factors< / span >
< span class = "k" > def< / span > < span class = "nf" > get_prime_factors< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > ):< / span >
< span class = "n" > remainder< / span > < span class = "o" > =< / span > < span class = "n" > number< / span >
< span class = "n" > factors< / span > < span class = "o" > =< / span > < span class = "p" > []< / span >
< span class = "k" > for< / span > < span class = "n" > p< / span > < span class = "ow" > in< / span > < span class = "n" > sieve_of_eratosthenes< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "o" > //< / span > < span class = "mi" > 2< / span > < span class = "p" > ):< / span >
< span class = "k" > while< / span > < span class = "n" > remainder< / span > < span class = "o" > %< / span > < span class = "n" > p< / span > < span class = "o" > ==< / span > < span class = "mi" > 0< / span > < span class = "p" > :< / span >
< span class = "n" > remainder< / span > < span class = "o" > //=< / span > < span class = "n" > p< / span >
< span class = "n" > factors< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > p< / span > < span class = "p" > )< / span >
< span class = "k" > if< / span > < span class = "n" > remainder< / span > < span class = "o" > ==< / span > < span class = "mi" > 1< / span > < span class = "p" > :< / span >
< span class = "k" > break< / span >
< span class = "k" > if< / span > < span class = "n" > p< / span > < span class = "o" > > < / span > < span class = "n" > remainder< / span > < span class = "ow" > or< / span > < span class = "n" > p< / span > < span class = "o" > *< / span > < span class = "n" > p< / span > < span class = "o" > > < / span > < span class = "n" > number< / span > < span class = "p" > :< / span >
< span class = "n" > factors< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > remainder< / span > < span class = "p" > )< / span >
< span class = "k" > break< / span >
< span class = "k" > return< / span > < span class = "n" > factors< / span >
< span class = "kn" > import< / span > < span class = "nn" > timeit< / span >
< span class = "kn" > import< / span > < span class = "nn" > functools< / span >
< span class = "nb" > print< / span > < span class = "p" > (< / span > < span class = "n" > timeit< / span > < span class = "o" > .< / span > < span class = "n" > timeit< / span > < span class = "p" > (< / span > < span class = "n" > functools< / span > < span class = "o" > .< / span > < span class = "n" > partial< / span > < span class = "p" > (< / span > < span class = "n" > get_prime_factors_old< / span > < span class = "p" > ,< / span > < span class = "mi" > 1000000< / span > < span class = "p" > ),< / span > < span class = "n" > number< / span > < span class = "o" > =< / span > < span class = "mi" > 10< / span > < span class = "p" > ))< / span >
< span class = "nb" > print< / span > < span class = "p" > (< / span > < span class = "n" > timeit< / span > < span class = "o" > .< / span > < span class = "n" > timeit< / span > < span class = "p" > (< / span > < span class = "n" > functools< / span > < span class = "o" > .< / span > < span class = "n" > partial< / span > < span class = "p" > (< / span > < span class = "n" > get_prime_factors< / span > < span class = "p" > ,< / span > < span class = "mi" > 1000000< / span > < span class = "p" > ),< / span > < span class = "n" > number< / span > < span class = "o" > =< / span > < span class = "mi" > 10< / span > < span class = "p" > ))< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > get_prime_factors_old< / span > < span class = "p" > (< / span > < span class = "mi" > 100000< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "n" > get_prime_factors< / span > < span class = "p" > (< / span > < span class = "mi" > 100000< / span > < span class = "p" > ))< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< div class = "output_wrapper" >
< div class = "output" >
< div class = "output_area" > < div class = "prompt" > < / div >
< div class = "output_subarea output_stream output_stdout output_text" >
< pre > 6.370271180404346
5.785088868397899
< / pre >
< / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > Okay, actually no big difference. Kind of expected if you check how similar the implementations are. I though stopping if p is greater than the remainder may be a common case.< / p >
< p > Now we can use the prime factors to get all factors. For this each prime number can be used 0 to n times where n is the number how often the prime occurs in the prime factorization. For example, all possible fators for the primes $2, 2, 3$ are $2^0 \times 3^0, 2^1 \times 3^0, 2^2 \times 3^0, 2^0 \times 3^1, 2^1 \times 3^1, 2^2 \times 3^1$ which results in $1, 2, 4, 3, 6, 12$.< / p >
< p > So we proceed in the following way. Once we have the prime factors we group then. Then we calculate the factors for each group. For example for $2,2$ the factors would be $1, 2, 4$ as explained in the previous paragraph. Then we calculate the combinations of all factor groups.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [3]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > group< / span > < span class = "p" > (< / span > < span class = "n" > xs< / span > < span class = "p" > ):< / span >
< span class = "kn" > from< / span > < span class = "nn" > functools< / span > < span class = "k" > import< / span > < span class = "n" > reduce< / span >
< span class = "n" > rs< / span > < span class = "o" > =< / span > < span class = "p" > []< / span >
< span class = "k" > def< / span > < span class = "nf" > f< / span > < span class = "p" > (< / span > < span class = "n" > xss< / span > < span class = "p" > ,< / span > < span class = "n" > x< / span > < span class = "p" > ):< / span >
< span class = "k" > if< / span > < span class = "n" > xss< / span > < span class = "ow" > and< / span > < span class = "n" > x< / span > < span class = "ow" > in< / span > < span class = "n" > xss< / span > < span class = "p" > [< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > ]:< / span >
< span class = "n" > xss< / span > < span class = "p" > [< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > ]< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > x< / span > < span class = "p" > )< / span >
< span class = "k" > else< / span > < span class = "p" > :< / span >
< span class = "n" > xss< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > ([< / span > < span class = "n" > x< / span > < span class = "p" > ])< / span >
< span class = "k" > return< / span > < span class = "n" > xss< / span >
< span class = "k" > return< / span > < span class = "n" > reduce< / span > < span class = "p" > (< / span > < span class = "n" > f< / span > < span class = "p" > ,< / span > < span class = "n" > xs< / span > < span class = "p" > ,< / span > < span class = "p" > [])< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > group< / span > < span class = "p" > ([< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 5< / span > < span class = "p" > ])< / span > < span class = "o" > ==< / span > < span class = "p" > [[< / span > < span class = "mi" > 1< / span > < span class = "p" > ],< / span > < span class = "p" > [< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ],< / span > < span class = "p" > [< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ],< / span > < span class = "p" > [< / span > < span class = "mi" > 5< / span > < span class = "p" > ]])< / span >
< span class = "k" > def< / span > < span class = "nf" > group< / span > < span class = "p" > (< / span > < span class = "n" > xs< / span > < span class = "p" > ):< / span >
< span class = "n" > r< / span > < span class = "o" > =< / span > < span class = "p" > []< / span >
< span class = "k" > for< / span > < span class = "n" > x< / span > < span class = "ow" > in< / span > < span class = "n" > xs< / span > < span class = "p" > :< / span >
< span class = "k" > if< / span > < span class = "n" > r< / span > < span class = "ow" > and< / span > < span class = "n" > r< / span > < span class = "p" > [< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > ][< / span > < span class = "mi" > 0< / span > < span class = "p" > ]< / span > < span class = "o" > ==< / span > < span class = "n" > x< / span > < span class = "p" > :< / span >
< span class = "n" > r< / span > < span class = "p" > [< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > ]< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > (< / span > < span class = "n" > x< / span > < span class = "p" > )< / span >
< span class = "k" > else< / span > < span class = "p" > :< / span >
< span class = "n" > r< / span > < span class = "o" > .< / span > < span class = "n" > append< / span > < span class = "p" > ([< / span > < span class = "n" > x< / span > < span class = "p" > ])< / span >
< span class = "k" > return< / span > < span class = "n" > r< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > group< / span > < span class = "p" > ([< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 5< / span > < span class = "p" > ])< / span > < span class = "o" > ==< / span > < span class = "p" > [[< / span > < span class = "mi" > 1< / span > < span class = "p" > ],< / span > < span class = "p" > [< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ],< / span > < span class = "p" > [< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ],< / span > < span class = "p" > [< / span > < span class = "mi" > 5< / span > < span class = "p" > ]])< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > As you can see I had a fancy version with reduce first, but the loop is way more readable. We iterate over the input list. If the current item is in the last sublist of the return list we add it to the list. Otherwise we append a new sublist with the item.< / p >
< p > Next we write a function that calculates the resulting factors for a factor group.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [4]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > prime_group_to_factors< / span > < span class = "p" > (< / span > < span class = "n" > group< / span > < span class = "p" > ):< / span >
< span class = "n" > f< / span > < span class = "o" > =< / span > < span class = "n" > group< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ]< / span >
< span class = "n" > n< / span > < span class = "o" > =< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > group< / span > < span class = "p" > )< / span >
< span class = "k" > return< / span > < span class = "p" > [< / span > < span class = "n" > f< / span > < span class = "o" > **< / span > < span class = "n" > i< / span > < span class = "k" > for< / span > < span class = "n" > i< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "p" > )]< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > prime_group_to_factors< / span > < span class = "p" > ([< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ])< / span > < span class = "o" > ==< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 8< / span > < span class = "p" > ])< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > No we only have to get all factors by calculating the combination of two factor lists.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [5]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > combine_factors< / span > < span class = "p" > (< / span > < span class = "n" > xs< / span > < span class = "p" > ,< / span > < span class = "n" > ys< / span > < span class = "p" > ):< / span >
< span class = "k" > return< / span > < span class = "p" > [< / span > < span class = "n" > x< / span > < span class = "o" > *< / span > < span class = "n" > y< / span > < span class = "k" > for< / span > < span class = "n" > y< / span > < span class = "ow" > in< / span > < span class = "n" > ys< / span > < span class = "k" > for< / span > < span class = "n" > x< / span > < span class = "ow" > in< / span > < span class = "n" > xs< / span > < span class = "p" > ]< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > combine_factors< / span > < span class = "p" > ([< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ],< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 3< / span > < span class = "p" > ])< / span > < span class = "o" > ==< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 3< / span > < span class = "p" > ,< / span > < span class = "mi" > 6< / span > < span class = "p" > ,< / span > < span class = "mi" > 12< / span > < span class = "p" > ])< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > Now actually we want to combine an arbitrary number of factor lists.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [6]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > combine_factors< / span > < span class = "p" > (< / span > < span class = "n" > xss< / span > < span class = "p" > ):< / span >
< span class = "n" > ys< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ]< / span >
< span class = "k" > for< / span > < span class = "n" > xs< / span > < span class = "ow" > in< / span > < span class = "n" > xss< / span > < span class = "p" > :< / span >
< span class = "n" > ys< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > x< / span > < span class = "o" > *< / span > < span class = "n" > y< / span > < span class = "k" > for< / span > < span class = "n" > y< / span > < span class = "ow" > in< / span > < span class = "n" > ys< / span > < span class = "k" > for< / span > < span class = "n" > x< / span > < span class = "ow" > in< / span > < span class = "n" > xs< / span > < span class = "p" > ]< / span >
< span class = "k" > return< / span > < span class = "n" > ys< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [7]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > get_divisors< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > ):< / span >
< span class = "n" > prime_factors< / span > < span class = "o" > =< / span > < span class = "n" > get_prime_factors< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > )< / span >
< span class = "n" > prime_groups< / span > < span class = "o" > =< / span > < span class = "n" > group< / span > < span class = "p" > (< / span > < span class = "n" > prime_factors< / span > < span class = "p" > )< / span >
< span class = "n" > factor_groups< / span > < span class = "o" > =< / span > < span class = "nb" > list< / span > < span class = "p" > (< / span > < span class = "nb" > map< / span > < span class = "p" > (< / span > < span class = "n" > prime_group_to_factors< / span > < span class = "p" > ,< / span > < span class = "n" > prime_groups< / span > < span class = "p" > ))< / span >
< span class = "n" > divisors< / span > < span class = "o" > =< / span > < span class = "n" > combine_factors< / span > < span class = "p" > (< / span > < span class = "n" > factor_groups< / span > < span class = "p" > )< / span >
< span class = "n" > divisors< / span > < span class = "o" > .< / span > < span class = "n" > sort< / span > < span class = "p" > ()< / span >
< span class = "k" > return< / span > < span class = "n" > divisors< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > get_divisors< / span > < span class = "p" > (< / span > < span class = "mi" > 220< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 4< / span > < span class = "p" > ,< / span > < span class = "mi" > 5< / span > < span class = "p" > ,< / span > < span class = "mi" > 10< / span > < span class = "p" > ,< / span > < span class = "mi" > 11< / span > < span class = "p" > ,< / span > < span class = "mi" > 20< / span > < span class = "p" > ,< / span > < span class = "mi" > 22< / span > < span class = "p" > ,< / span > < span class = "mi" > 44< / span > < span class = "p" > ,< / span > < span class = "mi" > 55< / span > < span class = "p" > ,< / span > < span class = "mi" > 110< / span > < span class = "p" > ,< / span > < span class = "mi" > 220< / span > < span class = "p" > ])< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > get_divisors< / span > < span class = "p" > (< / span > < span class = "mi" > 13< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 13< / span > < span class = "p" > ])< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > get_divisors< / span > < span class = "p" > (< / span > < span class = "mi" > 26< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "mi" > 13< / span > < span class = "p" > ,< / span > < span class = "mi" > 26< / span > < span class = "p" > ])< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > We can finally start to get the solution. Remember that we only want to get the sum of proper divisors which are the divisors withouth the number itself.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [8]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > ):< / span >
< span class = "k" > return< / span > < span class = "nb" > sum< / span > < span class = "p" > (< / span > < span class = "n" > get_divisors< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > )[:< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > ])< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "mi" > 220< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "mi" > 284< / span > < span class = "p" > )< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "mi" > 284< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "mi" > 220< / span > < span class = "p" > )< / span >
< span class = "k" > def< / span > < span class = "nf" > is_amicable< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > ):< / span >
< span class = "n" > s< / span > < span class = "o" > =< / span > < span class = "n" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > )< / span >
< span class = "k" > if< / span > < span class = "n" > s< / span > < span class = "o" > ==< / span > < span class = "n" > number< / span > < span class = "p" > :< / span >
< span class = "k" > return< / span > < span class = "kc" > False< / span >
< span class = "k" > return< / span > < span class = "n" > number< / span > < span class = "o" > ==< / span > < span class = "n" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "n" > s< / span > < span class = "p" > )< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > is_amicable< / span > < span class = "p" > (< / span > < span class = "mi" > 220< / span > < span class = "p" > ))< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > The if statement is very import because a number pair $a, b$ is only amicable if $a \neq b$.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [9]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "n" > amicable_till_10000< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "k" > for< / span > < span class = "n" > i< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 10000< / span > < span class = "p" > )< / span > < span class = "k" > if< / span > < span class = "n" > is_amicable< / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "p" > )]< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "nb" > sum< / span > < span class = "p" > (< / span > < span class = "n" > amicable_till_10000< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "mi" > 31626< / span > < span class = "p" > )< / span >
< span class = "nb" > print< / span > < span class = "p" > (< / span > < span class = "nb" > sum< / span > < span class = "p" > (< / span > < span class = "n" > amicable_till_10000< / span > < span class = "p" > ))< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< div class = "output_wrapper" >
< div class = "output" >
< div class = "output_area" > < div class = "prompt" > < / div >
< div class = "output_subarea output_stream output_stdout output_text" >
< pre > 31626
< / pre >
< / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > I found some brute force solutions in the Euler forum and they claim to have a sick performance. I want to compare them.< / p >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [10]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > brute_force< / span > < span class = "p" > ():< / span >
< span class = "k" > return< / span > < span class = "nb" > sum< / span > < span class = "p" > ([< / span > < span class = "n" > i< / span > < span class = "k" > for< / span > < span class = "n" > i< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 10000< / span > < span class = "p" > )< / span > < span class = "k" > if< / span > < span class = "n" > is_amicable< / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "p" > )])< / span >
< span class = "kn" > import< / span > < span class = "nn" > timeit< / span >
< span class = "nb" > print< / span > < span class = "p" > (< / span > < span class = "n" > timeit< / span > < span class = "o" > .< / span > < span class = "n" > timeit< / span > < span class = "p" > (< / span > < span class = "n" > brute_force< / span > < span class = "p" > ,< / span > < span class = "n" > number< / span > < span class = "o" > =< / span > < span class = "mi" > 10< / span > < span class = "p" > ))< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< div class = "output_wrapper" >
< div class = "output" >
< div class = "output_area" > < div class = "prompt" > < / div >
< div class = "output_subarea output_stream output_stdout output_text" >
< pre > 108.68903765424807
< / pre >
< / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing code_cell rendered" >
< div class = "input" >
< div class = "prompt input_prompt" > In [14]:< / div >
< div class = "inner_cell" >
< div class = "input_area" >
< div class = " highlight hl-ipython3" > < pre > < span > < / span > < span class = "k" > def< / span > < span class = "nf" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > ):< / span >
< span class = "kn" > from< / span > < span class = "nn" > math< / span > < span class = "k" > import< / span > < span class = "n" > sqrt< / span >
< span class = "n" > s< / span > < span class = "o" > =< / span > < span class = "mi" > 1< / span >
< span class = "k" > for< / span > < span class = "n" > i< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "mi" > 2< / span > < span class = "p" > ,< / span > < span class = "nb" > int< / span > < span class = "p" > (< / span > < span class = "n" > sqrt< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "p" > ))< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "p" > ):< / span >
< span class = "k" > if< / span > < span class = "n" > n< / span > < span class = "o" > %< / span > < span class = "n" > i< / span > < span class = "o" > ==< / span > < span class = "mi" > 0< / span > < span class = "p" > :< / span >
< span class = "n" > s< / span > < span class = "o" > +=< / span > < span class = "n" > i< / span >
< span class = "n" > s< / span > < span class = "o" > +=< / span > < span class = "p" > (< / span > < span class = "n" > n< / span > < span class = "o" > //< / span > < span class = "n" > i< / span > < span class = "p" > )< / span >
< span class = "k" > return< / span > < span class = "n" > s< / span >
< span class = "k" > assert< / span > < span class = "p" > (< / span > < span class = "n" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "mi" > 220< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "mi" > 284< / span > < span class = "p" > )< / span >
< span class = "k" > def< / span > < span class = "nf" > is_amicable< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > ):< / span >
< span class = "n" > s< / span > < span class = "o" > =< / span > < span class = "n" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "n" > number< / span > < span class = "p" > )< / span >
< span class = "k" > if< / span > < span class = "n" > s< / span > < span class = "o" > ==< / span > < span class = "n" > number< / span > < span class = "p" > :< / span >
< span class = "k" > return< / span > < span class = "kc" > False< / span >
< span class = "k" > return< / span > < span class = "n" > number< / span > < span class = "o" > ==< / span > < span class = "n" > sum_of_proper_divisors< / span > < span class = "p" > (< / span > < span class = "n" > s< / span > < span class = "p" > )< / span >
< span class = "k" > def< / span > < span class = "nf" > brute_force< / span > < span class = "p" > ():< / span >
< span class = "k" > return< / span > < span class = "nb" > sum< / span > < span class = "p" > ([< / span > < span class = "n" > i< / span > < span class = "k" > for< / span > < span class = "n" > i< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "mi" > 10000< / span > < span class = "p" > )< / span > < span class = "k" > if< / span > < span class = "n" > is_amicable< / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "p" > )])< / span >
< span class = "kn" > import< / span > < span class = "nn" > timeit< / span >
< span class = "nb" > print< / span > < span class = "p" > (< / span > < span class = "n" > timeit< / span > < span class = "o" > .< / span > < span class = "n" > timeit< / span > < span class = "p" > (< / span > < span class = "n" > brute_force< / span > < span class = "p" > ,< / span > < span class = "n" > number< / span > < span class = "o" > =< / span > < span class = "mi" > 10< / span > < span class = "p" > ))< / span >
< / pre > < / div >
< / div >
< / div >
< / div >
< div class = "output_wrapper" >
< div class = "output" >
< div class = "output_area" > < div class = "prompt" > < / div >
< div class = "output_subarea output_stream output_stdout output_text" >
< pre > 0.7862764837699387
< / pre >
< / div >
< / div >
< / div >
< / div >
< / div >
< div class = "cell border-box-sizing text_cell rendered" >
< div class = "prompt input_prompt" >
< / div >
< div class = "inner_cell" >
< div class = "text_cell_render border-box-sizing rendered_html" >
< p > This is actually really embarrassing. Especially I was aware of the sqrt trick all the way but did not manage to put it into use properly. Another example where greate engineer will leave you far behind compared to great thinking.< / p >
2018-02-06 22:22:00 +01:00
< p > < strong > Supplement:< / strong > Also I have discovered that my sum of proper divisors function has a bug when n is a square, because in that case < em > i == (n // 1)< / em > which adds this value two times. So there is no amicable number under 10000 which is also a square because otherwise we would have failed.< / p >
2018-02-06 19:23:59 +01:00
< / div >
< / div >
< / div >
< / div >
< / div >
< / body >
< / html >