Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Start on test strap and moving fastest answers into default file loca…

…tions. Will need to reimplement some here
  • Loading branch information...
commit 5b6cf97dada0f99e1a9758c8c6801655e64f8d25 1 parent 0cd1b8b
@jmmastey authored
View
6 p002.rb
@@ -7,14 +7,14 @@ def sum_even_fibbs max
break if b >= max
- print " #{b}"
+ #print " #{b}"
sum += b if (b%2 == 0)
end
- puts "\n"
+ #puts "\n"
sum
end
-max = (!ARGV.empty? && ARGV[0].to_i) || 4000000
+max = (!ARGV.empty? && ARGV[0].to_i) || 4_000_000
puts sum_even_fibbs max
View
2  p003.rb
@@ -9,7 +9,7 @@ def largest_prime_factor max
next unless max%i == 0
# test primality
- puts "found candidate #{i.to_i}\n"
+ #puts "found candidate #{i.to_i}\n"
lgst = i if is_prime? i
end
View
4 p004.rb
@@ -1,7 +1,9 @@
max = 0
999.downto 100 do |i|
- 999.downto 100 do |j|
+ i.downto 100 do |j|
sum = i*j
+ break if sum < max
+
max = [max,sum].max if sum.to_s == sum.to_s.reverse
end
end
View
4 p004_optimized.rb → p004_slow.rb
@@ -1,9 +1,7 @@
max = 0
999.downto 100 do |i|
- i.downto 100 do |j|
+ 999.downto 100 do |j|
sum = i*j
- break if sum < max
-
max = [max,sum].max if sum.to_s == sum.to_s.reverse
end
end
View
35 p005.rb
@@ -1,28 +1,23 @@
-def mods_work? num
- [11,12,13,14,15,16,17,18,19,20].each do |i|
- return false if (num%i != 0)
+class Fixnum
+ def prime?
+ ('1' * self.to_i) !~ /^1?$|^(11+?)\1+$/
end
-
- return true
end
-def multsum arr
- sum = 1
- arr.each { |i| sum *= i }
- sum
-end
+k = 20
-shitty_answer = multsum((11..20).to_a)
-puts "looking for something better than #{shitty_answer}"
+operands = []
-nm = 0
-step = 2520
-while nm <= shitty_answer
- nm += step
+2.upto k do |i|
+ next unless i.prime?
- puts "trying #{nm}"
- if mods_work? nm
- puts nm
- exit
+ max = 0
+ 1.upto k do |j|
+ next if i**j > k
+ max = i**j
end
+
+ operands << max
end
+
+puts operands.inject(1) { |sum,i| sum *= i }
View
23 p005_euler.rb
@@ -1,23 +0,0 @@
-class Fixnum
- def prime?
- ('1' * self.to_i) !~ /^1?$|^(11+?)\1+$/
- end
-end
-
-k = 20
-
-operands = []
-
-2.upto k do |i|
- next unless i.prime?
-
- max = 0
- 1.upto k do |j|
- next if i**j > k
- max = i**j
- end
-
- operands << max
-end
-
-puts operands.inject(1) { |sum,i| sum *= i }
View
28 p005_naive.rb
@@ -0,0 +1,28 @@
+def mods_work? num
+ [11,12,13,14,15,16,17,18,19,20].each do |i|
+ return false if (num%i != 0)
+ end
+
+ return true
+end
+
+def multsum arr
+ sum = 1
+ arr.each { |i| sum *= i }
+ sum
+end
+
+shitty_answer = multsum((11..20).to_a)
+#puts "looking for something better than #{shitty_answer}"
+
+nm = 0
+step = 2520
+while nm <= shitty_answer
+ nm += step
+
+ #puts "trying #{nm}"
+ if mods_work? nm
+ puts nm
+ exit
+ end
+end
View
7 p006.rb
@@ -3,6 +3,7 @@
sum_of_sq = (1..k).inject(0) { |sum,i| sum += i**2 }
sq_of_sum = ((1..k).inject(0) { |sum,i| sum += i })**2
-puts "sum of squares: #{sum_of_sq}"
-puts "square of sums: #{sq_of_sum}"
-puts "diff of the two: #{sq_of_sum - sum_of_sq}"
+#puts "sum of squares: #{sum_of_sq}"
+#puts "square of sums: #{sq_of_sum}"
+#puts "diff of the two: #{sq_of_sum - sum_of_sq}"
+puts sq_of_sum - sum_of_sq
View
18 p007.rb
@@ -1,17 +1,3 @@
require 'tools'
-
-p_num = 6
-p_max = 10001
-curr = 13
-primes = [3,5,7,11]
-
-while p_num <= p_max
- curr += 2
- if curr.prime?(primes)
- p_num += 1
- primes << curr
- end
-end
-
-puts "#{p_max}st prime is #{curr}"
-
+primes = Sieve.primes_to_1m
+puts primes[10000]
View
17 p007_slow.rb
@@ -0,0 +1,17 @@
+require 'tools'
+
+p_num = 6
+p_max = 10001
+curr = 13
+primes = [3,5,7,11]
+
+while p_num <= p_max
+ curr += 2
+ if curr.prime?(primes)
+ p_num += 1
+ primes << curr
+ end
+end
+
+puts "#{p_max}st prime is #{curr}"
+
View
2  p008.rb
@@ -28,4 +28,4 @@
max = [curr,max].max
end
-puts "max is #{max}"
+puts max
View
7 p009.rb
@@ -4,13 +4,16 @@ def is_pythagorean(a, b, c)
(a**2 + b**2) == c**2
end
-target = 10000
+target = 1000
a = b = 0
(target-3).downto 1 do |c|
ab = target - c
(ab-1).downto((ab/2)+1) do |b|
a = ab-b
- puts "#{a},#{b},#{c} is pythagorean (product: #{a*b*c})" if is_pythagorean(a,b,c)
+ if is_pythagorean(a,b,c)
+ puts a*b*c
+ exit
+ end
end
end
View
2  p011.rb
@@ -38,7 +38,7 @@
diag_r = a[i][j]*a[i-1][j-1]*a[i-2][j-2]*a[i-3][j-3] unless (j < 3 || i+3 > xsentry)
max = [max,across,down,diag_l,diag_r].max
- puts "#{i},#{j}: #{max} (#{a[i][j]}) (#{across}, #{down}, #{diag_l}, #{diag_r})"
+ #puts "#{i},#{j}: #{max} (#{a[i][j]}) (#{across}, #{down}, #{diag_l}, #{diag_r})"
end
end
View
2  p012.rb
@@ -10,7 +10,7 @@
divisors += 2 if (0 == triangle % divisor)
end
- puts "#{triangle}: #{divisors}" if divisors > 300
+ #puts "#{triangle}: #{divisors}" if divisors > 300
end
puts triangle
View
0  p068.rb 100644 → 100755
File mode changed
View
0  p077.rb 100644 → 100755
File mode changed
View
0  p085.rb 100644 → 100755
File mode changed
View
0  p089.rb 100644 → 100755
File mode changed
View
0  p089_roman.txt 100644 → 100755
File mode changed
View
0  p100.rb 100644 → 100755
File mode changed
View
0  p104.rb 100644 → 100755
File mode changed
View
0  p120.rb 100644 → 100755
File mode changed
View
0  p124.rb 100644 → 100755
File mode changed
View
0  p179.rb 100644 → 100755
File mode changed
View
3  test.rb
@@ -1,3 +0,0 @@
-#!/usr/bin/ruby
-
-p ARGV[0]
View
10 testall.rb
@@ -0,0 +1,10 @@
+1.upto 400 do |i|
+ test = "p%03d.rb" % i
+ next unless File.exists? test
+
+ start = Time.now
+ res = `ruby #{test}`
+ endt = Time.now
+
+ puts "Executed %d in %3.5fs and got %s" % [i, (endt.to_f-start.to_f), res]
+end
View
2  tools.rb
@@ -394,7 +394,7 @@ def self.phi_to_10m
end
def self.primes_to(max, ashash = false)
- return self.quick_primes_to(max, ashash) if max > 1_000_000
+ return self.quick_primes_to max if max > 1_000_000
series = (2..max).to_a
sqrt = max**0.5
Please sign in to comment.
Something went wrong with that request. Please try again.