0% found this document useful (0 votes)
3 views2 pages

Task 1 Algo

The document contains two tasks involving Python code for measuring the time complexity of algorithms. Task 1 checks if numbers are prime and plots the time taken for different input sizes, while Task 2 compares the time taken for recursive and iterative implementations of the Fibonacci sequence. Both tasks utilize matplotlib for plotting the results.

Uploaded by

tawfikhisham37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Task 1 Algo

The document contains two tasks involving Python code for measuring the time complexity of algorithms. Task 1 checks if numbers are prime and plots the time taken for different input sizes, while Task 2 compares the time taken for recursive and iterative implementations of the Fibonacci sequence. Both tasks utilize matplotlib for plotting the results.

Uploaded by

tawfikhisham37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// task1//

******************************************************************
*********
import matplotlib.pyplot as plt
import time
import random

:def is_prime(n)
:if n < 2
return False
:for i in range(2, int(n**0.5) + 1)
:if n % i == 0
return False
return True
problem_sizes = list(range(0, 31, 1))
][ = times
:for n in problem_sizes
input_value = n
)(start = time.time
result = is_prime(input_value)
)(end = time.time
time_taken = end - start
times.append(time_taken)
print(f"For n={n}, is prime({input_value}) is take
{time_taken} seconds")
plt.plot(problem_sizes, times)
plt.xlabel('Problem Size (n)')
plt.ylabel('Time Taken (seconds)')
plt.title('Prime Checking Time Complexity')
)(plt.show
-----------------------------------------------------------------
------------------------
//task 2//
******************************************************************
**************************
import matplotlib.pyplot as plt
import time

:def fibonacci_recursive(n)
:if n == 0
return 0
:elif n == 1
return 1
:else
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)

:def fibonacci_iterative(n)
a, b = 0, 1
:for _ in range(n)
a, b = b, a + b
return a

:def measure_time(func, n)
)(start_time = time.time
result = func(n)
)(end_time = time.time
return (end_time - start_time) * 1000
n = 35

recursive_times = [measure_time(fibonacci_recursive, i) for i in


range(n)]
iterative_times = [measure_time(fibonacci_iterative, i) for i in
range(n)]

plt.plot(range(n), recursive_times, label='Recursive')


plt.plot(range(n), iterative_times, label='Iterative')

plt.xlabel('n (Index of Fibonacci Number)')


plt.ylabel('Time (milliseconds)')
plt.title('Time Complexity of Fibonacci Sequence')

)(plt.legend

)(plt.show

You might also like