This file contains hidden or 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 evaluate_for_primes(primes, count) | |
numbers = (primes[count-1]**2+1..primes[count]**2).to_a | |
1.upto(count) {|c| numbers.select! {|number| number%primes[c] != 0}} | |
primes.concat(numbers) | |
end | |
primes, count = [1, 2], 1 | |
until primes.length > 10001 do | |
evaluate_for_primes(primes, count) | |
count += 1 |
This file contains hidden or 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
# Solution to Project Euler's first problem | |
# http://projecteuler.net/index.php?section=problems&id=1 | |
# Sum all numbers divisible by 3 or 5 below 1000. | |
sum = 0 | |
1.upto(999) do |n| | |
if (n % 3 == 0 || n % 5 == 0) then | |
sum = sum + n | |
end | |
end |
This file contains hidden or 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
# Solution to Project Euler's second problem | |
# http://projecteuler.net/index.php?section=problems&id=2 | |
# Sum all even numbers in the Fibonnacci sequence below 4000000. | |
i1, i2, sum = 1, 1, 0 | |
until i1 > 4000000 | |
if i1.even? then sum = sum + i1 end | |
i1 = i1 + i2 | |
if i2.even? then sum = sum + i2 end | |
i2 = i2 + i1 | |
end |
This file contains hidden or 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
# Solution to Project Euler's third problem | |
# http://projecteuler.net/index.php?section=problems&id=3 | |
# What is the largest prime factor of the number 600851475143? | |
def generate_primes(ceiling) | |
primes, count = [1, 2], 1 | |
until primes.last > Math.sqrt(ceiling) do | |
numbers = (primes[count-1]**2+1..primes[count]**2).to_a | |
1.upto(count) {|c| numbers.select! {|number| number%primes[c] != 0}} | |
primes.concat(numbers) | |
count += 1 |
This file contains hidden or 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
# Solution to Project Euler's fourth problem | |
# http://projecteuler.net/index.php?section=problems&id=4 | |
# Find the largest palindromic number which is the product of two three-digit numbers. | |
def generate_and_test | |
numbers = [] | |
999.downto(100) do |x| | |
999.downto(100) do |y| | |
number = x * y | |
if number.to_s == number.to_s.reverse then | |
numbers.push number |
This file contains hidden or 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
# Solution to Project Euler's fifth problem | |
# http://projecteuler.net/index.php?section=problems&id=5 | |
# Find the smallest number divisible with no remainder by each number below twenty. | |
def generate_primes(ceiling) | |
primes, count = [1, 2], 1 | |
until primes.last > ceiling do | |
numbers = (primes[count-1]**2+1..primes[count]**2).to_a | |
1.upto(count) {|c| numbers.select! {|number| number%primes[c] != 0}} | |
primes.concat(numbers) | |
count += 1 |
This file contains hidden or 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
# Solution to Project Euler's sixth problem | |
# http://projecteuler.net/index.php?section=problems&id=6 | |
# Find the difference between the square of the sum of the first hundred natural numbers and the sum of their squares. | |
def sum_squares(ceiling) | |
sum = 0 | |
(1..ceiling).each {|n| sum = sum +n**2} | |
return sum | |
end | |
def square_sums(ceiling) | |
sum = 0 |
This file contains hidden or 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
# Solution to Project Euler's eighth problem | |
# http://projecteuler.net/index.php?section=problems&id=8 | |
# Find the largest product of five consecutive digits in a thousand-digit number. | |
def generate_array(number) | |
number = number.to_s | |
array = [] | |
(0..number.length-1).each {|n| array.push number.slice(n).to_i} | |
return array | |
end | |
def find_products(array) |
This file contains hidden or 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
# Solution to Project Euler's ninth problem | |
# http://projecteuler.net/index.php?section=problems&id=9 | |
# Find the product of the Pythagorean triplets whose sum equals 1000. | |
def find_Pythagorean_triplet(number) | |
squares = (1..number).to_a.reverse | |
squares = squares.each_index {|s| squares[s] = squares[s]**2} | |
p squares | |
squares.each do |c| | |
squares.each do |b| | |
squares.each do |a| |
This file contains hidden or 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
# Solution to Project Euler's tenth problem | |
# http://projecteuler.net/index.php?section=problems&id=10 | |
# Find the sum of all primes below two million. | |
def generate_primes(ceiling) | |
primes, count = [1, 2], 1 | |
until primes.last > ceiling do | |
numbers = (primes[count-1]**2+1..primes[count]**2).to_a | |
1.upto(count) {|c| numbers.select! {|number| number%primes[c] != 0}} | |
primes.concat(numbers) | |
count += 1 |
OlderNewer