Skip to content

Instantly share code, notes, and snippets.

View johana-star's full-sized avatar
🕶️

Johana Star johana-star

🕶️
View GitHub Profile
@johana-star
johana-star / primes.rb
Created May 22, 2011 21:10
Find the 10001st prime
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
@johana-star
johana-star / euler1.rb
Created May 26, 2011 05:27
Euler Project Problem #001
# 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
@johana-star
johana-star / euler2.rb
Created May 26, 2011 05:43
Euler Project Problem #002
# 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
@johana-star
johana-star / euler3.rb
Created May 26, 2011 07:22
Euler Project Problem #003
# 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
@johana-star
johana-star / euler4.rb
Created May 26, 2011 08:16
Euler Project Problem #004
# 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
@johana-star
johana-star / euler5.rb
Created May 26, 2011 09:55
Euler Project Problem #005
# 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
@johana-star
johana-star / euler6.rb
Created May 26, 2011 17:24
Euler Project Problem #006
# 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
@johana-star
johana-star / euler8.rb
Created May 26, 2011 18:12
Euler Project Problem #008
# 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)
@johana-star
johana-star / euler9.rb
Created May 27, 2011 06:33
Euler Project Problem #009
# 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|
@johana-star
johana-star / euler10.rb
Created May 27, 2011 07:15
Euler Project Problem #010
# 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