-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasics.rb
More file actions
54 lines (41 loc) · 1.35 KB
/
basics.rb
File metadata and controls
54 lines (41 loc) · 1.35 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
#----------------------------------------------------------------------------------------------------------------------------------
#
# Question 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.
def three_five(number)
sum = 0
threes = Array.new
fives = Array.new
for x in (1...number)
threes.push(x) if x%3 == 0
fives.push(x) if x%5 == 0
end
threes.each {|t| fives.push(t) if !fives.include?(t)}
fives.each {|f| sum += f}
puts sum
end
#three_five(1000)
#---------------------------------------------------------------------------------------------------------------------------------
# Question 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.
def Fibonacci_max()
s = [1,1]
puts s
x = s.last
even_sum = 0
while x < 4000000
c = s.count
n = s[c-2]+s[c-1]
s.push(n)
even_sum = even_sum + n if n%2 == 0
x = s.last
end
s.delete(s.last)
puts even_sum
end
Fibonacci_max()