Done puzzle 39
[project-euler.git] / euler35.rb
1 require 'ruby-prof'
2 load 'primes.rb'
3 load 'array-numbers.rb'
4
5 class Array
6 # def to_i
7 # self.map {|d| d.to_s}.join.to_i
8 # end
9
10 # def to_r
11 # Rational(self[0].to_i, self[1].to_i)
12 # end
13
14 def all_rotations
15 (0...self.length).map {|r| self.rotate(r)}
16 end
17 end
18
19 class Integer
20 # @@primes = Primes.instance
21
22 # def prime?
23 # @@primes.include? self
24 # end
25
26 def pl
27 @@primes.primes.length
28 end
29
30 def circular_prime?
31 self.prime? && self.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| n.prime?}
32 end
33
34 # def to_digits
35 # self.to_s.split('').map {|d| d.to_i}
36 # end
37 end
38
39
40
41 RubyProf.start
42
43 # (10**6).prime?
44
45
46 pl0 = 0.pl
47 beginning_time = Time.now
48 cps = (2..10**6).select {|n| n.circular_prime?}
49 end_time = Time.now
50 pl1 = 0.pl
51 puts "Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached"
52 puts cps.length
53
54 result = RubyProf.stop
55
56 printer = RubyProf::MultiPrinter.new(result)
57 printer.print(:path => ".", :profile => "profile")
58
59