Skip to content

Instantly share code, notes, and snippets.

@eqdw
Created October 9, 2010 19:25
Show Gist options
  • Save eqdw/618516 to your computer and use it in GitHub Desktop.
Save eqdw/618516 to your computer and use it in GitHub Desktop.
require 'mathn'
@min = 227000
@primes = [2,3,5,7,11]
@fibs = [1,1,2,3,5,8,13]
#fills @fibs until @fibs[-1] contains the first pribe fib greater than @min
def gen_fibs
i = 0
#reach/pass min
fib_next = 21
while fib_next < @min
@fibs << fib_next
fib_next = @fibs[-1] + @fibs[-2]
i += 1
i %= 10
if i == 0
puts fib_next
end
end
puts "finished part one"
#keep going until you find a prime
until fib_next.prime?
@fibs << fib_next
fib_next = @fibs[-1] + @fibs[-2]
i += 1
i %= 10
if i == 0
puts fib_next
end
end
@fibs << fib_next
end
def next_prime
start = @primes[-1]+1
while(42)
prime = true
@primes.each do |i|
if start % i == 0
prime = false
end
end
if prime
@primes << start
break
end
start+= 1
end
end
def prime_factorization n
@factors = []
index = 0
while true
if index >= @primes.length
next_prime
end
if @primes[index] > n
break
end
if( n % @primes[index] == 0)
n = n / @primes[index]
# @factors << @primes[index] unless @factors.include? @primes[index]
if @factors.include? @primes[index]
@factors[-1][1] += 1
else
@factors << [@primes[index], 1];
end
else
index+=1
end
end
@factors
end
#after writing these helpers I walked through the problem in IRB.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment