Created
December 13, 2013 23:36
-
-
Save CharlieTruong/7953513 to your computer and use it in GitHub Desktop.
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
def next_prime(number) | |
prime=false | |
while prime==false | |
number+=1 | |
for i in 2..number | |
prime=true if i==number | |
break if number % i ==0 | |
end | |
end | |
return number | |
end | |
def is_prime?(number) | |
prime=false | |
for i in 2..number | |
prime=true if i==number | |
break if number % i ==0 | |
end | |
return prime | |
end | |
def consecutive_prime | |
start_prime=2 | |
max_prime_count=Array.new(3,0) | |
max_sum_prime=Array.new(3,0) | |
while max_prime_count.last>=max_prime_count[max_prime_count.length-3] || max_prime_count[max_prime_count.length-2]>=max_prime_count[max_prime_count.length-3] | |
prime_sums_only=Array.new | |
prime_count_array=Array.new | |
prime=start_prime | |
sum_prime=0 | |
prime_count=0 | |
while sum_prime+prime<1_000_000 | |
sum_prime+=prime | |
prime_count+=1 | |
prime_sums_only.push(sum_prime) if is_prime?(sum_prime) | |
prime_count_array.push(prime_count) if is_prime?(sum_prime) | |
prime=next_prime(prime) | |
end | |
max_sum_prime.push(prime_sums_only.max) | |
max_prime_count.push(prime_count_array.max) | |
puts max_prime_count.last | |
start_prime=next_prime(start_prime) | |
end | |
return max_sum_prime[max_sum_prime.length-3], max_prime_count[max_prime_count.length-3] | |
end | |
p consecutive_prime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment