├── README.md ├── 1.rb ├── 6.rb ├── 2.rb ├── 4.rb ├── 5.rb └── 8.rb /README.md: -------------------------------------------------------------------------------- 1 | Project Euler Solutions 2 | === 3 | 4 | Solutions to some of the problems at [Project Euler](http://projecteuler.net/). 5 | -------------------------------------------------------------------------------- /1.rb: -------------------------------------------------------------------------------- 1 | #Problem 1 2 | #Find sum of all multiples of 3 or 5 below 1000. 3 | 4 | sum = 0 5 | (1...1000).each do |x| 6 | if x % 3 == 0 || x % 5 == 0 7 | sum += x 8 | end 9 | end 10 | puts sum 11 | -------------------------------------------------------------------------------- /6.rb: -------------------------------------------------------------------------------- 1 | sumsquares = 0 2 | squaresums = 0 3 | 4 | (1..100).each do |x| 5 | sumsquares += x**2 6 | squaresums +=x 7 | end 8 | 9 | squaresums = squaresums ** 2 10 | 11 | puts (squaresums - sumsquares) 12 | -------------------------------------------------------------------------------- /2.rb: -------------------------------------------------------------------------------- 1 | # find sum of the even-valued terms of fibonacci terms up to 4,000,000 2 | # 1 , 2, 3, 5, 8, 13, 21, 34, 55, ... 3 | # 4 | 5 | 6 | def fib(sum, curr, prev) 7 | if curr > 4_000_000 8 | puts sum 9 | return 10 | end 11 | if curr % 2 == 0 12 | sum += curr 13 | end 14 | fib(sum, prev + curr, curr) 15 | end 16 | 17 | fib(0,1,1) 18 | 19 | -------------------------------------------------------------------------------- /4.rb: -------------------------------------------------------------------------------- 1 | #finds largest palindrome made 2 | #from two 3-digit numbers 3 | 4 | 5 | def palindrome?(x, y) 6 | return true if (x*y).to_s == (x*y).to_s.reverse 7 | return false 8 | end 9 | 10 | max = 0 11 | 100.upto(999) do |x| 12 | 100.upto(999) do |y| 13 | max = x * y if palindrome?(x,y) and x * y > max 14 | end 15 | end 16 | 17 | puts max 18 | -------------------------------------------------------------------------------- /5.rb: -------------------------------------------------------------------------------- 1 | #smallest positive int divisible 2 | #by all numbers 1..20 3 | 4 | def divisible?(x, lo, hi) 5 | (lo..hi).each do |n| 6 | return false if x % n != 0 7 | end 8 | return true 9 | end 10 | 11 | 12 | ret = 0 13 | curr = 19 14 | 15 | while ret == 0 do 16 | ret = curr if divisible?(curr, 1, 20) 17 | curr += 19 18 | end 19 | 20 | puts ret 21 | -------------------------------------------------------------------------------- /8.rb: -------------------------------------------------------------------------------- 1 | bignum = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" 2 | 3 | lo = 0 4 | hi = 4 5 | max = 0 6 | 7 | while hi < 999 do 8 | prod = 1 9 | (lo..hi).each do |x| 10 | prod = prod * bignum[x].to_i 11 | end 12 | max = prod if prod > max 13 | lo += 1 14 | hi += 1 15 | end 16 | 17 | puts max 18 | --------------------------------------------------------------------------------