Skip to content
TheChatArchiveThe Chat Archive
Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)Hugging Face OpenAssistant Conversations (oasst2)

Please write me python code to get all Fibonacci numbers until 10 with a recursive…

In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
  1. User

    Please write me python code to get all Fibonacci numbers until 10 with a recursive function.

  2. Assistant
    def _fibonacci_req(tm2=0, tm1=0, max_num=0):
    	"""
    	Function to recursively yield all Fibonacci numbers until max_num
    	:param tm2: number at t-2
    	:param tm1: number at t-1
    	:param max_num: maximum number to yield
    	:return: the fibonacci number at t
    	"""
    	if tm1 > max_num:
    		return
    	yield tm1
    	t = tm1+tm2 if tm1 > 0 else 1
    	yield from _fibonacci_req(tm1, t, max_num)
    
    def fibonacci_numbers(max_num=10):
    	"""
    	Function to get all Fibonacci numbers until max_num (default 10)
    	:param max_num: maximum number of the list
    	:return:
    	"""
    	return [x for x in _fibonacci_req(max_num=max_num)]
    
  3. User

    Thanks. Can you now update it to 15?

  4. Assistant

    There's no need to update anything in the code.

    If you want to use the number 15, all you have to do is pass that number into the fibonacci_numbers function when you call it.

    Here's an example:

    # Call the fibonacci function and pass the number 15
    sequence = fibonacci_numbers(15)
    
    # Print each number in the sequence
    for n in sequence:
      print(n)
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.