Class: Tasks::MathExercises
- Inherits:
-
Object
- Object
- Tasks::MathExercises
- Defined in:
- lib/tasks.rb
Instance Method Summary collapse
-
#conditions(number:) ⇒ Object
puts task - conditions.
-
#factorial(a) ⇒ Object
factorial is used to solve task235.
-
#gcd(a, b) ⇒ Object
gcd is used to solve task323.
-
#line(a, b) ⇒ Object
line is used to solve task25.
- #task1(a:, b:) ⇒ Object
- #task10(h:) ⇒ Object
- #task107(m:) ⇒ Object
- #task108(n:) ⇒ Object
- #task12(a:) ⇒ Object
- #task13(l:) ⇒ Object
- #task140(n: 100) ⇒ Object
- #task148(n: 100) ⇒ Object
- #task15(hypotenuse:, leg1:) ⇒ Object
- #task16(l:) ⇒ Object
- #task17(radius:) ⇒ Object
- #task182(n:) ⇒ Object
- #task2(x:, y:) ⇒ Object
- #task235(n:, m:) ⇒ Object
- #task24(x1:, y1:, x2:, y2:) ⇒ Object
- #task25(x1:, y1:, x2:, y2:, x3:, y3:) ⇒ Object
- #task251(text:) ⇒ Object
- #task28(x:) ⇒ Object
- #task3(a:) ⇒ Object
- #task323(n:) ⇒ Object
- #task327(a:, b:) ⇒ Object
- #task328(n: 100) ⇒ Object
- #task33(x:, y:) ⇒ Object
- #task35(x:, y:, z:) ⇒ Object
- #task36(a:, b:, c:) ⇒ Object
- #task39(a:, b:) ⇒ Object
- #task4(a:, b:) ⇒ Object
- #task40(a:, b:) ⇒ Object
- #task41(a:, b:, c:) ⇒ Object
- #task42(x:, y:) ⇒ Object
- #task43(a:, b:, c:) ⇒ Object
- #task5(a:, b:) ⇒ Object
- #task55(a:, b:, c:, d:) ⇒ Object
- #task6(a:, b:) ⇒ Object
- #task606(a:, b:, c:, d:) ⇒ Object
- #task62(a:) ⇒ Object
- #task621(a:, b:, c:, d:) ⇒ Object
- #task64(n:) ⇒ Object
- #task65(n:) ⇒ Object
- #task73(k:, l:) ⇒ Object
- #task75(n:) ⇒ Object
- #task77(n:) ⇒ Object
- #task8(corners:, radius:) ⇒ Object
- #task809(n:) ⇒ Object
- #task811(n:) ⇒ Object
- #task822(year:) ⇒ Object
- #task87(n:, m:) ⇒ Object
- #task9(r1:, r2:, r3:) ⇒ Object
- #task_37(a:, b:, c:) ⇒ Object
Instance Method Details
#conditions(number:) ⇒ Object
puts task - conditions
9 10 11 12 |
# File 'lib/tasks.rb', line 9 def conditions(number:) $number = number CONDITIONS[number] # Const from file conditions.rb end |
#factorial(a) ⇒ Object
factorial is used to solve task235
20 21 22 |
# File 'lib/tasks.rb', line 20 def factorial(a) (1..a).inject(:*) || 1 end |
#gcd(a, b) ⇒ Object
gcd is used to solve task323
25 26 27 28 29 30 31 |
# File 'lib/tasks.rb', line 25 def gcd(a, b) if (a % b).zero? b else gcd(b, a % b) end end |
#line(a, b) ⇒ Object
line is used to solve task25
15 16 17 |
# File 'lib/tasks.rb', line 15 def line(a, b) Math.sqrt((b[0] - a[0])**2 + (b[1] - a[1])**2) end |
#task1(a:, b:) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/tasks.rb', line 33 def task1(a:, b:) $a = a $b = b sum = a + b diff = a - b mult = a * b [sum, diff, mult] end |
#task10(h:) ⇒ Object
92 93 94 95 |
# File 'lib/tasks.rb', line 92 def task10(h: ) $h = h Math.sqrt(2 * (h / 9.8)) end |
#task107(m:) ⇒ Object
281 282 283 284 285 286 |
# File 'lib/tasks.rb', line 281 def task107(m: ) $m = m k = 0 k += 1 until (4 ** k) >= m k end |
#task108(n:) ⇒ Object
288 289 290 291 292 293 |
# File 'lib/tasks.rb', line 288 def task108(n: ) $n = n r = 0 r += 1 until (2**r) > n r end |
#task12(a:) ⇒ Object
97 98 99 100 |
# File 'lib/tasks.rb', line 97 def task12(a: ) $a = a 0.43301270189 * a**2 end |
#task13(l:) ⇒ Object
102 103 104 105 |
# File 'lib/tasks.rb', line 102 def task13(l: ) $l = l 2 * Math::PI * Math.sqrt(l / 9.8 ) end |
#task140(n: 100) ⇒ Object
295 296 297 298 299 300 |
# File 'lib/tasks.rb', line 295 def task140(n: 100) $n = n results = [] (1..n).each { |elem| results.push((3 * elem +4) / (elem**2 - (5 * elem) -9)) } results end |
#task148(n: 100) ⇒ Object
302 303 304 305 306 307 |
# File 'lib/tasks.rb', line 302 def task148(n: 100) $n = n temp_hash ={} (1..n).each { |c| temp_hash[c] = ((9 / 5) * c) + 32 } temp_hash end |
#task15(hypotenuse:, leg1:) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/tasks.rb', line 107 def task15(hypotenuse: , leg1: ) $hypotenuse = hypotenuse $leg1 = leg1 leg2 = Math.sqrt(hypotenuse**2 - leg1**2) radius = (leg1 + leg2 - hypotenuse) / 2 [leg2, radius] end |
#task16(l:) ⇒ Object
115 116 117 118 |
# File 'lib/tasks.rb', line 115 def task16(l: ) $l = l Math::PI * (l / (2 * Math::PI))**2 end |
#task17(radius:) ⇒ Object
120 121 122 123 |
# File 'lib/tasks.rb', line 120 def task17(radius: ) $radius = radius (Math::PI * radius**2) - (Math::PI * 20**2) end |
#task182(n:) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/tasks.rb', line 309 def task182(n: ) $n = n sum = 0 count = 0 (0..n).each do |elem| if (elem % 5) == 0 && (elem % 7) != 0 sum += elem count += 1 end end [sum, count] end |
#task2(x:, y:) ⇒ Object
42 43 44 45 46 |
# File 'lib/tasks.rb', line 42 def task2(x:, y:) $x = x $y = y (x - y).abs / (1 + (x * y).abs) end |
#task235(n:, m:) ⇒ Object
322 323 324 325 326 |
# File 'lib/tasks.rb', line 322 def task235(n: , m: ) $n = n $m = m (factorial(m) + factorial(n)) / factorial(m + n) end |
#task24(x1:, y1:, x2:, y2:) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/tasks.rb', line 125 def task24(x1: , y1: , x2: , y2: ) $x1 = x1 $y1 = y1 $x2 = x2 $y2 = y2 a = [x1, y1] b = [x2, y2] Math.sqrt((b[0] - a[0])**2 + (b[1] - a[1])**2) end |
#task25(x1:, y1:, x2:, y2:, x3:, y3:) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/tasks.rb', line 135 def task25(x1: , y1: , x2: , y2: , x3: , y3: ) $x1 = x1 $y1 = y1 $x2 = x2 $y2 = y2 $x3 = x3 $y3 = y3 a, b, c, = [x1, y1], [x2, y2], [x3, y3] perimeter = line(a, b) + line(b, c) + line(a, c) p = perimeter / 2 square = Math.sqrt(p * (p - line(a, b)) * (p - line(b, c)) * (p - line(a, c))) [perimeter, square] end |
#task251(text:) ⇒ Object
328 329 330 331 332 |
# File 'lib/tasks.rb', line 328 def task251(text: ) $text = text count = text.split('').map(&:to_s).select { |item| item.include?("x") } count.length end |
#task28(x:) ⇒ Object
149 150 151 152 |
# File 'lib/tasks.rb', line 149 def task28(x: ) $x = x x * (x * (x * (2 * x - 3) + 4) - 5) + 6 end |
#task3(a:) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/tasks.rb', line 48 def task3(a: ) $a = a volume = a**3 square = (a**2) * 4 [volume, square] end |
#task323(n:) ⇒ Object
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/tasks.rb', line 334 def task323(n: ) $n = n nums = [] (1..n).each do |x| if x < n gcd(n, x) == 1 ? nums.push(x) : nums end end nums end |
#task327(a:, b:) ⇒ Object
345 346 347 348 349 350 351 |
# File 'lib/tasks.rb', line 345 def task327(a: , b: ) $a = a $b = b nums = [] (a..b).each { |elm| Prime.prime?(elm) ? nums.push(elm) : nums } nums end |
#task328(n: 100) ⇒ Object
353 354 355 356 |
# File 'lib/tasks.rb', line 353 def task328(n: 100) $n = n Prime.first(n) end |
#task33(x:, y:) ⇒ Object
154 155 156 157 158 |
# File 'lib/tasks.rb', line 154 def task33(x: , y: ) $x = x $y = y [[x, y].max , [x, y].min] end |
#task35(x:, y:, z:) ⇒ Object
160 161 162 163 164 165 166 167 168 |
# File 'lib/tasks.rb', line 160 def task35(x: , y: , z: ) $x = x $y = y $z = z xyz = [x, y, z] part_a = [xyz.inject(0){ |res, elem| res + elem }, xyz.inject(1){ |res, elem| res * elem }].max part_b = (([((1..2).to_a.inject(0){ |res, elem| res + elem } + xyz[2] / 2), xyz.inject(1){ |res, elem| res * elem }].min)**2) + 1 [part_a, part_b] end |
#task36(a:, b:, c:) ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/tasks.rb', line 170 def task36(a: , b: , c: ) $a = a $b = b $c = c if b > a && c > b true else false end end |
#task39(a:, b:) ⇒ Object
197 198 199 200 201 |
# File 'lib/tasks.rb', line 197 def task39(a: , b: ) $a = a $b = b a > b ? [a] : [a, b] end |
#task4(a:, b:) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/tasks.rb', line 55 def task4(a: , b: ) $a = a $b = b average =(a + b) / 2 geometric = Math.sqrt(a * b) [average, geometric] end |
#task40(a:, b:) ⇒ Object
203 204 205 206 207 |
# File 'lib/tasks.rb', line 203 def task40(a: , b: ) $a = a $b = b a <= b ? [0, b] : [a, b] end |
#task41(a:, b:, c:) ⇒ Object
209 210 211 212 213 214 215 216 |
# File 'lib/tasks.rb', line 209 def task41(a: , b: , c: ) $a = a $b = b $c = c nums = [a, b, c] nums.each { |num| (1..3).include?(num) ? num : nums.delete(num) } nums end |
#task42(x:, y:) ⇒ Object
218 219 220 221 222 |
# File 'lib/tasks.rb', line 218 def task42(x: , y: ) $x = x $y = y x < y && x != y ? [(x + y) / 2, (x * y) * 2] : [(x * y) * 2 , (x + y) / 2] end |
#task43(a:, b:, c:) ⇒ Object
224 225 226 227 228 229 230 231 |
# File 'lib/tasks.rb', line 224 def task43(a: , b: , c: ) $a = a $b = b $c = c nums = [] [a, b, c].each { |elem| elem > 0 ? nums.push(elem**2) : nums.push(elem) } nums end |
#task5(a:, b:) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/tasks.rb', line 63 def task5(a: , b: ) $a = a $b = b average =(a + b) / 2 geometric = Math.sqrt((a * b).abs) [average, geometric] end |
#task55(a:, b:, c:, d:) ⇒ Object
233 234 235 236 237 238 239 240 241 |
# File 'lib/tasks.rb', line 233 def task55(a: , b: , c: , d: ) $a = a $b = b $c = c $d = d fig1 = [a, b] fig2 = [ c, d] fig1.max <= fig2.max && fig1.min <= fig2.min ? true : false end |
#task6(a:, b:) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/tasks.rb', line 71 def task6(a: , b: ) $a = a $b = b hypotenuse = Math.hypot(a, b) square = (a * b) / 2 [hypotenuse, square] end |
#task606(a:, b:, c:, d:) ⇒ Object
358 359 360 361 362 363 364 365 |
# File 'lib/tasks.rb', line 358 def task606(a: , b: , c: , d: ) $a = a $b = b $c = c $d = d arr = [a, b, c, d] arr.max >= (arr.delete_if{ |x| x == arr.max }.inject(0){ |res, elem| res + elem }) ? "Possible" : "Impossible" end |
#task62(a:) ⇒ Object
243 244 245 246 |
# File 'lib/tasks.rb', line 243 def task62(a: ) $a = a a.even? ? true : false end |
#task621(a:, b:, c:, d:) ⇒ Object
367 368 369 370 371 372 373 374 375 |
# File 'lib/tasks.rb', line 367 def task621(a: , b: , c: , d: ) $a = a $b = b $c = c $d = d fig1 = [a, b] fig2 = [c, d] fig1.inject(0){ |res, elem| res + elem } <= fig2.inject(0){ |res, elem| res + elem } ? "It is possible" : "It is not possible" end |
#task64(n:) ⇒ Object
248 249 250 251 |
# File 'lib/tasks.rb', line 248 def task64(n: ) $n = n n > 99 ? n / 100 : 0 end |
#task65(n:) ⇒ Object
253 254 255 256 |
# File 'lib/tasks.rb', line 253 def task65(n: ) $n = n n.to_s.split("").map(&:to_i) == n**2 ? true : false end |
#task73(k:, l:) ⇒ Object
258 259 260 261 262 263 |
# File 'lib/tasks.rb', line 258 def task73(k: , l: ) $k = k $l = l k != l ? new_k = new_l = [k, l].max : new_k = new_l = 0 [new_k, new_l] end |
#task75(n:) ⇒ Object
265 266 267 268 |
# File 'lib/tasks.rb', line 265 def task75(n: ) $n = n ((n % 5) % 3 == 0) || ((n % 3) % 5 == 0) ? true : false end |
#task77(n:) ⇒ Object
270 271 272 273 |
# File 'lib/tasks.rb', line 270 def task77(n: ) $n = n [2**n, ((1..n).inject(:*) || 1)] end |
#task8(corners:, radius:) ⇒ Object
79 80 81 82 83 |
# File 'lib/tasks.rb', line 79 def task8(corners: , radius: ) $corners = corners $radius = radius (2 * radius * Math.tan(Math::PI / corners)) * corners end |
#task809(n:) ⇒ Object
377 378 379 380 381 382 383 384 385 386 |
# File 'lib/tasks.rb', line 377 def task809(n: ) $n = n number = n.to_s.split('').map(&:to_s) i = 3 (number.length / 3).times do number.insert((number.length - i), " ") i += 4 end number.join end |
#task811(n:) ⇒ Object
388 389 390 391 392 393 394 |
# File 'lib/tasks.rb', line 388 def task811(n: ) $n = n rur = n / 100 penny = n - (rur * 100) penny.to_s.split('').map(&:to_s).length == 1 ? (penny.insert(0, "0")) : penny "the prise is #{rur} rur. #{penny} penny." end |
#task822(year:) ⇒ Object
396 397 398 399 |
# File 'lib/tasks.rb', line 396 def task822(year: ) $year = year (year % 4).zero? && year % 100 != 0 || (year % 400).zero? ? "366 days in #{year}" : "365 days in #{year}" end |
#task87(n:, m:) ⇒ Object
275 276 277 278 279 |
# File 'lib/tasks.rb', line 275 def task87(n: , m: ) $n = n $m = m (n.to_s.split("").map(&:to_i)).last(m).inject(0){ |res, elem| res = res + elem } end |
#task9(r1:, r2:, r3:) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/tasks.rb', line 85 def task9(r1: , r2: , r3: ) $r1 = r1 $r2 = r2 $r3 = r3 Float(1 / (r1**-1 + r2**-1 + r3**-1)) end |
#task_37(a:, b:, c:) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/tasks.rb', line 181 def task_37(a: , b:, c: ) $a = a $b = b $c = c if a >= b && b >= c a *= 2 b *= 2 c *= 2 else a = a.abs b = b.abs c = c.abs end [a, b, c] end |