-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_methods2.rb
executable file
·160 lines (109 loc) · 2.04 KB
/
test_methods2.rb
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Practice various other methods, mathematical whatnot on 3/3/2013
def is_prime(number_in_question)
# Prime numbers, as defined on Wikipedia:
# 1. A number greater than 1
# 2. Has NO positive divisors other than 1 and itself
if number_in_question <= 1
puts "InputError: Number cannot be 1 or less."
else
i = 2
other_divisors = []
while i < number_in_question
if ((number_in_question % i)) == 0
other_divisors << i
end
i += 1
end
if other_divisors.size == 0
puts "#{number_in_question} is a prime number"
else
puts "#{number_in_question} is not a prime number; other divisors include:"
puts other_divisors.join(', ')
end
end
end
puts is_prime(1)
puts is_prime(2)
puts is_prime(3)
puts is_prime(4)
puts is_prime(5)
puts is_prime(6)
puts is_prime(7)
puts is_prime(8)
puts is_prime(9)
puts is_prime(10)
puts
def pow(base, exponent)
result = 1
i = 1
while i <= exponent
result *= base
i += 1
end
result
end
puts pow(2,2)
puts pow(3,3)
puts pow(4,4)
puts pow(5,5)
puts pow(6,6)
puts
def sum(this_array)
sum = 0
this_array.each do |this_number|
sum += this_number
end
sum
end
p sum([1,2,3]) # 6
p sum([4,5,6,7]) # 22
p sum([8,2,5,5,15]) # 35
puts
# 10m
def silly_sum(this_array)
i = 0
total = 0
this_array.each do |this_num|
total += (this_num * i)
i += 1
end
return total
end
puts silly_sum([2, 3]) # 3
puts silly_sum([2, 3, 4]) # 11
puts
# 15m
def num_squares(max_num)
i = 1
squares = []
until i == max_num
if ((i * i) < max_num)
squares << (i * i)
end
i += 1
end
return squares
end
puts num_squares(5).join(', ')
puts num_squares(10).join(', ')
puts num_squares(25).join(', ')
puts num_squares(50).join(', ')
puts
# 7m
def silly_nums(max_num)
i = 0
list_of_valid_nums = []
until i == max_num
if (i % 3 == 0) || (i % 5 == 0)
if (i % 3 == 0) && (i % 5 == 0)
else
list_of_valid_nums << i
end
end
i += 1
end
list_of_valid_nums
end
puts silly_nums(3).join(', ')
puts silly_nums(10).join(', ')
puts silly_nums(20).join(', ')