Решение на Трета задача от Бони Бонев

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

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

Резултати

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

Код

class RationalSequence
include Enumerable
def initialize(range)
@range, @should_stop = range, range >= 0
end
def each
top, bottom, count = 1, 1, 0
while count < @range or not @should_stop
if mutual_simple_fraction? top, bottom
yield Rational(top, bottom)
count += 1
end
current_top = top
current_bottom = bottom
top = get_next_top current_top, current_bottom
bottom = get_next_bottom current_top, current_bottom
end
end
def get_next_top(top, bottom)
if top % 2 == bottom % 2
top + 1
else
top - 1 > 1 ? top - 1 : 1
end
end
def get_next_bottom(top, bottom)
if top % 2 == bottom % 2
bottom - 1 > 1 ? bottom - 1 : 1
else
bottom + 1
end
end
def mutual_simple_fraction?(top, bottom)
top.gcd(bottom) == 1
end
end
class PrimeSequence
include Enumerable
def initialize(range)
@range = range
end
def each
current = 1
length = 0
while length < @range
current += 1
while not prime? current
current += 1
end
length += 1
yield current
end
end
def prime?(number)
max_number = Math.sqrt number
current = 2
while current <= max_number
if number % current == 0
return false
end
current += 1
end
true
end
end
class FibonacciSequence
include Enumerable
def initialize(range, first: 1, second: 1)
@range, @first, @second = range, first, second
end
def each
step = 0
while step < @range
yield @first
@second, @first = @second + @first, @second
step += 1
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
prime_object = PrimeSequence.new(0)
all_rationals = RationalSequence.new(n).to_a
primes = all_rationals.select { |item|
prime_object.prime?(item.denominator) and prime_object.prime?(item.numerator) }
others = all_rationals - primes
prime = multiply primes
other = multiply others
prime / other
end
def aimless(n)
sequence = PrimeSequence.new(n).to_a
if not sequence.length % 2 == 0
sequence.push 1
end
array = Array.new
index = 0
while index < sequence.length - 1
array.push Rational(sequence[index], sequence[index + 1])
index += 2
end
array.length > 0 ? array.reduce { |first, second| first + second } : 1
end
def worthless(n)
rational_sequence = RationalSequence.new -1
fibonacci_number = FibonacciSequence.new(n).to_a[-1]
sum = 0
result = rational_sequence.take_while { |item|
sum += item
sum <= fibonacci_number }
end
def multiply(array)
array.length > 0 ? array.reduce { |first, second| first * second } : 1
end
end

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

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

Finished in 0.01173 seconds
20 examples, 0 failures

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

Бони обнови решението на 24.10.2015 22:13 (преди над 8 години)

+class RationalSequence
+ include Enumerable
+
+ def initialize(range)
+ @range, @should_stop = range, range >= 0
+ end
+
+ def each
+ top, bottom, count = 1, 1, 0
+
+ while count < @range or not @should_stop
+ if mutual_simple_fraction? top, bottom
+ yield Rational(top, bottom)
+ count += 1
+ end
+
+ current_top = top
+ current_bottom = bottom
+ top = get_next_top current_top, current_bottom
+ bottom = get_next_bottom current_top, current_bottom
+ end
+ end
+
+ def get_next_top(top, bottom)
+ if top % 2 == bottom % 2
+ top + 1
+ else
+ top - 1 > 1 ? top - 1 : 1
+ end
+ end
+
+ def get_next_bottom(top, bottom)
+ if top % 2 == bottom % 2
+ bottom - 1 > 1 ? bottom - 1 : 1
+ else
+ bottom + 1
+ end
+ end
+
+ def mutual_simple_fraction?(top, bottom)
+ top.gcd(bottom) == 1
+ end
+end
+
+class PrimeSequence
+ include Enumerable
+
+ def initialize(range)
+ @range = range
+ end
+
+ def each
+ current = 1
+ length = 0
+
+ while length < @range
+ current += 1
+ while not prime? current
+ current += 1
+ end
+
+ length += 1
+ yield current
+ end
+ end
+
+ def prime?(number)
+ max_number = Math.sqrt number
+
+ current = 2
+ while current <= max_number
+ if number % current == 0
+ return false
+ end
+
+ current += 1
+ end
+
+ true
+ end
+end
+
+class FibonacciSequence
+ include Enumerable
+
+ def initialize(range, first: 1, second: 1)
+ @range, @first, @second = range, first, second
+ end
+
+ def each
+ step = 0
+
+ while step < @range
+ yield @first
+ @second, @first = @second + @first, @second
+ step += 1
+ end
+ end
+end
+
+module DrunkenMathematician
+ module_function
+
+ def meaningless(n)
+ prime_object = PrimeSequence.new(0)
+ all_rationals = RationalSequence.new(n).to_a
+
+ primes = all_rationals.select { |item|
+ prime_object.prime?(item.denominator) and prime_object.prime?(item.numerator) }
+
+ others = all_rationals - primes
+
+ prime = multiply primes
+ other = multiply others
+ prime / other
+ end
+
+ def aimless(n)
+ sequence = PrimeSequence.new(n).to_a
+ if not sequence.length % 2 == 0
+ sequence.push 1
+ end
+
+ array = Array.new
+ index = 0
+ while index < sequence.length - 1
+ array.push Rational(sequence[index], sequence[index + 1])
+ index += 2
+ end
+
+ array.length > 0 ? array.reduce { |first, second| first + second } : 1
+ end
+
+ def worthless(n)
+ rational_sequence = RationalSequence.new -1
+ fibonacci_number = FibonacciSequence.new(n).to_a[-1]
+ sum = 0
+
+ result = rational_sequence.take_while { |item|
+ sum += item
+ sum <= fibonacci_number }
+ end
+
+ def multiply(array)
+ array.length > 0 ? array.reduce { |first, second| first * second } : 1
+ end
+end

Бони обнови решението на 25.10.2015 11:41 (преди над 8 години)

class RationalSequence
include Enumerable
def initialize(range)
@range, @should_stop = range, range >= 0
end
def each
top, bottom, count = 1, 1, 0
while count < @range or not @should_stop
- if mutual_simple_fraction? top, bottom
+ if mutual_simple_fraction? top, bottom
yield Rational(top, bottom)
count += 1
- end
+ end
- current_top = top
- current_bottom = bottom
+ current_top = top
+ current_bottom = bottom
top = get_next_top current_top, current_bottom
- bottom = get_next_bottom current_top, current_bottom
+ bottom = get_next_bottom current_top, current_bottom
end
end
def get_next_top(top, bottom)
if top % 2 == bottom % 2
top + 1
else
top - 1 > 1 ? top - 1 : 1
end
end
def get_next_bottom(top, bottom)
if top % 2 == bottom % 2
bottom - 1 > 1 ? bottom - 1 : 1
else
bottom + 1
end
end
def mutual_simple_fraction?(top, bottom)
top.gcd(bottom) == 1
end
end
class PrimeSequence
include Enumerable
def initialize(range)
@range = range
end
def each
current = 1
- length = 0
+ length = 0
while length < @range
- current += 1
- while not prime? current
+ current += 1
+ while not prime? current
current += 1
- end
+ end
- length += 1
+ length += 1
yield current
end
end
def prime?(number)
max_number = Math.sqrt number
- current = 2
+ current = 2
while current <= max_number
- if number % current == 0
+ if number % current == 0
return false
- end
+ end
- current += 1
+ current += 1
end
true
end
end
class FibonacciSequence
include Enumerable
def initialize(range, first: 1, second: 1)
@range, @first, @second = range, first, second
end
def each
step = 0
while step < @range
yield @first
@second, @first = @second + @first, @second
- step += 1
+ step += 1
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
prime_object = PrimeSequence.new(0)
all_rationals = RationalSequence.new(n).to_a
primes = all_rationals.select { |item|
- prime_object.prime?(item.denominator) and prime_object.prime?(item.numerator) }
+ prime_object.prime?(item.denominator) and prime_object.prime?(item.numerator) }
others = all_rationals - primes
prime = multiply primes
- other = multiply others
- prime / other
+ other = multiply others
+ prime / other
end
def aimless(n)
sequence = PrimeSequence.new(n).to_a
if not sequence.length % 2 == 0
sequence.push 1
end
array = Array.new
index = 0
while index < sequence.length - 1
array.push Rational(sequence[index], sequence[index + 1])
index += 2
end
array.length > 0 ? array.reduce { |first, second| first + second } : 1
end
def worthless(n)
rational_sequence = RationalSequence.new -1
- fibonacci_number = FibonacciSequence.new(n).to_a[-1]
+ fibonacci_number = FibonacciSequence.new(n).to_a[-1]
sum = 0
result = rational_sequence.take_while { |item|
sum += item
sum <= fibonacci_number }
end
def multiply(array)
array.length > 0 ? array.reduce { |first, second| first * second } : 1
end
end

Бони обнови решението на 25.10.2015 11:41 (преди над 8 години)

class RationalSequence
include Enumerable
def initialize(range)
@range, @should_stop = range, range >= 0
end
def each
top, bottom, count = 1, 1, 0
while count < @range or not @should_stop
if mutual_simple_fraction? top, bottom
yield Rational(top, bottom)
- count += 1
+ count += 1
end
current_top = top
current_bottom = bottom
top = get_next_top current_top, current_bottom
bottom = get_next_bottom current_top, current_bottom
end
end
def get_next_top(top, bottom)
if top % 2 == bottom % 2
top + 1
else
top - 1 > 1 ? top - 1 : 1
end
end
def get_next_bottom(top, bottom)
if top % 2 == bottom % 2
bottom - 1 > 1 ? bottom - 1 : 1
else
bottom + 1
end
end
def mutual_simple_fraction?(top, bottom)
top.gcd(bottom) == 1
end
end
class PrimeSequence
include Enumerable
def initialize(range)
@range = range
end
def each
current = 1
length = 0
while length < @range
current += 1
while not prime? current
current += 1
end
length += 1
yield current
end
end
def prime?(number)
max_number = Math.sqrt number
current = 2
while current <= max_number
if number % current == 0
return false
end
current += 1
end
true
end
end
class FibonacciSequence
include Enumerable
def initialize(range, first: 1, second: 1)
@range, @first, @second = range, first, second
end
def each
step = 0
while step < @range
yield @first
@second, @first = @second + @first, @second
step += 1
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
prime_object = PrimeSequence.new(0)
all_rationals = RationalSequence.new(n).to_a
primes = all_rationals.select { |item|
prime_object.prime?(item.denominator) and prime_object.prime?(item.numerator) }
others = all_rationals - primes
prime = multiply primes
other = multiply others
prime / other
end
def aimless(n)
sequence = PrimeSequence.new(n).to_a
if not sequence.length % 2 == 0
sequence.push 1
end
array = Array.new
index = 0
while index < sequence.length - 1
array.push Rational(sequence[index], sequence[index + 1])
index += 2
end
array.length > 0 ? array.reduce { |first, second| first + second } : 1
end
def worthless(n)
rational_sequence = RationalSequence.new -1
fibonacci_number = FibonacciSequence.new(n).to_a[-1]
sum = 0
result = rational_sequence.take_while { |item|
sum += item
sum <= fibonacci_number }
end
def multiply(array)
array.length > 0 ? array.reduce { |first, second| first * second } : 1
end
end

Бони обнови решението на 25.10.2015 11:42 (преди над 8 години)

class RationalSequence
include Enumerable
def initialize(range)
@range, @should_stop = range, range >= 0
end
def each
top, bottom, count = 1, 1, 0
while count < @range or not @should_stop
if mutual_simple_fraction? top, bottom
yield Rational(top, bottom)
count += 1
- end
+ end
current_top = top
current_bottom = bottom
top = get_next_top current_top, current_bottom
bottom = get_next_bottom current_top, current_bottom
end
end
def get_next_top(top, bottom)
if top % 2 == bottom % 2
top + 1
else
top - 1 > 1 ? top - 1 : 1
end
end
def get_next_bottom(top, bottom)
if top % 2 == bottom % 2
bottom - 1 > 1 ? bottom - 1 : 1
else
bottom + 1
end
end
def mutual_simple_fraction?(top, bottom)
top.gcd(bottom) == 1
end
end
class PrimeSequence
include Enumerable
def initialize(range)
@range = range
end
def each
current = 1
length = 0
while length < @range
current += 1
while not prime? current
current += 1
end
length += 1
yield current
end
end
def prime?(number)
max_number = Math.sqrt number
current = 2
while current <= max_number
if number % current == 0
return false
end
current += 1
end
true
end
end
class FibonacciSequence
include Enumerable
def initialize(range, first: 1, second: 1)
@range, @first, @second = range, first, second
end
def each
step = 0
while step < @range
yield @first
@second, @first = @second + @first, @second
step += 1
end
end
end
module DrunkenMathematician
module_function
def meaningless(n)
prime_object = PrimeSequence.new(0)
all_rationals = RationalSequence.new(n).to_a
primes = all_rationals.select { |item|
prime_object.prime?(item.denominator) and prime_object.prime?(item.numerator) }
others = all_rationals - primes
prime = multiply primes
other = multiply others
prime / other
end
def aimless(n)
sequence = PrimeSequence.new(n).to_a
if not sequence.length % 2 == 0
sequence.push 1
end
array = Array.new
index = 0
while index < sequence.length - 1
array.push Rational(sequence[index], sequence[index + 1])
index += 2
end
array.length > 0 ? array.reduce { |first, second| first + second } : 1
end
def worthless(n)
rational_sequence = RationalSequence.new -1
fibonacci_number = FibonacciSequence.new(n).to_a[-1]
sum = 0
result = rational_sequence.take_while { |item|
sum += item
sum <= fibonacci_number }
end
def multiply(array)
array.length > 0 ? array.reduce { |first, second| first * second } : 1
end
end