Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. require 'active_support/core_ext/enumerable'
  2. require 'pry'
  3. class ProjectEuler
  4.  
  5. attr_accessor :hash, :array
  6.  
  7. def initialize( args = {} )
  8. self.hash = {}
  9. self.array = []
  10. end
  11.  
  12. def three_and_five
  13. # If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
  14. # Find the sum of all the multiples of 3 or 5 below 1000.
  15. ( 1..999 ).each do |n|
  16. next unless n % 3 == 0 || n % 5 == 0
  17. array.push n
  18. end
  19. array.sum
  20. end
  21.  
  22. def even_fibonacci_numbers
  23. # Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
  24. # 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  25. # By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  26. array = [ 1, 2 ]
  27. past = 1
  28. present = 2
  29. loop_through = true
  30. while loop_through do
  31. array.push( past + present )
  32. past = present
  33. present = array.last
  34. loop_through = false if ( past + present ) > 4000000
  35. end
  36. array.select{ |n| n % 2 == 0 }.sum
  37. end
  38.  
  39. def largest_prime_factor
  40. # The prime factors of 13195 are 5, 7, 13 and 29.
  41. # What is the largest prime factor of the number 600851475143 ?
  42. check_again = lambda{ |n| }
  43. number = 600851475143
  44. hash = {
  45. number => { }
  46. }
  47. (2..( number / 2 ) ).each do |n|
  48. hash[ number ][ number / n ] = {} if number % n == 0
  49. end
  50. end
  51.  
  52.  
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement