Created
July 10, 2012 21:57
-
-
Save pelf/3086490 to your computer and use it in GitHub Desktop.
Project Euler in Ruby
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
puts (1...1000).inject(0) {|sum,i| | |
sum = (sum + i) if i%5 == 0 or i%3 == 0 | |
sum | |
} |
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
# Find the sum of all the primes below two million. | |
max = 2_000_000 | |
# # 3rd approach: optimized sieve | |
@@primes = Array.new(max,true) | |
# cross out even nrs | |
4.step(max,2) do |n| | |
@@primes[n] = false | |
end | |
# now we start at 3 | |
sum = 2 | |
3.step(max,2) do |n| | |
if @@primes[n] | |
sum += n | |
# sieve n mults! | |
# You only need to start crossing out multiples at p^2 , because any smaller multiple of p has a prime divisor less than p and has already been crossed out as a multiple of that | |
(n*n).step(max, 2*n) do |s| # step 2*n because even products are already out | |
@@primes[s] = false | |
end | |
end | |
end | |
puts sum | |
# 2nd approach: sieve. faster for big limits | |
# @@non_primes = [] | |
# sum = 0 | |
# | |
# (2..max).each do |n| | |
# unless @@non_primes[n] | |
# sum += n | |
# # sieve n mults! | |
# (n*2).step(max,n) do |s| | |
# @@non_primes[s] = true | |
# end | |
# end | |
# end | |
# | |
# puts sum | |
# 1st approach: trial division. not too bad with the sqrt(n) limit | |
# @@primes = [] | |
# | |
# def prime?(n) | |
# sqrt = Math.sqrt(n).to_i | |
# # if n isnt prime, it MUST divide by one of the lower primes | |
# @@primes.each do |p| | |
# break if p > sqrt # improvement: we may stop at sqrt(n) | |
# return false if n%p == 0 | |
# end | |
# # if it doesnt, it's a new prime | |
# @@primes << n | |
# return true | |
# end | |
# | |
# # we're skiping the 2 so we can skip even numbers in the cycle | |
# sum = 2 | |
# @@primes << 2 | |
# | |
# 3.step(max,2) do |n| | |
# sum += n if prime?(n) | |
# end | |
# | |
# puts sum |
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
pp = 0 | |
p = 1 | |
c = 1 | |
sum = 0 | |
loop do | |
# current value | |
c = p + pp | |
# too big? | |
break if c > 4000000 | |
# sum when due | |
sum += c if c%2 == 0 | |
# shift values | |
pp = p | |
p = c | |
end | |
puts sum |
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
n = 600851475143 | |
(2..n).each do |f| | |
break if f>n # done! - n is updated inside the cycle, this will happen | |
if n%f == 0 | |
puts f | |
n = n/f | |
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
def pal?(n) | |
s = n.to_s | |
(0..s.size/2).each do |c| | |
return false if s[c] != s[s.size-c-1] | |
end | |
return true | |
end | |
largest = 0 | |
999.downto(100) do |i| | |
999.downto(100) do |j| | |
p = i*j | |
break if p < largest | |
if pal?(p) | |
largest = p | |
end | |
end | |
end | |
puts largest |
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
puts (1..20).inject(1) {|t,n| t.lcm n} |
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
# euler - problem 6: | |
# | |
# The sum of the squares of the first ten natural numbers is, | |
# | |
# 1^2 + 2^2 + ... + 10^2 = 385 | |
# The square of the sum of the first ten natural numbers is, | |
# | |
# (1 + 2 + ... + 10)^2 = 55^2 = 3025 | |
# Hence the difference between the sum of the squares of the first ten natural numbers and the # square of the sum is 3025 385 = 2640. | |
# | |
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. | |
# brute force. is there another way? | |
sum = 0 | |
sq_sum = 0 | |
1.upto(100) do |n| | |
sum += n | |
sq_sum += n**2 | |
end | |
puts sum**2 - sq_sum | |
# yes there is. from the forums: | |
# The sum of squares is n*(n +1)*(2*n +1) /12 | |
# The square of sum of n numbers is ( n*(n+1)/2 ) * ( n *(n +1)/2 ) | |
# If we take the diffrence and solve it algebraically | |
# We get the diffrence to be - | |
# ( 3 * n^4 + 2 * n^3 -3 * n^2 - 2 * n )/12 | |
# so answer is the value of above expression | |
# To reduce number of multiplication operations store value of n^2 . | |
# |
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
@@primes = [] | |
def prime?(n) | |
# if n isnt prime, it MUST divide by one of the lower primes | |
@@primes.each do |p| | |
return false if n%p == 0 | |
end | |
# if it doesnt, it's a new prime | |
@@primes << n | |
return true | |
end | |
nth = 10001 | |
count = 0 | |
n = 2 | |
loop do | |
count += 1 if prime?(n) | |
break if count == nth | |
n += 1 | |
end | |
puts n |
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
n1k = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" | |
max = 0 | |
(0..n1k.size-5).each do |index| | |
prod = n1k[index,5].chars.inject(1) {|s,e| s*e.to_i} | |
max = prod if prod > max | |
end | |
puts max |
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
1.upto(1000) do |a| | |
1.upto(1000) do |b| | |
csq = a**2 + b**2 | |
c = Math.sqrt(csq) | |
next if c%1 != 0 # not an int | |
puts "#{a} #{b} #{c} #{a*b*c}" if a+b+c == 1000 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment