Skip to content

Instantly share code, notes, and snippets.

@wobh
Last active December 15, 2015 11:19
Show Gist options
  • Save wobh/5252231 to your computer and use it in GitHub Desktop.
Save wobh/5252231 to your computer and use it in GitHub Desktop.
Some fun with primes in ruby
require 'prime'
def list_primes_below(n)
# List primes below argument
return Prime::EratosthenesGenerator.new.take_while { |i| i <= n }
end
def list_primes_count(n)
# List argument number of primes
return Prime::EratosthenesGenerator.new.take(n)
end
def prime_pair_combos_below(n)
# All combinations of prime pairs for primes up to n
primes = list_primes_below(n)
primes.shift
prime_pairs = [[2, 2]]
return prime_pairs += primes.repeated_combination(2).to_a
end
def prime_pair_combos_count(n)
# All combinations of prime pairs for n number of primes
primes = list_primes_count(n)
primes.shift
prime_pairs = [[2, 2]]
return prime_pairs += primes.repeated_combination(2).to_a
end
def prime_pair_permutes_below(n)
# All permutations of primes below n
primes = list_primes_below(n)
primes.shift
prime_pairs = [[2, 2]]
return prime_pairs += primes.repeated_permutation(2).to_a
end
def prime_pair_permutes_count(n)
# All permutations of n primes
primes = list_primes_count(n)
primes.shift
prime_pairs = [[2, 2]]
return prime_pairs += primes.repeated_permutation(2).to_a
end
def make_goldbach_hash_sum(prime_lists)
goldbach_hash = Hash.new {|h,k| h[k]=[]}
prime_lists.each do |prime_list|
sum = prime_list.inject(:+)
goldbach_hash[sum] << prime_list
end
return goldbach_hash
end
def make_goldbach_hash_avg(prime_lists)
goldbach_hash = Hash.new {|h,k| h[k]=[]}
prime_lists.each do |prime_list|
sum = prime_list.inject(:+)
avg = sum / prime_list.length
goldbach_hash[avg] << prime_list
end
return goldbach_hash
end
def report_goldbach_hash(goldbach_hash)
# generate a basic report
report = ""
goldbach_hash.sort.map do |number, prime_combos|
report << "#{number}, "
prime_combos.each do |primes|
report << "#{primes}, "
end
report << "\n"
end
return report
end
def report_sums_table(n)
ps = list_primes_count(n)
ps.shift
rows = ""
ps.map do |p|
ps.map do |q|
rows << "#{p + q}, "
end
rows << "\n"
end
return rows
end
def report_avgs_table(n)
ps = list_primes_count(n)
ps.shift
rows = ""
ps.map do |p|
ps.map do |q|
rows << "#{(p + q) / 2}, "
end
rows << "\n"
end
return rows
end
# TODO: make some fancy reports. See examples below.
# Columnated Goldbach pairs
# 4, [ 2, 2],
# 6,, [ 3, 3],
# 8,, [ 3, 5], [ 5, 3],
# 10,, [ 3, 7], [ 5, 5], [ 7, 3],
# 12,, , [ 5, 7], [ 7, 5],
# 14,, [ 3, 11], , [ 7, 7], [11, 3],
# 16,, [ 3, 13], [ 5, 11], , [11, 5], [13, 3],
# 18,, , [ 5, 13], [ 7, 11], [11, 7], [13, 5],
# 20,, [ 3, 17], , [ 7, 13], , [13, 7], [17, 3],
# 22,, [ 3, 19], [ 5, 17], , [11, 11], , [17, 5], [19, 3],
# 24,, [ 5, 19], [ 7, 17], [11, 13], [13, 11], [17, 7], [19, 5],
# 26,, [ 3, 23], , [ 7, 19], , [13, 13], , [19, 7], [23, 3],
# 28,, , [ 5, 23], , [11, 17], , [17, 11], , [23, 5],
# 30,, , , [ 7, 23], [11, 19], [13, 17], [17, 13], [19, 11], [23, 7],
# 32,, [ 3, 29], , , , [13, 19], , [19, 13], , [29, 3],
# 34,, [ 3, 31], [ 5, 29], , [11, 23], , [17, 17], , [23, 11], [29, 5], [31, 3],
# 36,, , [ 5, 31], [ 7, 29], , [13, 23], [17, 19], [19, 17], [23, 13], [29, 7], [31, 5],
# 38,, , , [ 7, 31], , , , [19, 19], , , [31, 7],
# 40,, [ 3, 37], , , [11, 29], , [17, 23], , [23, 17], [29, 11], , [37, 3],
# 42,, , [ 5, 37], , [11, 31], [13, 29], , [19, 23], [23, 19], [29, 13], [31, 11], [37, 5],
# 44,, [ 3, 41], , [ 7, 37], , [13, 31], , , , , [31, 13], [37, 7], [41, 3],
# 46,, [ 3, 43], [ 5, 41], , , , [17, 29], , [23, 23], [29, 17], , , [41, 5], [43, 3],
# 48,, , [ 5, 43], [ 7, 41], [11, 37], , [17, 31], [19, 29], , [29, 19], [31, 17], [37, 11], [41, 7], [43, 5],
# 50,, [ 3, 47], , [ 7, 43], , [13, 37], , [19, 31], , , [31, 19], [37, 13], , [43, 7], [47, 3],
# ...
# Columnated avgs table
# 2,
# 2, 2,
# , 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, ...
# 3, 3, 4, 5, 7, 8, 10, 11, 13, 16, 17, 20,
# 5, 4, 5, 6, 8 , 9, 11, 12, 14, 17, 18, 21,
# 7, 5, 6, 7, 9, 10, 12, 13, 15, 18, 19, 22,
# 11, 7, 8, 9, 11, 12, 14, 15, 17, 20, 21, 24,
# 13, 8, 9, 10, 12, 13, 15, 16, 18, 21, 22, 25,
# 17, 10, 11, 12, 14, 15, 17, 18, 20, 23, 24, 27,
# 19, 11, 12, 13, 15, 16, 18, 19, 21, 24, 25, 28,
# 23, 13, 14, 15, 17, 18, 20, 21, 23, 26, 27, 30,
# 29, 16, 17, 18, 20, 21, 23, 24, 26, 29, 30, 33,
# 31, 17, 18, 19, 21, 22, 24, 25, 27, 30, 31, 34,
# 37, 20, 21, 22, 24, 25, 27, 28, 30, 33, 34, 37,
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment