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 / euler16.rb
Created May 27, 2011 07:28
Euler Project Problem #016
# Solution to Project Euler's sixteenth problem
# http://projecteuler.net/index.php?section=problems&id=16
# Find the sum of all the digits in the decimal representation of 2^1000.
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 sum_array(array)
@johana-star
johana-star / euler20.rb
Created May 27, 2011 07:47
Euler Project Problem #020
# Solution to Project Euler's twentieth problem
# http://projecteuler.net/index.php?section=problems&id=20
# Find the sum of all the digits in 100 factorial.
def factorial(number)
product = 1
(1..number).each {|n| product = product * n}
return product
end
def generate_array(number)
@johana-star
johana-star / euler25.rb
Created May 27, 2011 17:06
Euler Project Problem #025
# Solution to Project Euler's twenty-fifth problem
# http://projecteuler.net/index.php?section=problems&id=10
# Find the term of the first thousand-digit Fibonacci number.
i1, i2, count = 1, 1, 2
until i1.to_s.length == 1000 or i2.to_s.length == 1000
i1 = i1 + i2
i2 = i2 + i1
count += 2
end
if i1.to_s.length == 1000 then puts "#{i1} whose term is #{count-1}"
@johana-star
johana-star / euler13.rb
Created May 27, 2011 17:38
Euler Project Problem #013
# Solution to Project Euler's thirteenth problem
# http://projecteuler.net/index.php?section=problems&id=13
# Find the first ten digits of the sum of one hundred fifty-digit numbers.
numbers = [37107287533902102798797998220837590246510135740250, 46376937677490009712648124896970078050417018260538, 74324986199524741059474233309513058123726617309629, 91942213363574161572522430563301811072406154908250, 23067588207539346171171980310421047513778063246676, 89261670696623633820136378418383684178734361726757, 28112879812849979408065481931592621691275889832738, 44274228917432520321923589422876796487670272189318, 47451445736001306439091167216856844588711603153276, 70386486105843025439939619828917593665686757934951, 62176457141856560629502157223196586755079324193331, 64906352462741904929101432445813822663347944758178, 92575867718337217661963751590579239728245598838407, 58203565325359399008402633568948830189458628227828, 80181199384826282014278194139940567587151170094390, 35398664372827112653829987240784473053190104293586
@johana-star
johana-star / euler14.rb
Created May 27, 2011 21:30
Euler Project Problem #014
# Solution to Project Euler's fourteenth problem
# http://projecteuler.net/index.php?section=problems&id=14
# Find the longest Collatz conjecture chain starting with a number under one million.
def find_lengths(ceiling=999999)
numbers = (1..ceiling).to_a
terms = Array.new(ceiling, 1)
numbers.each_with_index do |n, i|
if n%16==15 or n%16==7 or n%16==11 or n%16==9 or n%16==1 then #to improve the search speed, filter out all hexadecimal numbers not ending in 15, 7, 11, 9, or 1, in that order for all numbers % 16 greater than 64, the remainder is either 15, 7, 11, 9, or 1, with a greater frequency on the fifteens and then the sevens.
until n == 1
if n.even? then
@johana-star
johana-star / euler48.rb
Created May 27, 2011 21:56
Euler Project Problem #048
# Solution to Project Euler's forty-eighth problem
# http://projecteuler.net/index.php?section=problems&id=48
# Find the last ten digits of the sum of the sequence 1^1, 2^2, 3^3 ... 1000^1000.
def generate_array(number=1000)
array = (1..number).to_a
array.each_with_index do |exp, i|
array[i] = exp**exp
end
return array
end
@johana-star
johana-star / euler12.rb
Created May 27, 2011 22:58
Euler Project Problem #012
# Solution to Project Euler's twelfth problem
# http://projecteuler.net/index.php?section=problems&id=12
# Find the first triangular number with more than 500 divisors.
def generate_triangle_numbers(ceiling=15000)
numbers = Array.new
sum = 0
(1..ceiling).each do |n|
sum = sum + n
if Math.sqrt(sum) > 8000 then numbers.push(sum) end
end
@johana-star
johana-star / euler28.rb
Created May 28, 2011 19:25
Euler Project Problem #028
# Solution to Project Euler's twenty-eighth problem
# http://projecteuler.net/index.php?section=problems&id=28
# Find the sum of the diagonals of a 1001 spiral grid.
def spiral_sum(number)
sum, n = 0, 0
(0..number).to_a.each do |x|
n = 2*x + n
sum = sum + n + 1
end
n = 0
@johana-star
johana-star / euler30.rb
Created May 29, 2011 05:42
Euler Project Problem #030
# Solution to Project Euler's thirtieth problem
# http://projecteuler.net/index.php?section=problems&id=30
# Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
def seperate_digits(numbers)
array = []
numbers.each_with_index do |n, i|
array[i] = []
until n < 10 do
array[i].unshift (n % 10)
n = n / 10
@johana-star
johana-star / euler29.rb
Created May 29, 2011 06:28
Euler Project Problem #029
# Solution to Project Euler's twenty-ninth problem
# http://projecteuler.net/index.php?section=problems&id=29
# Find the sum of all the numbers that can be written as the sum of fifth powers of
def generate_exponential_sequence(number)
array = []
(2..number).each do |n|
(2..number).each do |exp|
array.push n**exp
end
end