Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Write a Python program that prints the first 10 Fibonacci numbers.

  1. User

    Write a Python program that prints the first 10 Fibonacci numbers.

  2. Assistant

    def fib(n): if n <= 1: return n else: return fib(n-1) + fib(n-2)

    num_terms = 10

    for i in range(num_terms): print(fib(i), end=" ")

  3. User

    Evaluate:

    def print_primes(start, end):
        for num in range(start, end+1):
            if is_prime(num):
                print(num, end=" ")
        print()
    
    def is_prime(num):
        if num <= 1:
            return False
        elif num == 2:
            return True
        else:
            for i in range(2, int(num ** 0.5) + 1):
                if num % i == 0:
                    return False
            return True
    
    print_primes(2, 30)
    
  4. Assistant

    This Python code defines two functions:

    1. is_prime(num): This function checks whether a given number num is a prime number or not. If the number is prime, it returns True, and if not, it returns False.

    2. print_primes(start, end): This function takes two input arguments start and end and prints all the prime numbers between start and end, inclusive. It calls is_prime(num) to check whether each number between start and end is prime or not.

    In the last line of the code, the function print_primes(2, 30) is called, which prints all prime numbers in the range of 2 to 30.

    When you run the code, you will see the following output:

    2 3 5 7 11 13 17 19 23 29 
    

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