Created
September 16, 2012 16:18
-
-
Save bilalq/3733042 to your computer and use it in GitHub Desktop.
Fibonacci Primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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[@[email protected]] | |
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