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