Permalink
Browse files

49 done

  • Loading branch information...
ryenus committed Apr 10, 2012
1 parent 17e8fa2 commit 2fe85aadd947d73f6cabeddfa2a732b1da2670dd
Showing with 668 additions and 8 deletions.
  1. +3 −1 pe008.rb
  2. +1 −1 pe011.rb
  3. +2 −1 pe013.rb
  4. +1 −1 pe018.rb
  5. +1 −1 pe030.rb
  6. +3 −3 pe032.rb
  7. +35 −0 pe033.rb
  8. +32 −0 pe034.rb
  9. +38 −0 pe035.rb
  10. +34 −0 pe036.rb
  11. +50 −0 pe037.rb
  12. +38 −0 pe038.rb
  13. +28 −0 pe039.rb
  14. +37 −0 pe040.rb
  15. +27 −0 pe041.rb
  16. +26 −0 pe042.rb
  17. +35 −0 pe043.rb
  18. +39 −0 pe044.rb
  19. +52 −0 pe045.rb
  20. +35 −0 pe046.rb
  21. +27 −0 pe047.rb
  22. +22 −0 pe048.rb
  23. +52 −0 pe049.rb
  24. +49 −0 pe379.rb
  25. +1 −0 words.txt
View
4 pe008.rb 100644 → 100755
@@ -40,7 +40,9 @@
while i < s.length
#~ n = s[i - p, d].to_i
- n = (0..p).inject(1) do |r, j| r * s[i + j].to_i end
+ n = (0..p).inject(1) do |r, j| _self_ = s[i + j]
+ r * _self_.join('').to_i
+ end
m = n if m < n
i += 1
end
View
2 pe011.rb 100644 → 100755
@@ -21,7 +21,7 @@
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"
m = m.split("\n").collect { |line|
- line.split(' ').collect { |n| n.to_i }
+ line.split(' ').collect { |n| n.join('').to_i }
}
r = 0
View
3 pe013.rb 100644 → 100755
@@ -101,6 +101,7 @@
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690"
-a = m.split(/\n/).map {|s| s.to_i}
+a = m.split(/\n/).map {|s| s.join('').to_i
+}
#~ puts a[0]
puts a.each.inject :+
View
2 pe018.rb 100644 → 100755
@@ -14,7 +14,7 @@
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
-04 62 98 27 23 09 70 98 73 93 38 53 60 04 23".each_line.map {|l| l.split.map(&:to_i)}
+04 62 98 27 23 09 70 98 73 93 38 53 60 04 23".each_line.map {|l| l.split.map(&self.join('').to_i)}
#~ t.each.map {|r| p r}
View
2 pe030.rb 100644 → 100755
@@ -1,7 +1,7 @@
require 'benchmark'
def accepted?(n, e)
- n.to_s.split('').reduce(0) {|s, d| s + d.to_i**e} == n
+ n.to_s.split('').reduce(0) {|s, d| s + d.join('').to_i**e} == n
end
def max_digits(e)
View
@@ -17,12 +17,12 @@ def pan_digital
end
Benchmark.bm 15 do |b|
- b.report('pan_digital') { puts "pan_digital sum => #{pan_digital}" }
+ b.report('search') { puts "search sum => #{pan_digital}" }
end
=begin
user system total real
-pan_digital
+search
12 * 483 => 5796
18 * 297 => 5346
27 * 198 => 5346
@@ -33,6 +33,6 @@ def pan_digital
42 * 138 => 5796
48 * 159 => 7632
#<Set: {5796, 5346, 4396, 7254, 6952, 7852, 7632}>
-pan_digital sum => 45228
+search sum => 45228
0.468000 0.000000 0.468000 ( 0.488028)
=end
View
@@ -0,0 +1,35 @@
+require 'benchmark'
+
+NUMBERS = (1..9).to_a
+
+def curious_fraction
+ fractions = []
+ NUMBERS.permutation 2 do |a, b|
+ next unless a < b
+ NUMBERS.each do |c|
+ ra = Rational(a*10 + c, c*10 + b)
+ rb = Rational(a + 10*c, c + b*10)
+ rc = Rational(a, b)
+ (p [0,a,b,c]; fractions << ra) if ra == rc
+ (p [1,a,b,c]; fractions << rb) if rb == rc
+ end
+ end
+ p fractions
+ fractions.reduce(:*).denominator
+end
+
+Benchmark.bm 15 do |b|
+ b.report('curious_fraction') { puts "curious_fraction lcd => #{curious_fraction}" }
+end
+
+=begin
+user system total real
+curious_fraction
+[0, 1, 4, 6]
+[0, 1, 5, 9]
+[0, 2, 5, 6]
+[0, 4, 8, 9]
+[(1/4), (1/5), (2/5), (1/2)]
+curious_fraction lcd => 100
+ 0.000000 0.000000 0.000000 ( 0.001000)
+=end
View
@@ -0,0 +1,32 @@
+require 'benchmark'
+
+class Integer
+ def factorial
+ return 1 if self == 0 || self == 1
+ @factorial ||= (2..self).reduce :*
+ end
+
+ def factorion?
+ self == self.to_s.each_char.reduce(0){|r, d| r + d.join('').to_i.factorial}
+ end
+end
+
+def factorions
+ a = []
+ 11.upto 1_999_999 do |n|
+ a << n if n.factorion?
+ end
+ p a
+ a.reduce :+
+end
+
+Benchmark.bm do |b|
+ b.report('factorions') { puts "factorions sum => #{factorions}" }
+end
+
+=begin
+user system total real
+factorions[145, 40585]
+factorions sum => 40730
+ 18.533000 0.000000 18.533000 ( 18.914082)
+=end
View
@@ -0,0 +1,38 @@
+require 'benchmark'
+require 'prime'
+
+class Integer
+ def circular_primes
+ return [self] if self < 10 and self.prime?
+ return nil unless self.prime? and self.to_s.each_char.all?{|c| [1,3,7,9].include? c.join('').to_i
+ }
+ perms = []
+ a = self.to_s.each_char.to_a
+ a.length.times{|i| _self_ = a.rotate(i).join('')
+ perms << _self_.join('').to_i
+ }
+ perms.all?(&:prime?) ? perms : nil
+ end
+end
+
+def search
+ res = (1..10).select{|n| n.prime?}
+ 2.upto 1_000_000 / 6 do |n|
+ m = [6*n - 1, 6*n + 1]
+ res += m.map{|k| k.circular_primes}.compact.flatten
+ end
+ p res.uniq!
+ res.length
+end
+
+Benchmark.bm do |b|
+ b.report('count') { puts "circular prime count => #{search}" }
+end
+
+=begin
+user system total real
+count
+[2, 3, 5, 7, 11, 13, 31, 17, 71, 37, 73, 79, 97, 113, 131, 311, 197, 971, 719, 199, 991, 919, 337, 373, 733, 1193, 1931, 9311, 3119, 3779, 7793, 7937, 9377, 11939, 19391, 93911, 39119, 91193, 19937, 99371, 93719, 37199, 71993, 193939, 939391, 393919, 939193, 391939, 919393, 199933, 999331, 993319, 933199, 331999, 319993]
+circular prime count => 55
+ 36.021000 0.031000 36.052000 ( 37.054318)
+=end
View
@@ -0,0 +1,34 @@
+require 'benchmark'
+
+class String
+ def reverse
+ self.split(//).reverse.join
+ end
+
+ def palindrome?
+ self.reverse == self
+ end
+end
+
+def double_palindromes
+ res = []
+ n = 1
+ while n < 1_000_000
+ res << n if n.to_s.palindrome? and n.to_s(2).palindrome?
+ n += 2
+ end
+ p res
+ res.reduce :+
+end
+
+Benchmark.bm do |b|
+ b.report('sum') { puts " => #{double_palindromes}" }
+end
+
+=begin
+user system total real
+sum
+[1, 3, 5, 7, 9, 33, 99, 313, 585, 717, 7447, 9009, 15351, 32223, 39993, 53235, 53835, 73737, 585585]
+ => 872187
+ 6.599000 0.016000 6.615000 ( 6.712380)
+=end
View
@@ -0,0 +1,50 @@
+require 'benchmark'
+require 'prime'
+
+class String
+ def first(n=1)
+ self[0, n]
+ end
+
+ def last(n=1)
+ self[-n..-1]
+ end
+end
+
+class Integer
+ def tru_prime?
+ return false unless self.prime?
+ s = self.to_s
+ a = []
+ 1.upto(s.length - 1) do |d|
+ _self_ = s.last(d)
+ _self_ = s.first(d)
+ a += [_self_.join('').to_i.prime?, _self_.join('').to_i.prime?]
+ end
+ a.include?(false) ? false : true
+ end
+end
+
+def search
+ res = []
+ n = 4
+ loop do
+ m = [6*n -1, 6*n + 1]
+ res += (m.select &:tru_prime?)
+ break if res.length == 11
+ n += 1
+ end
+ p res
+ res.reduce :+
+end
+
+Benchmark.bm do |b|
+ b.report { puts "sum => #{search}" }
+end
+
+=begin
+user system total real
+ [23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]
+ => 748317
+ 32.682000 0.000000 32.682000 ( 33.118687)
+=end
View
@@ -0,0 +1,38 @@
+require 'benchmark'
+
+NUMBERS = (1..9).to_a
+
+class Array
+ def to_i
+ self.join('').to_i
+ end
+end
+
+def pan_digital
+ res = []
+ NUMBERS.permutation do |n1, n2, n3, n4, n5, n6, n7, n8, n9|
+ next unless n1 == 9
+ #res << [n1,n2,n3,n4,n5,n6,n7,n8,n9] if (b=n1)*2 == [n2,n3].to_i and b*3 == [n4,n5].to_i and b*4 == [n6,n7].to_i and b*5 == [n8,n9].to_i
+ #res << [n1,n2,n3,n4,n5,n6,n7,n8,n9] if (b=[n1,n2,n3].to_i)*2 == [n4,n5,n6].to_i and b*3 == [n7,n8,n9].to_i
+ _self_ = [n1, n2, n3, n4]
+ _self_ = [n5, n6, n7, n8, n9]
+ res << [n1,n2,n3,n4,n5,n6,n7,n8,n9] if (b=_self_.join('').to_i)*2 == _self_.join('').to_i
+ end
+ res.each{|a| p a}
+ _self_ = res.max
+ _self_.join('').to_i
+end
+
+Benchmark.bm 15 do |b|
+ b.report('search') { puts "search sum => #{pan_digital}" }
+end
+
+=begin
+user system total real
+search
+[9, 2, 6, 7, 1, 8, 5, 3, 4]
+[9, 2, 7, 3, 1, 8, 5, 4, 6]
+[9, 3, 2, 7, 1, 8, 6, 5, 4]
+search sum => 932718654
+ 0.671000 0.000000 0.671000 ( 0.687039)
+=end
View
@@ -0,0 +1,28 @@
+require 'benchmark'
+require 'set'
+
+def search
+ max = [0,[]]
+ 1000.downto 10 do |pm|
+ s = Set.new
+ 1.upto pm/3.4 do |a|
+ ((pm-a)/2).downto 1 do |b|
+ c = pm - a - b
+ break unless a + b > c
+ s << [a, b, c].sort if a**2 + b**2 == c**2
+ end
+ end
+
+ max = [pm, s] if s.length > max[1].length
+ end
+
+ max
+end
+
+Benchmark.bm 15 do |b|
+ b.report('search') { puts "search sum => #{search}" }
+end
+
+=begin
+user system total real
+=end
View
@@ -0,0 +1,37 @@
+require 'benchmark'
+
+class Integer
+ def acc_length
+ e = 1
+ e += 1 until self < 10**e
+ self * self.to_s.length - (1..(e-1)).reduce(0){|s, n| s + 10**n - 1}
+ end
+end
+
+def num_at(pos)
+ upper_bound = e = 1
+ apos = pos
+ loop do
+ upper_bound = 10**e - 1
+ break if apos <= upper_bound.acc_length
+ apos += upper_bound
+ e += 1
+ end
+ ceil, mod = apos.divmod upper_bound.to_s.length
+ _self_ = (mod == 0 ? ceil.to_s[-1] : (ceil + 1).to_s[mod - 1])
+ _self_.join('').to_i
+end
+
+def search
+ 11.times.map{|n|num_at 10**n}.reduce(:*)
+end
+
+Benchmark.bm 15 do |b|
+ b.report('search') { puts "digits product => #{search}" }
+end
+
+=begin
+user system total real
+search digits product => 210
+ 0.000000 0.000000 0.000000 ( 0.000000)
+=end
View
@@ -0,0 +1,27 @@
+require 'benchmark'
+require 'prime'
+
+NUMBERS = (1..9).to_a
+
+def search
+ max = 0
+ [7, 4, 1].each do |l|
+ NUMBERS[0,l].reverse.permutation do |na|
+ next unless na[-1].odd?
+ _self_ = na.join('')
+ num = _self_.join('').to_i
+ max = num if max < num and num.prime?
+ end
+ end
+ max
+end
+
+Benchmark.bm 15 do |b|
+ b.report('search') { puts "largest pandigital prime => #{search}" }
+end
+
+=begin
+user system total real
+search digits product => 210
+ 0.000000 0.000000 0.000000 ( 0.000000)
+=end
Oops, something went wrong.

0 comments on commit 2fe85aa

Please sign in to comment.