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...1000).to_a.select {|n| n.remainder(3) == 0 || n.remainder(5) == 0}.reduce(:+) |
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
| fib = [1, 2] | |
| def fib.next | |
| self[-1] + self[-2] | |
| end | |
| fib << fib.next while fib.next < 4_000_000 | |
| fib.select {|f| f.remainder(2) == 0}.reduce(:+) |
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
| require 'mathn' | |
| 600851475143.prime_division.last.first |
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 palindrome?(integer) | |
| string = integer.to_s | |
| string == string.reverse | |
| end | |
| products = [] | |
| 999.step(100, -1).to_a.each do |a| | |
| 999.step(100, -1).to_a.each do |b| | |
| product = a*b |
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..20).to_a.reduce :lcm |
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..100).to_a.reduce(:+)**2 - (1..100).to_a.reduce {|sum_of_squares, n| sum_of_squares + 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
| require 'mathn' | |
| prime = Prime.new | |
| 10_000.times {prime.succ} | |
| prime.succ |
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
| numbers = <<EOS | |
| 73167176531330624919225119674426574742355349194934 | |
| 96983520312774506326239578318016984801869478851843 | |
| 85861560789112949495459501737958331952853208805511 | |
| 12540698747158523863050715693290963295227443043557 | |
| 66896648950445244523161731856403098711121722383113 | |
| 62229893423380308135336276614282806444486645238749 | |
| 30358907296290491560440772390713810515859307960866 | |
| 70172427121883998797908792274921901699720888093776 | |
| 65727333001053367881220235421809751254540594752243 |
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 pythagorean_triple(m, n) | |
| m, n = n, m if m > n | |
| [n**2 - m**2, 2*m*n, n**2 + m**2] | |
| end | |
| infinity = 1.0/0.0 | |
| catch :answer_found do | |
| (1..infinity).each do |m| | |
| (m+1..infinity).each do |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
| require 'mathn' | |
| Prime.new.inject(0) { |sum, n| | |
| break(sum) if n >= 2_000_000 | |
| sum + n | |
| } |
OlderNewer