Решение на Трета задача от Даниела Иванова

Обратно към всички решения

Към профила на Даниела Иванова

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 20 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class RationalSequence
include Enumerable
def initialize(count)
@count = count
end
def each
numerator = 1
denominator = 1
counter = 0
members = []
while counter < @count
fraction = Rational(numerator, denominator)
if !members.include?(fraction)
members.push(fraction)
counter += 1
yield fraction
end
if numerator % 2 == denominator % 2
numerator += 1
denominator = [denominator - 1, 1].max
else
denominator += 1
numerator = [numerator - 1, 1].max
end
end
end
end
class PrimeSequence
include Enumerable
def initialize(count)
@count = count
end
def each
counter = 0
prime_candidate = 2
while counter < @count
if DrunkenMathematician.is_prime?(prime_candidate)
yield prime_candidate
counter += 1
end
prime_candidate += 1
end
end
end
class FibonacciSequence
include Enumerable
def initialize(count, first: 1, second: 1)
@count = count
@first = first
@second = second
end
def each
counter = 0
fibonacci_elements = [@first, @second]
while counter < @count
fibonacci_elements.push(fibonacci_elements[-1] + fibonacci_elements[-2])
yield fibonacci_elements[counter]
counter += 1
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
prime_group = []
non_prime_group = []
RationalSequence.new(n).each do |element|
if is_prime?(element.denominator) || is_prime?(element.numerator)
prime_group.push(element)
else
non_prime_group.push(element)
end
end
(prime_group.reduce(:*) || 1) / (non_prime_group.reduce(:*) || 1)
end
def aimless(n)
arr_primes = PrimeSequence.new(n).to_a
rationals = []
while !arr_primes.empty?
rationals.push(Rational(arr_primes.shift, arr_primes.shift || 1))
end
rationals.reduce(:+) || 0
end
def worthless(n)
fibonacci_element = FibonacciSequence.new(n).to_a.last
sum_of_rationals = 0
rationals = RationalSequence.new(Float::INFINITY)
rationals.take_while{ |rational| (sum_of_rationals += rational) <= fibonacci_element }
end
def is_prime?(number)
return false if number < 2
Math.sqrt(number).to_i.downto(2).all? { |divisor| number % divisor != 0 }
end
end

Лог от изпълнението

....................

Finished in 0.01704 seconds
20 examples, 0 failures

История (1 версия и 0 коментара)

Даниела обнови решението на 24.10.2015 19:49 (преди над 8 години)

+class RationalSequence
+ include Enumerable
+
+ def initialize(count)
+ @count = count
+ end
+
+ def each
+ numerator = 1
+ denominator = 1
+ counter = 0
+ members = []
+
+ while counter < @count
+ fraction = Rational(numerator, denominator)
+ if !members.include?(fraction)
+ members.push(fraction)
+ counter += 1
+
+ yield fraction
+ end
+
+ if numerator % 2 == denominator % 2
+ numerator += 1
+ denominator = [denominator - 1, 1].max
+ else
+ denominator += 1
+ numerator = [numerator - 1, 1].max
+ end
+ end
+ end
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(count)
+ @count = count
+ end
+
+ def each
+ counter = 0
+ prime_candidate = 2
+
+ while counter < @count
+ if DrunkenMathematician.is_prime?(prime_candidate)
+ yield prime_candidate
+ counter += 1
+ end
+
+ prime_candidate += 1
+ end
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(count, first: 1, second: 1)
+ @count = count
+ @first = first
+ @second = second
+ end
+
+ def each
+ counter = 0
+ fibonacci_elements = [@first, @second]
+
+ while counter < @count
+ fibonacci_elements.push(fibonacci_elements[-1] + fibonacci_elements[-2])
+ yield fibonacci_elements[counter]
+ counter += 1
+ end
+ end
+end
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ prime_group = []
+ non_prime_group = []
+
+ RationalSequence.new(n).each do |element|
+ if is_prime?(element.denominator) || is_prime?(element.numerator)
+ prime_group.push(element)
+ else
+ non_prime_group.push(element)
+ end
+ end
+
+ (prime_group.reduce(:*) || 1) / (non_prime_group.reduce(:*) || 1)
+ end
+
+ def aimless(n)
+ arr_primes = PrimeSequence.new(n).to_a
+ rationals = []
+ while !arr_primes.empty?
+ rationals.push(Rational(arr_primes.shift, arr_primes.shift || 1))
+ end
+
+ rationals.reduce(:+) || 0
+ end
+
+ def worthless(n)
+ fibonacci_element = FibonacciSequence.new(n).to_a.last
+ sum_of_rationals = 0
+ rationals = RationalSequence.new(Float::INFINITY)
+ rationals.take_while{ |rational| (sum_of_rationals += rational) <= fibonacci_element }
+ end
+ def is_prime?(number)
+ return false if number < 2
+ Math.sqrt(number).to_i.downto(2).all? { |divisor| number % divisor != 0 }
+ end
+end