-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2612.rb
62 lines (58 loc) · 974 Bytes
/
2612.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
def hello_world(val)
'HelloWorld' * val
end
def fizz_buzz(val)
if val % 15 == 0
'FizzBuzz'
elsif val % 5 == 0
'Buzz'
elsif val % 3 == 0
'Fizz'
else
val
end
end
def prime(val)
primes = [2]
return primes[val-1] if val == 1
i = 3
loop do
is_prime = true
3.step(Math.sqrt(i), 2) do |j|
if i % j == 0
is_prime = false
break
end
end
if is_prime
primes << i
if primes.size == val
return i
end
end
i += 2
end
end
def fibonacci(val)
list = [1, 1]
return list[val-1] if val <= 2
i = 2
loop do
list << list[i-1] + list[i-2]
return list[i] if val == i + 1
i += 1
end
end
while str = STDIN.gets
key, value = str.chomp.split ','
case key
when 'HelloWorld'
puts hello_world(value.to_i)
when 'FizzBuzz'
puts fizz_buzz(value.to_i)
when 'Prime'
puts prime(value.to_i)
when 'Fibonacci'
puts fibonacci(value.to_i)
end
end