-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplayground_5_euler1.rb
More file actions
130 lines (95 loc) · 3.17 KB
/
playground_5_euler1.rb
File metadata and controls
130 lines (95 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 1) Go to http://projecteuler.net/ and sign up for an account
# General strategies
# 1) Try to simplify the problem
# - break the problem up into smaller chunks
# - substitute small number for really big numbers
# - only go through one iteration of a loop
# - try hardcoding variables first, then making them dynamic
# 2) Verbally describe what you want your program to do
# 3) Brainstorm tools that might be applicable to your problem
# 4) Write some pseudo code for the simplest possible case
# 5) Translate the pseudo code into Ruby
# 6) Gradually add complexity until you've solved the problem
# PROBLEM 1
##############################
# 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.
# Find the sum of all the multiples of 3 or 5 below 1000.
# Tools
# - Use modulus to check for divisibility
# ex) 5%3 should be 2
# ex) 6%3 should be 0
# - Use find_all
# Strategies
# - If there is a remainder when using a modulus check, stick the number into an array
# - Use find_all with modulus in the condition
# Dan Park's solution
# sum = 0
# a = (1...1000).find_all { |i| i % 3 == 0 || i % 5 == 0 }
# puts a.each { |x| sum += x }
# puts sum
# Travis Loncar's solution
# sum = 0
# 1000.times { |num| num % 3 == 0 || num % 5 == 0 ? (sum += num) : () }
# puts sum
# PROBLEM 2
##############################
# 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:
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
# By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
# One approach
# start with an empty array
# have the first two elements be the first two elements in the sequence
# find the sum and add it on as the third element
# add one to the index that you're pulling out
# Another approach
# use a while loop
# store three numbers, for the three places we are using
# in the while loop, check if the current number is even
# if so, add the number to the sum
# Jacob Hart's Solution
# # First round Second round
# number_a = 0 # 1 # 1
# number_b = 1 # 1 # 2
# sum = 0
# while number_b < 4_000_000 do
# if number_b.even?
# sum += number_b
# end
# # First Second Third
# number_c = number_a # c: 0 # c: 1 # c: 1
# number_a = number_b # a: 1 # a: 1 # a: 2
# number_b += number_c # b: 1 # b: 2 # b: 3
# end
# puts sum #=> 4613732
# Jacob's Solution with renamed variables
#
# two_before = 0
# one_before = 1
# current_num = two_before + one_before
# sum = 0
# while current_num < 4_000_000 do
# if current_num.even?
# sum += current_num
# end
# next_num = one_before + current_num
# one_before = current_num
# current_num = next_num
# end
# puts sum #=> 4613732
# Eric Chesbrough's solution
#
# array = []
# a1 = 0
# a2 = 1
# sum = 0
# while true
# curr_term = a1 + a2
# if (4_000_000 <= curr_term)
# break
# end
# array += [curr_term]
# a1 = a2
# a2 = curr_term
# end
# array.find_all { |num| num % 2 == 0 ? (sum += num) : () }
# puts sum