Skip to content

Instantly share code, notes, and snippets.

@bilalq
Created September 16, 2012 16:18
Show Gist options
  • Save bilalq/3733042 to your computer and use it in GitHub Desktop.
Save bilalq/3733042 to your computer and use it in GitHub Desktop.
Fibonacci Primes
#!/usr/bin/env ruby
$path="/ilab/users/bilalq/.scripts/primeList.txt"
class Prime
def initialize
@primes=File.open($path).collect do |num|
num.to_i
end
@count = @primes.size
end
def prime? n
result = true
limit = Math.sqrt(n)
@primes.each do |curr|
if (n%curr == 0)
result = false
break
end
if (curr > limit)
result = true
break
end
end
result
end
def nextp
i = @primes.last + 2
until (prime? i)
i+=2
end
@primes.push(i)
end
def nthPrime n
unless (n<1)
until (@primes.length >= n)
nextp
end
@primes[n-1]
end
end
def getSub
if (@count == @primes.size)
-1
else
subPrimes = @primes[@count..@primes.size-1]
subPrimes
end
end
end
#if (ARGV[0] == nil)
#$path+="primeList.txt"
#test = Prime.new
#while (true)
#puts "Enter a number and get back nth prime:"
#num = gets.to_i
#break unless num > 0
#answer = test.nthPrime(num);
#puts answer
#end
#exit
#elsif (ARGV[0].to_s == "-h")
#puts "nth Prime Calculator"
#puts "To use, enter a single value as an argument."
#puts "Alternatively, you can run with no arguments for extended use."
#exit
#else
#$path += ( ARGV[0].to_i >= 1000000 ? "primeLong.txt" : "primeList.txt" )
#test = Prime.new
#answer = test.nthPrime(ARGV[0].to_i)
#puts answer
#end
#extras = test.getSub
#unless (extras == -1)
#f2 = File.open($path,"a")
#extras.each do |curr|
#f2.puts curr.to_s
#end
#end
#
test = Prime.new
fibPrimeIndex = 1
lastFib = 1
currFib = 2
until fibPrimeIndex == 12
temp = currFib
currFib += lastFib
lastFib = temp
if test.prime?(currFib)
puts currFib
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment