def problem_2(limit=4e6): """ Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. """ # This could be made much faster sum = 0 iteration = 0 current = functions.fibonacci(iteration) while current < limit: if not current % 2: sum += current iteration += 1 current = functions.fibonacci(iteration) return sum
def get(self, start_idx, end_idx): # Start the fibonacci. time_start = time.process_time() data = [fibonacci(n) for n in range(start_idx, end_idx)] time_elapsed = (time.process_time() - time_start) # To include the last fibonacci number. data.append(fibonacci(end_idx)) # Include benchmark results. # data.append({'benchmark': { # 'time_elapsed': f'{time_elapsed: .5f}', # } # }) # Log to database fib_data = FibData(start=start_idx, end=end_idx) db.session.add(fib_data) db.session.commit() return data
def get_fibonacci_number(message): # получаем число global number number = message.text if number.isdigit(): result_fibonacci = fibonacci(int(number)) fib_number = list(result_fibonacci)[0] fib_number = str(fib_number).replace('[', '').replace(']', '') print(fib_number) fib_sequence = list(result_fibonacci)[1] fib_sequence = str(fib_sequence).replace('[', '').replace(']', '') print(fib_sequence) bot.send_message(message.chat.id, f"{number} - имеет следующую последовательность Фибоначчи: \n" f"{fib_number}\n" f"Золотым сечением данной последовательности является число: " f"{fib_sequence}") keyboard = types.InlineKeyboardMarkup() key_yes = types.InlineKeyboardButton(text='❤️', callback_data='yes') # кнопка «Да» keyboard.add(key_yes) key_no = types.InlineKeyboardButton(text='😡', callback_data='no') keyboard.add(key_no) bot.send_message(message.from_user.id, text=f"Вам понравилось качество нашего обслуживания?", reply_markup=keyboard) @bot.callback_query_handler(func=lambda call: True) def callback_worker(call): if call.data == "yes": # call.data это callback_data, которую мы указали при объявлении кнопки bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAID0l9BbMxW6Q0_yRZ7ahHsU8CQO6c7AALHAAMWHfwLeXVXmujsd1AbBA') msg_id = message.message_id + 2 bot.delete_message(message.chat.id, msg_id) elif call.data == "no": bot.send_sticker(message.chat.id, 'CAACAgIAAxkBAAID0F9BbHvp_tcecJxgEApxkT3ZEnIIAAKsAAM60N8BRmmum8GHZ5wbBA') msg_id = message.message_id + 2 bot.delete_message(message.chat.id, msg_id) else: bot.send_message(message.chat.id, f"Введите число") bot.register_next_step_handler(message, get_fibonacci_number)
def test_large_numbers(self): self.assertEqual(102334155, functions.fibonacci(40)) self.assertEqual(1548008755920, functions.fibonacci(60)) self.assertEqual( 222232244629420445529739893461909967206666939096499764990979600, functions.fibonacci(300))
def test_small_numbers(self): answers = [2, 3, 5, 8, 13, 21, 34] for answer, n in zip(answers, range(3, 10)): self.assertEqual(answer, functions.fibonacci(n))
def test_base_case(self): self.assertEqual(0, functions.fibonacci(0)) self.assertEqual(1, functions.fibonacci(1))
def test_fibonacci(): assert fibonacci(7) == [1, 1, 2, 3, 5, 8, 13]
import sys, os, re sys.path.append("../src") def modifyPythonPathWithStage(): base = "../src/build/" # search for directory begining with 'lib' for root, dirs, files in os.walk(base): for dir in dirs: if re.search(r"^lib", dir) != None: stage = dir try: sys.path.append(base + stage) except: print("must build application first") sys.exit() modifyPythonPathWithStage() import functions functions.say_hello() functions.say_goodbye() print((functions.fibonacci(100000))) print((functions.sumOfSquares(2, 4)))
from importlib import reload __author__ = "Bartlomiej Ciurus" import functions print("First: " + str(functions.factorial(8))) print("Second: " + str(functions.fibonacci(8))) reload(functions) print("Third: " + str(functions.factorial(8))) print("Fourth: " + str(functions.fibonacci(8))) del functions import functions as func print("Fifth: " + str(func.factorial(6))) print("Sixth: " + str(func.fibonacci(5))) reload(func) print("Seventh: " + str(func.factorial(6))) print("Eight: " + str(func.fibonacci(5))) del func from functions import * print("Ninth: " + str(fibonacci(8)))