This file contains 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
y = 0 | |
(1..999).each { |x| y = y + x if (x % 3 == 0) || (x % 5 == 0) } | |
puts y |
This file contains 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
# Create Fibonnaci Array | |
a = [1,2] | |
upto = 4_000_000 | |
while a[-2] + a[-1] < upto | |
a << a[-2] + a[-1] | |
end | |
# Create sum of even Fibonnaci numbers | |
sum = 0 | |
a.each { |x| sum+= x if x.even? } |
This file contains 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
# We make array of prime numbers with modulus 0 as long as product_sum < n | |
def prime? n | |
(2..(n-1)).each { |x| return false if n % x == 0 } | |
true | |
end | |
n = 600_851_475_143 | |
a = [] | |
product_sum = 1 |
This file contains 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? x | |
y = x.to_s | |
checks = y.length / 2 | |
y[0..(checks-1)] == y[-checks..-1].reverse | |
end | |
def divideable? x | |
999.downto(100).each { |y| return true if x % y == 0 && (x / y).to_s.length == 3 } | |
false | |
end |
This file contains 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
upto = 20 | |
def prime? x | |
(2..x-1).each { |y| return false if x % y == 0 } | |
true | |
end | |
# For speed reasons we want to find max possible increment | |
def increment upto | |
a = 1 |
This file contains 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 sum_of_squares upto | |
a = 0 | |
(1..upto).each { |x| a += x**2 } | |
a | |
end | |
def square_of_sum upto | |
a = 0 | |
(1..upto).each { |x| a += x } | |
a**2 |
This file contains 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 prime? x | |
(2..x-1).each { |y| return false if x % y == 0 } | |
true | |
end | |
x = 10_001 # We look for 10 001st prime number | |
n = 3 # Start at 3 so we can skip even numbers | |
counter = 1 # Counter at 1 because 2 is prime number | |
while true |
This file contains 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
# Ideone.com can't handle the full integer, our integer n is only the relevant line of the 1000 digit number. | |
# n = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588 |
This file contains 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
# Euclid's formula yields Pythagorean triples for integers m and n with m < n: | |
# a = m**2 - n**2 ; b = 2*m*n ; c = m**2 + n**2 | |
x = 1000 | |
def euclids upto | |
result = [] | |
(2..upto).each do |m| # Start at 2 as 1 results nothing anyway | |
(1...m).each do |n| # Euclid's formula only works for m > n | |
result << [m**2 - n**2, 2*m*n, m**2 + n**2] |
This file contains 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
x = 2_000_000 | |
def prime? x | |
(2...x).each { |i| return false if x % i == 0 } | |
true | |
end | |
def primes upto | |
a = [] | |
(2..upto).each { |i| a << i if prime? i ; puts i } |
OlderNewer