CODE

CODE


def fib_recursive(n):

if n <= 0:

return 0

elif n == 1:

return 1

else:

return fib_recursive(n - 1) + fib_recursive(n - 2)


def fib_iterative(n):

if n <= 0:

return 0

elif n == 1:

return 1

else:

fib_numbers = [0, 1]

for i in range(2, n + 1):

fib_numbers.append(fib_numbers[i - 1] + fib_numbers[i - 2])

return fib_numbers[n]


def factorial_recursive(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial_recursive(n - 1)


def factorial_iterative(n):

if n == 0 or n == 1:

return 1

else:

result = 1

for i in range(2, n + 1):

result *= i

return result


def find_smallest_divisor(num):

for i in range(2, num + 1):

if num % i == 0:

return i

return num


def is_prime(num):

if num < 2:

return False

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

return False

return True


def calculate_sum_of_digits(number):

sum_of_digits = 0

while number > 0:

digit = number % 10

sum_of_digits += digit

number = number // 10

return sum_of_digits


def reverse_string(input_string):

return input_string[::-1]


def count_vowels(input_string):

vowels = "aeiouAEIOU"

count = 0

for char in input_string:

if char in vowels:

count += 1

return count


def generate_fibonacci_sequence(length):

fib_sequence = [0, 1]

while len(fib_sequence) < length:

next_number = fib_sequence[-1] + fib_sequence[-2]

fib_sequence.append(next_number)

return fib_sequence


def main():

num = 10

print(f"The {num}th Fibonacci number (recursive): {fib_recursive(num)}")

print(f"The {num}th Fibonacci number (iterative): {fib_iterative(num)}")


fact_num = 5

print(f"The factorial of {fact_num} (recursive): {factorial_recursive(fact_num)}")

print(f"The factorial of {fact_num} (iterative): {factorial_iterative(fact_num)}")


num_to_factor = 20

smallest_divisor = find_smallest_divisor(num_to_factor)

print(f"The smallest divisor of {num_to_factor} is: {smallest_divisor}")


prime_check = 37

print(f"{prime_check} is prime: {is_prime(prime_check)}")


num_to_sum_digits = 12345

print(f"The sum of digits of {num_to_sum_digits} is: {calculate_sum_of_digits(num_to_sum_digits)}")


input_str = "Hello, World!"

print(f"The reversed string is: {reverse_string(input_str)}")


input_str_vowels = "Hello, how are you?"

print(f"Number of vowels in the string: {count_vowels(input_str_vowels)}")


fib_sequence_length = 10

print(f"Fibonacci sequence of length {fib_sequence_

length}: {generate_fibonacci_sequence(fib_sequence_length)}")


if __name__ == "__main__":

main()

Report Page