From: Neil Smith Date: Thu, 16 Feb 2017 11:17:21 +0000 (+0000) Subject: Done puzzle 35 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=f0a9468751259255c113b11729e30169f9b2911e;p=project-euler.git Done puzzle 35 --- diff --git a/euler35.ipynb b/euler35.ipynb new file mode 100644 index 0000000..dcd6039 --- /dev/null +++ b/euler35.ipynb @@ -0,0 +1,659 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "#" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "load 'primes.rb'\n", + "$primes = Primes.instance" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":to_r" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Array\n", + " def to_i\n", + " self.map {|d| d.to_s}.join.to_i\n", + " end\n", + " \n", + " def to_r\n", + " Rational(self[0].to_i, self[1].to_i)\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":to_digits" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Integer\n", + " def to_digits\n", + " self.to_s.split('').map {|d| d.to_i}\n", + " end\n", + "end " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":all_rotations" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Array\n", + " def all_rotations\n", + " (0...self.length).map {|r| self.rotate(r)}\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1, 2, 3], [2, 3, 1], [3, 1, 2]]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[1,2,3].all_rotations" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[123, 231, 312]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[1,2,3].all_rotations.map {|n| n.to_i}" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[false, false, false]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[1,2,3].all_rotations.map {|n| n.to_i}.map {|n| $primes.include? n}" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[true, true]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "17.to_digits.all_rotations.map {|n| n.to_i}.map {|n| $primes.include? n}" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "false" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[1,2,3].all_rotations.map {|n| n.to_i}.all? {|n| $primes.include? n}" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "true" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "17.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| $primes.include? n}" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":circular_prime?" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Integer\n", + " @@primes = Primes.instance\n", + " \n", + " def prime?\n", + " @@primes.include? self\n", + " end\n", + " \n", + " def pl\n", + " @@primes.primes.length\n", + " end\n", + " \n", + " def circular_prime?\n", + " self.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| @@primes.include? n} if self.prime?\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + ":circular_prime?" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Integer\n", + " @@primes = Primes.instance\n", + " \n", + " def prime?\n", + " @@primes.include? self\n", + " end\n", + " \n", + " def pl\n", + " @@primes.primes.length\n", + " end\n", + " \n", + " def circular_prime?\n", + " self.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| n.prime?} if self.prime?\n", + " end\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1229" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "0.pl" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(2..100).select {|n| n.circular_prime?}" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(2..100).select {|n| n.circular_prime?}" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "13" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(2..100).select {|n| n.circular_prime?}.length" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# ps = $primes.take_while {|i| i <= 10**6}\n", + "# ps.length" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "# (2..10**4).select {|n| n.circular_prime?}" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Time elapsed 0.125950399 seconds, going from 1229 to 1230 primes cached\n" + ] + }, + { + "data": { + "text/plain": [ + "33" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pl0 = 0.pl\n", + "beginning_time = Time.now\n", + "cps = (2..10**4).select {|n| n.circular_prime?}\n", + "end_time = Time.now\n", + "pl1 = 0.pl\n", + "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n", + "cps.length" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Time elapsed 0.123380027 seconds, going from 1230 to 1230 primes cached\n" + ] + }, + { + "data": { + "text/plain": [ + "33" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pl0 = 0.pl\n", + "beginning_time = Time.now\n", + "cps = (2..10**4).select {|n| n.circular_prime?}\n", + "end_time = Time.now\n", + "pl1 = 0.pl\n", + "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n", + "cps.length" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Time elapsed 9.59874264 seconds, going from 1230 to 9593 primes cached\n" + ] + }, + { + "data": { + "text/plain": [ + "43" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pl0 = 0.pl\n", + "beginning_time = Time.now\n", + "cps = (2..10**5).select {|n| n.circular_prime?}\n", + "end_time = Time.now\n", + "pl1 = 0.pl\n", + "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n", + "cps.length" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Time elapsed 7.768858416 seconds, going from 9593 to 9593 primes cached\n" + ] + }, + { + "data": { + "text/plain": [ + "43" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pl0 = 0.pl\n", + "beginning_time = Time.now\n", + "cps = (2..10**5).select {|n| n.circular_prime?}\n", + "end_time = Time.now\n", + "pl1 = 0.pl\n", + "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n", + "cps.length" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Time elapsed 710.264030183 seconds, going from 9593 to 78499 primes cached\n" + ] + }, + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pl0 = 0.pl\n", + "beginning_time = Time.now\n", + "cps = (2..10**6).select {|n| n.circular_prime?}\n", + "end_time = Time.now\n", + "pl1 = 0.pl\n", + "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n", + "cps.length" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Time elapsed 597.009405584 seconds, going from 78499 to 78499 primes cached\n" + ] + }, + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pl0 = 0.pl\n", + "beginning_time = Time.now\n", + "cps = (2..10**6).select {|n| n.circular_prime?}\n", + "end_time = Time.now\n", + "pl1 = 0.pl\n", + "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n", + "cps.length" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Ruby 2.4.0", + "language": "ruby", + "name": "ruby" + }, + "language_info": { + "file_extension": ".rb", + "mimetype": "application/x-ruby", + "name": "ruby", + "version": "2.4.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/euler35.rb b/euler35.rb new file mode 100644 index 0000000..ebe8450 --- /dev/null +++ b/euler35.rb @@ -0,0 +1,56 @@ +require 'ruby-prof' +load 'primes.rb' + +class Array + def to_i + self.map {|d| d.to_s}.join.to_i + end + + def to_r + Rational(self[0].to_i, self[1].to_i) + end + + def all_rotations + (0...self.length).map {|r| self.rotate(r)} + end +end + +class Integer + @@primes = Primes.instance + + def prime? + @@primes.include? self + end + + def pl + @@primes.primes.length + end + + def circular_prime? + self.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| n.prime?} if self.prime? + end + + def to_digits + self.to_s.split('').map {|d| d.to_i} + end +end + + +(10**6).prime? + +RubyProf.start + +pl0 = 0.pl +beginning_time = Time.now +cps = (2..10**6).select {|n| n.circular_prime?} +end_time = Time.now +pl1 = 0.pl +puts "Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached" +puts cps.length + +result = RubyProf.stop + +printer = RubyProf::MultiPrinter.new(result) +printer.print(:path => ".", :profile => "profile") + +