Write a Python program that prints the first 10 Fibonacci numbers.
- User
Write a Python program that prints the first 10 Fibonacci numbers.
- 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=" ")
- 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) - Assistant
This Python code defines two functions:
-
is_prime(num): This function checks whether a given numbernumis a prime number or not. If the number is prime, it returnsTrue, and if not, it returnsFalse. -
print_primes(start, end): This function takes two input argumentsstartandendand prints all the prime numbers betweenstartandend, inclusive. It callsis_prime(num)to check whether each number betweenstartandendis 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 -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.